You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 1, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+56-58
Original file line number
Diff line number
Diff line change
@@ -5,21 +5,28 @@ It includes a wrapper for `next/server/image-optimizer` allowing to use S3.
5
5
And includes CLI and custom server handler to integrate with ApiGw.
6
6
7
7
-[NextJS Lambda Utils](#nextjs-lambda-utils)
8
+
-[TL;DR](#tldr)
8
9
-[Usage](#usage)
9
10
-[next.config.js](#nextconfigjs)
10
11
-[Server handler](#server-handler)
11
12
-[Image handler](#image-handler)
12
13
-[Via CDK](#via-cdk)
13
-
-[Sharp](#sharp)
14
+
-[Sharp layer](#sharp-layer)
15
+
-[Next layer](#next-layer)
14
16
-[Packaging](#packaging)
15
17
-[Server handler](#server-handler-1)
16
18
-[Static assets](#static-assets)
17
19
-[Versioning](#versioning)
18
20
-[Guess](#guess)
19
21
-[Shipit](#shipit)
20
-
-[TODO](#todo)
21
22
-[Disclaimer](#disclaimer)
22
23
24
+
## TL;DR
25
+
- In your NextJS project, set output to standalone.
26
+
- Run `npx @sladg/nextjs-lambda pack`
27
+
- Prepare CDK ([see](#via-cdk))
28
+
- Run `cdk deploy`
29
+
23
30
## Usage
24
31
25
32
We need to create 2 lambdas in order to use NextJS. First one is handling pages/api rendering, second is solving image optimization.
@@ -43,72 +50,68 @@ See:
43
50
44
51
### Server handler
45
52
46
-
```
47
-
next build
53
+
This is a Lambda entrypoint to handle non-asset requests. We need a way to start Next in lambda-friendly way and translate ApiGateway event into typical HTTP event. This is handled by server-handler, which sits alongside of next's `server.js` in standalone output.
48
54
49
-
npx @sladg/nextjs-lambda pack
50
-
```
55
+
### Image handler
51
56
52
-
Create new lambda function with NODE_16 runtime in AWS.
57
+
Lambda consumes Api Gateway requests, so we need to create ApiGw proxy (v2) that will trigger Lambda.
53
58
54
-
```
55
-
const dependenciesLayer = new LayerVersion(this, 'DepsLayer', {
Lambda is designed to serve `_next/image*` route in NextJS stack and replaces the default handler so we can optimize caching and memory limits for page renders and image optimization.
68
60
69
-
### Image handler
61
+
### Via CDK
70
62
71
-
Create new lambda function with NODE_16 runtime in AWS.
63
+
See `NextStandaloneStack` construct in `lib/construct.ts`.
72
64
65
+
You can easily create `cdk/app.ts` and use following code:
const imageOptimizerFn = new Function(this, 'LambdaFunction', {
80
-
code: Code.fromAsset(imageHandlerZip),
81
-
runtime: Runtime.NODEJS_16_X,
82
-
handler: 'image-handler.handler',
83
-
layers: [sharpLayer],
84
-
memorySize: 1024,
85
-
timeout: Duration.seconds(15),
86
-
environment: {
87
-
S3_BUCKET_SOURCE: assetsBucket.bucketName
88
-
}
89
-
})
90
-
```
67
+
#!/usr/bin/env node
68
+
import 'source-map-support/register'
69
+
import * as cdk from 'aws-cdk-lib'
70
+
import * as path from 'path'
91
71
92
-
Lambda consumes Api Gateway requests, so we need to create ApiGw proxy (v2) that will trigger Lambda.
72
+
import { NextStandaloneStack } from '@sladg/nextjs-lambda'
93
73
94
-
Lambda is designed to serve `_next/image*` route in NextJS stack and replaces the default handler so we can optimize caching and memory limits for page renders and image optimization.
new NextStandaloneStack(app, 'StandaloneNextjsStack-2', {
81
+
assetsZipPath,
82
+
codeZipPath,
83
+
dependenciesZipPath,
84
+
})
85
+
```
97
86
98
-
See `NextStandaloneLambda` construct in `lib/construct.ts`.
99
-
This is a complete definition which consumes output of `pack` CLI command. It requires 3 zips (assets, dependencies and code) and exposes get methods for all resources to provide a way to set custom routes, set environment variables, etc.
87
+
This imports pre-made construct, you only need to worry about paths to outputed zip files from CLI `pack` command.
100
88
89
+
> More granular CDK construct coming soon.
101
90
102
-
### Sharp
91
+
92
+
### Sharp layer
103
93
104
94
Besides handler (wrapper) itself, underlying NextJS also requires sharp binaries.
105
95
To build those, we use `npm install` with some extra parametes. Then we zip all sharp dependencies and compress it to easily importable zip file.
import { sharpLayerZipPath } from '@sladg/nextjs-lambda'
109
99
110
-
const sharpLayer = new LayerVersion(this, 'SharpLayer', {
111
-
code: Code.fromAsset(code)
100
+
const sharpLayer = new LayerVersion(this, 'SharpDependenciesLayer', {
101
+
code: Code.fromAsset(sharpLayerZipPath)
102
+
})
103
+
```
104
+
105
+
### Next layer
106
+
107
+
To provide both image and server handlers with all depdencies (next is using `require.resolve` inside, so it cannot be bundled standalone for now).
108
+
109
+
We pre-package this layer so it can be included in Lambda without hasle.
110
+
```
111
+
import { nextLayerZipPath } from '@sladg/nextjs-lambda'
112
+
113
+
const nextLayer = new LayerVersion(this, 'NextDependenciesLayer', {
114
+
code: Code.fromAsset(nextLayerZipPath)
112
115
})
113
116
```
114
117
@@ -147,6 +150,8 @@ Cloudfront paths used:
147
150
-`_next/*`
148
151
-`assets/*`
149
152
153
+
Keep in minda, Cloudfront does not allow for multiple regex patterns in single origin, so using extensions to distinguish image/server handlers is not doable.
154
+
150
155
# Versioning
151
156
152
157
This package exposes two CLI functions intended to deal with versioning your application and releasing.
@@ -164,15 +169,6 @@ Similar to guess command, however, it automatically tags a commit on current bra
164
169
Simply call `@sladg/next-lambda shipit` on any branch and be done.
165
170
166
171
167
-
# TODO
168
-
169
-
- Explain scripts used for packaging Next app,
170
-
- Add CDK examples on how to set it up,
171
-
- Export CDK contruct for simple plug-n-play use,
172
-
- Use lib/index.ts as single entry and export all paths/functions from it (including zip paths).
173
-
- Consider using \*.svg, \*.png, \*.jpeg etc. as routing rule for Cloudfront to distinguish between assets and pages.
174
-
- Add command for guessing version bumps from git commits & keywords, existing solutions are horendously huge, we just need a simple version bumping.
175
-
176
172
# Disclaimer
177
173
178
174
At this point, advanced features were not tested with this setup. This includes:
@@ -184,3 +180,5 @@ At this point, advanced features were not tested with this setup. This includes:
184
180
- custom babel configuration.
185
181
186
182
I am looking for advanced projects implementing those features, so we can test them out! Reach out to me!
0 commit comments