Skip to content

Commit d291e6a

Browse files
authored
docs: add documentation on minimizing lambda bundle (#167)
* Add e2e test instructions; Add documentation on bundle size
1 parent 6efbc25 commit d291e6a

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

docs/pages/common_issues.mdx

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ If you are using sentry, API routes returns empty body. You could try configurin
1414

1515
#### My ISR page has this cache-control header `s-maxage=2, stale-while-revalidate=2592000`
1616

17-
Given how ISR works, while waiting for the revalidation to happen, the page will be served using this cache control header. This prevent your server from being overloaded by a lot of requests while the revalidation is done. You can read more about it [here](/inner_workings/isr).
17+
Given how ISR works, while waiting for the revalidation to happen, the page will be served using this cache control header. This prevent your server from being overloaded by a lot of requests while the revalidation is done. You can read more about it [here](/inner_workings/isr).
18+
19+
#### Unzipped size must be smaller than 262144000 bytes
20+
21+
AWS Lambda has an unzipped size limit of 250MB. If your app is over this limit, then it is most likely using a node_module library that is too large for serverless or there is a large dev dependency getting bundled.
22+
For example, `pdfjs` has `canvas` optional dependency which takes up 180MB. For more details, [read me](/common_issues/bundle_size).
23+
Note: a large bundle size will increase cold start significantly.
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {Callout} from 'nextra/components'
2+
3+
4+
#### Reducing Bundle Size
5+
6+
Next will incorrectly trace dev dependencies to be included in the output `node_modules`, which will significantly increase the lambda bundle. For example, the @swc/core-\* binary is ~33MB!
7+
8+
Add this to your next.config.js to help minimize the lambda bundle size:
9+
10+
```typescript
11+
outputFileTracingExcludes: {
12+
'*': [
13+
'@swc/core',
14+
'esbuild',
15+
'uglify-js',
16+
'watchpack',
17+
'webassemblyjs',
18+
'sharp'
19+
],
20+
},
21+
```
22+
23+
<Callout type="warning" emoji="⚠️">
24+
NextJS currently doesn't expose `outputFileTracingExcludes` as an environmental variable so `open-next`cannot programmatically set this like it does for`output`and`outputFileTracingRoot`.
25+
Currently, next uses `webpack` to trace server actions, so you shouldn't add `webpack` to the excludes list, otherwise it will break server actions.
26+
</Callout>
27+
28+
#### Unzipped size must be smaller than 262144000 bytes
29+
30+
To identify the module that's taking up too much space (and isn't serverless friendly):
31+
32+
```bash
33+
du -hs .open-next/server-function/node_modules/* | sort -rh
34+
```
35+
36+
If your app requires the offending library, then consider moving your business logic of the `api` to its own lambda, eg: `/api/v2` => `Api Lambda`
37+
38+
<Callout type="info" emoji="ℹ️">
39+
There is a [PR](https://github.com/sst/open-next/pull/242) to remove some dev dependency from the output node_modules but that requires more testing before it can merge.
40+
</Callout>
41+
42+
#### Common issues
43+
44+
##### Sharp
45+
46+
`sharp` is not needed outside of the `Image Optimization` function so you should not have it as a dependency. But if you are depending on `sharp`, be sure to install it with the correct flags for your lambda.
47+
eg: `--arch=arm64 --platform=linux --target=18 --libc=glibc`
48+
49+
##### pdfjs
50+
51+
If you need to use pdfjs, you should install it with `npm i pdfjs-dist--no-optional` because the optional dep: `canvas` takes about 180MB.
52+
53+
##### Others
54+
55+
Please open an issue or let us know on discord if there are any other modules that are causing issues.

packages/tests-e2e/README.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ The 3 permutations are:
1010

1111
Their respective `tests/` folders are:
1212

13-
1. [appDirOnly](./tests/appDirOnly)
14-
2. [pagesOnly](./tests/pagesOnly)
15-
3. [appDirAndPages](./tests//appDirAndPages)
13+
1. [appRouter](./tests/appDirOnly)
14+
2. [pagesRouter](./tests/pagesOnly)
15+
3. [appPagesRouter](./tests//appDirAndPages)
1616

1717
Their respective `packages/` are located at:
1818

@@ -22,9 +22,25 @@ Their respective `packages/` are located at:
2222

2323
The GitHub actions will trigger the [e2e test](/.github/workflows//e2e.yml), which deploys the app in the [Example](/example/) folder. The deploy command is:
2424

25-
```
25+
### Running the tests against the deployed app
26+
27+
1. Deploy the app
28+
```bash
29+
cd examples/sst
2630
npx sst deploy --stage e2e
2731
```
32+
2. Export the URLS
33+
```bash
34+
export APP_ROUTER_URL=$(jq -r '.["e2e-example-AppRouter"].url' .sst/outputs.json)
35+
export PAGES_ROUTER_URL=$(jq -r '.["e2e-example-PagesRouter"].url' .sst/outputs.json)
36+
export APP_PAGES_ROUTER_URL=$(jq -r '.["e2e-example-AppPagesRouter"].url' .sst/outputs.json)
37+
```
38+
3. Run the test
39+
```bash
40+
cd ../packages/tests-e2e
41+
npm run e2e:dev
42+
```
43+
2844

2945
## Gotchas
3046

0 commit comments

Comments
 (0)