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
Copy file name to clipboardExpand all lines: README.md
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,6 +139,8 @@ Create a CloudFront distribution, and dispatch requests to their cooresponding h
139
139
140
140
Create a Lambda function with the code from `.open-next/middleware-function`, and attach it to the `/_next/data/*` and `/*` behaviors as `viewer request` edge function. This allows the function to run your [Middleware](https://nextjs.org/docs/advanced-features/middleware) code before the request hits your server function, and also before cached content.
141
141
142
+
The middleware function uses the Node.js 18 [global fetch API](https://nodejs.org/de/blog/announcements/v18-release-announce/#new-globally-available-browser-compatible-apis). It requires to run on Node.js 18 runtime. [See why Node.js 18 runtime is required.](#workaround-add-headersgetall-extension-to-the-middleware-function)
143
+
142
144
Note that if middleware is not used in the Next.js app, the `middleware-function` will not be generated. In this case, you don't have to create the Lambda@Edge function, and configure it in the CloudFront distribution.
143
145
144
146
## Limitations and workarounds
@@ -177,6 +179,15 @@ To workaround the issue, the middleware function JSON encodes all request header
177
179
178
180
Note that the `x-op-middleware-request-headers` and `x-op-middleware-response-headers` headers need to be added to CloudFront distribution's cache policy allowed list.
179
181
182
+
#### WORKAROUND: Add `Headers.getAll()` extension to the middleware function
183
+
184
+
Vercel uses the `Headers.getAll()` function in the middleware code. This function is not part of the Node.js 18 [global fetch API](https://nodejs.org/de/blog/announcements/v18-release-announce/#new-globally-available-browser-compatible-apis). We have two options:
185
+
186
+
1. Inject the `getAll()` function to the global fetch API.
187
+
2. Use the [`node-fetch`](https://github.com/node-fetch/node-fetch) package to polyfill the fetch API.
188
+
189
+
We decided to go with option 1. It does not require addition dependency. And it is likely that Vercel removes the usage of the `getAll()` function down the road.
190
+
180
191
## Example
181
192
182
193
In the `example` folder, you can find a Next.js feature test app. Here's a link deployed using SST's [`NextjsSite`](https://docs.sst.dev/constructs/NextjsSite) construct. It contains a handful of pages. Each page aims to test a single Next.js feature.
// WORKAROUND (AWS): pass middleware headers to server
47
+
// WORKAROUND: Pass headers from middleware function to server function (AWS specific) — https://github.com/serverless-stack/open-next#workaround-pass-headers-from-middleware-function-to-server-function-aws-specific
// WORKAROUND (AWS): pass middleware headers to server
64
+
// WORKAROUND: Pass headers from middleware function to server function (AWS specific) — https://github.com/serverless-stack/open-next#workaround-pass-headers-from-middleware-function-to-server-function-aws-specific
// WORKAROUND: `NextServer` does not set cache response headers for HTML pages
73
+
// WORKAROUND: `NextServer` does not set cache response headers for HTML pages — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-cache-response-headers-for-html-pages
// WORKAROUND (AWS): pass middleware headers to server
78
+
// WORKAROUND: Pass headers from middleware function to server function (AWS specific) — https://github.com/serverless-stack/open-next#workaround-pass-headers-from-middleware-function-to-server-function-aws-specific
@@ -198,6 +205,30 @@ function createMiddlewareBundle(buildOutput: any) {
198
205
write: true,
199
206
allowOverwrite: true,
200
207
outfile: path.join(outputPath,"index.mjs"),
208
+
banner: {
209
+
js: [
210
+
// WORKAROUND: Add `Headers.getAll()` extension to the middleware function — https://github.com/serverless-stack/open-next#workaround-add-headersgetall-extension-to-the-middleware-function
211
+
"class Response extends globalThis.Response {",
212
+
" constructor(body, init) {",
213
+
" super(body, init);",
214
+
" this.headers.getAll = (name) => {",
215
+
" name = name.toLowerCase();",
216
+
" if (name !== 'set-cookie') {",
217
+
" throw new Error('Headers.getAll is only supported for Set-Cookie');",
0 commit comments