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
|`/_next/image`| Image optimization | image optimization function |
195
-
|`/_next/data/*`| data requests | server function |
196
-
|`/api/*`| API | server function |
197
-
|`/*`| catch all | server function fallback to<br />S3 bucket on 503<br />[see why](#workaround-public-static-files-served-out-by-server-function-aws-specific)|
191
+
| Behavior | Requests |CloudFront Function |Origin |
|`/_next/image`| Image optimization |- |image optimization function |
195
+
|`/_next/data/*`| data requests |set `x-forwarded-host`<br />[see why](#workaround-set-x-forwarded-host-header-aws-specific)|server function |
196
+
|`/api/*`| API |set `x-forwarded-host`<br />[see why](#workaround-set-x-forwarded-host-header-aws-specific)|server function |
197
+
|`/*`| catch all |set `x-forwarded-host`<br />[see why](#workaround-set-x-forwarded-host-header-aws-specific)|server function fallback to<br />S3 bucket on 503<br />[see why](#workaround-public-static-files-served-out-by-server-function-aws-specific)|
198
198
199
199
#### Running at edge
200
200
201
201
The server function can also run at edge locations by configuring it as Lambda@Edge on Origin Request. The server function can accept both regional request events (API payload version 2.0) and edge request events (CloudFront Origin Request payload). Depending on the shape of the Lambda event object, the function will process the request accordingly.
|`/_next/data/*`| data requests |set `x-forwarded-host`<br />[see why](#workaround-set-x-forwarded-host-header-aws-specific)|server function | - |
210
+
|`/api/*`| API |set `x-forwarded-host`<br />[see why](#workaround-set-x-forwarded-host-header-aws-specific)|server function | - |
211
+
|`/*`| catch all |set `x-forwarded-host`<br />[see why](#workaround-set-x-forwarded-host-header-aws-specific)|server function | S3 bucket<br />[see why](#workaround-public-static-files-served-out-by-server-function-aws-specific)|
212
212
213
213
## Limitations and workarounds
214
214
@@ -231,6 +231,22 @@ During the build process, the top-level file and directory names in the `public/
231
231
232
232
This means that on cache miss, the request may take slightly longer to process.
233
233
234
+
#### WORKAROUND: Set `x-forwarded-host` header (AWS specific)
235
+
236
+
When the server function receives a request, the `host` value in the Lambda request header is set to the hostname of the AWS Lambda service instead of the actual frontend hostname. This creates an issue for the server function (middleware, SSR routes, or API routes) when it needs to know the frontend host.
237
+
238
+
To work around the issue, a CloudFront function is run on Viewer Request, which sets the frontend hostname as the `x-forwarded-host` header. The function code looks like this:
The server function would then sets the `host` header of the request to the value of the `x-forwarded-host` header when sending the request to the `NextServer`.
249
+
234
250
#### WORKAROUND: `NextServer` does not set cache response headers for HTML pages
235
251
236
252
As mentioned in the [Server function](#server-lambda-function) section, the server function uses the `NextServer` class from Next.js' build output to handle requests. However, `NextServer` does not seem to set the correct `Cache Control` headers.
// WORKAROUND: public/ static files served by the server function (AWS specific) — https://github.com/serverless-stack/open-next#workaround-public-static-files-served-by-the-server-function-aws-specific
0 commit comments