Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 0d8b850

Browse files
authored
doc: improved documentation for new parts, added todos (#12)
Co-authored-by: Jan Soukup <[email protected]>
1 parent ee93b03 commit 0d8b850

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

Diff for: README.md

+47-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ This is a set of utils needed for deploying NextJS into AWS Lambda.
44
It includes a wrapper for `next/server/image-optimizer` allowing to use S3.
55
And includes CLI and custom server handler to integrate with ApiGw.
66

7+
- [NextJS Lambda Utils](#nextjs-lambda-utils)
8+
- [Usage](#usage)
9+
- [Sharp](#sharp)
10+
- [Packaging](#packaging)
11+
- [Server handler](#server-handler)
12+
- [Static assets](#static-assets)
13+
- [TODO](#todo)
14+
715
## Usage
816

917
Create new lambda function with NODE_16 runtime in AWS.
@@ -45,8 +53,44 @@ const sharpLayer = new LayerVersion(this, 'SharpLayer', {
4553
})
4654
```
4755

48-
## Notes
56+
## Packaging
57+
58+
In order to succefully deploy, you firstly need to include `target: 'standalone'` in your `next.config.js` setup.
59+
Make sure to use NextJS in version 12 or above so this is properly supported.
60+
61+
Once target is set, you can go on and use your `next build` command as you normally would.
62+
To package everything, make sure to be in your project root folder and next folder `.next` and `public` exist. Packaging is done via NPM CLI command of `@slack/nextjs-lambda package`.
63+
64+
It will create `next.out/` folder with 3 zip packages. One zip Lambda's code, one is dependencies layer and one is assets layer.
65+
66+
- code zip: include all files generated by next that are required to run on Lambda behind ApiGateway. Original handler as well as new server handler are included. Use `handler.handler` for custom one or `server.handler` for original one.
67+
- dependencies layer: all transpilied `node_modules`. Next includes only used files, dramatically reducing overall size.
68+
- assets layer: your public folder together with generated assets. Keep in mind that to public refer file, you need to include it in `public/assets/` folder, not just in public. This limitation dramatically simplifies whole setup. This zip file is uploaded to S3, it's not included in Lambda code.
69+
70+
### Server handler
71+
72+
Custom wrapper around NextServer to allow for passing ApiGateway events into Next server.
73+
74+
Cloudfront paths used:
75+
76+
- `default`
77+
- `_next/data/*`
78+
79+
### Static assets
80+
81+
Next uses multiple directories to determine which file should be served. By default next provides us with list of routes for API/images/assets/pages. To simplify the process as much as possible, we are tapping into resulting paths.
82+
83+
We are packaging those assets to simulate output structure and we are using S3 behind CloudFront to serve those files.
84+
Also, Image Handler is tapping into S3 to provide images, so correct folder structure is crucial.
85+
86+
Cloudfront paths used:
87+
88+
- `_next/*`
89+
- `assets/*`
4990

50-
This is part of NextJS to Lambda deployment process. More info to follow.
91+
# TODO
5192

52-
## @TODO: Add Server-handler description
93+
- Explain script used for packaging Next app,
94+
- Add CDK examples on how to set it up,
95+
- Export CDK contruct for simple plug-n-play use,
96+
- Use lib/index.ts as single entry and export all paths/functions from it (including zip paths).

0 commit comments

Comments
 (0)