Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pages/aws/v2.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import { Callout } from "nextra/components";

### Latest Releases

- OpenNext [2.3.10](https://www.npmjs.com/package/open-next/v/2.3.10)
- OpenNext [2.3.9](https://github.com/sst/open-next/releases/tag/v2.3.9)
16 changes: 8 additions & 8 deletions pages/aws/v2/advanced/architecture.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ This ensures that the Lambda handler remains at `index.mjs`.

Create a CloudFront distribution, and dispatch requests to their corresponding handlers (behaviors). The following behaviors are configured:

| Behavior | Requests | CloudFront Function | Origin |
| --------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | ----------------------------------------------------------------------------------------------------- | --------------------------- |
| `/_next/static/*` | Hashed static files | - | S3 bucket |
| `/favicon.ico`<br />`/my-images/*`<br />[see why](workaround#workaround-public-static-files-served-out-by-server-function-aws-specific) | public assets | - | S3 bucket |
| `/_next/image` | Image optimization | - | image optimization function |
| `/_next/data/*` | data requests | set `x-forwarded-host`<br />[see why](workaround#workaround-set-x-forwarded-host-header-aws-specific) | server function |
| `/api/*` | API | set `x-forwarded-host`<br />[see why](workaround#workaround-set-x-forwarded-host-header-aws-specific) | server function |
| `/*` | catch all | set `x-forwarded-host`<br />[see why](workaround#workaround-set-x-forwarded-host-header-aws-specific) | server function |
| Behavior | Requests | CloudFront Function | Origin |
| --------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
| `/_next/static/*` | Hashed static files | - | S3 bucket |
| `/favicon.ico`<br />`/my-images/*`<br />[see why](workaround#workaround-public-static-files-served-out-by-server-function-aws-specific) | public assets | - | S3 bucket |
| `/_next/image` | Image optimization | - | image optimization function |
| `/_next/data/*` | data requests | set `x-forwarded-host`<br />[see why](workaround#workaround-set-x-forwarded-host-header-aws-specific) | server function |
| `/api/*` | API | set `x-forwarded-host`<br />[see why](workaround#workaround-set-x-forwarded-host-header-aws-specific) | server function |
| `/*` | catch all | set `x-forwarded-host`<br />[see why](workaround#workaround-set-x-forwarded-host-header-aws-specific). Also [see this](workaround#workaround-nextserver-does-not-set-cache-headers-for-html-pages) | server function |

#### Running at edge

Expand Down
5 changes: 4 additions & 1 deletion pages/aws/v2/advanced/workaround.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ export function middleware(request: NextRequest) {

As mentioned in the [Server function](./architecture#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.

To work around the issue, the server function checks if the request is for an HTML page, and sets the `Cache Control` header to:
To work around the issue, the server function checks if the request is for a fully static HTML page from page router (i.e. without `getStaticProps`), and sets the `Cache Control` header to:

```
public, max-age=0, s-maxage=31536000, must-revalidate
```

If you plan on using fully static HTML pages, you should also add the `x-middleware-prefetch` header to cloudfront cache headers to avoid cloudfront caching an empty response when this header is set.
You can also just add an empty `getStaticProps` function to your page, which will set the correct cache headers.

#### WORKAROUND: `NextServer` does not set correct SWR cache headers

`NextServer` does not seem to set an appropriate value for the `stale-while-revalidate` cache header. For example, the header might look like this:
Expand Down