Skip to content

Commit 87a2e13

Browse files
authored
update docs for AWS (#224)
1 parent c50ac62 commit 87a2e13

16 files changed

Lines changed: 197 additions & 31 deletions

‎pages/aws/common_issues.mdx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ This will output **A LOT** of additional logs to the console.This also disable m
88
OPEN_NEXT_DEBUG=true npx open-next@latest build
99
```
1010

11+
A few other environment variables can help debugging at runtime:
12+
13+
- `OPEN_NEXT_REQUEST_ID_HEADER` - When set, the `x-opennext-requestid` header is added to the responses (it is always added in debug mode). Available since `@opennextjs/aws` 3.7.0.
14+
- `OPEN_NEXT_ERROR_LOG_LEVEL` - The minimum level of the internal OpenNext errors that are logged: `0` (or `debug`), `1` (or `warn`, the default) or `2` (or `error`).
15+
1116
#### Cannot find module next
1217

1318
You might stumble upon this error inside cloudwatch logs: `Cannot find module 'next'`. It is likely that you are in a monorepo and you have several lock files. **Just make sure that you have a single lock file in the root of your project.**
@@ -32,6 +37,9 @@ You should also not add sharp as a dependency unless absolutely necessary, the i
3237
- `next/dist/compiled/babel-packages`
3338
- `next/dist/compiled/amphtml-validator`
3439

40+
Since `@opennextjs/aws` 3.10.0, OpenNext also excludes the platform specific native binaries for non-Linux platforms (i.e. `@swc/core-darwin-arm64`) that are traced when building on macOS or Windows.
41+
You can set the `OPEN_NEXT_SKIP_PLATFORM_FILTER` environment variable to `true` at build time to disable this filter.
42+
3543
##### Remove sourcemaps
3644

3745
Source maps could add a lot to the bundle, excluding them from standalone output could be worth it.

‎pages/aws/config.mdx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
### Build Arguments
22

3-
There is two build arguments that you can pass to the `open-next build` command:
3+
These are the build arguments that you can pass to the `open-next build` command:
44

55
- `--config-path` - This is the path to the configuration file that you want to use. By default, it will look for `open-next.config.ts` in the current working directory. This needs to be relative to the current working directory.
66
- `--node-externals` - You can configure externals for the esbuild compilation of the `open-next.config.ts` file (i.e `--node-externals @aws-sdk/*,open-next/dist/queue/*`)
7+
- `--dangerously-use-unsupported-next-version` - Since `@opennextjs/aws` 3.9.13, the build fails when the Next.js version is no longer supported by the Next.js team (see the [Next.js support policy](https://nextjs.org/support-policy)) as it may contain unpatched security vulnerabilities. This flag allows building anyway.
78

89
### Configuration File
910

‎pages/aws/config/custom_overrides.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ const config = {
312312
export default config;
313313
```
314314

315-
[Included](https://github.com/opennextjs/opennextjs-aws/tree/main/packages/open-next/src/overrides/tagCache) ones are `'dynamodb' | 'dynamodb-lite' | 'fs-dev' | 'dummy'`
315+
[Included](https://github.com/opennextjs/opennextjs-aws/tree/main/packages/open-next/src/overrides/tagCache) ones are `'dynamodb' | 'dynamodb-lite' | 'dynamodb-nextMode' | 'fs-dev' | 'fs-dev-nextMode' | 'dummy'`
316316

317317
## Custom Origin Resolver
318318

@@ -418,7 +418,7 @@ import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
418418
const config = {
419419
default: {},
420420
middleware: {
421-
assetResolver: import("./customAssetResolver").then((mod) => mod.default),
421+
assetResolver: () => import("./customAssetResolver").then((mod) => mod.default),
422422
},
423423
} satisfies OpenNextConfig;
424424

‎pages/aws/config/overrides/automatic_cdn_invalidation.mdx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ It will be called for `revalidatePath`, `revalidateTag` and `res.revalidate()`.
77

88
If you want to better understand how to implement your own Automatic CDN Invalidation, the easiest way would be to take a look at the existing [included Automatic CDN Invalidation](https://github.com/opennextjs/opennextjs-aws/tree/main/packages/open-next/src/overrides/cdnInvalidation)
99

10+
## Custom implementations
11+
12+
An Automatic CDN Invalidation override must implement `invalidatePaths` and can optionally implement `invalidateTags`:
13+
14+
```ts
15+
type CDNInvalidationHandler = {
16+
name: string;
17+
invalidatePaths: (paths: CDNPath[]) => Promise<void>;
18+
invalidateTags?: (tags: string[]) => Promise<void>;
19+
};
20+
```
21+
22+
### `invalidatePaths`
23+
24+
Invalidates the CDN entries of the given paths. Each path includes the path before (`initialPath`) and after (`rawPath`) routing as well as the routes that matched while resolving the request (`resolvedRoutes`).
25+
26+
For `revalidateTag` and `revalidatePath`, the paths are resolved from the tags by the [Tag Cache](/aws/config/overrides/tag_cache). Tag caches in `nextMode` can only resolve paths when they implement the optional `getPathsByTags` method.
27+
28+
### `invalidateTags`
29+
30+
Available since `@opennextjs/aws` 4.1.14
31+
32+
If your CDN supports invalidating its cache by tags, you can implement `invalidateTags` to invalidate the original cache tags directly instead of resolving them to paths.
33+
34+
- It is called once per `revalidateTag` or `revalidatePath` call, after the tag cache has been updated, with the tags as passed to Next.js: hard tags from `revalidateTag()` and soft tags (prefixed with `_N_T_/`) from `revalidatePath()`. Tags are not expanded, i.e. the hard tags of the paths matching a soft tag are not added.
35+
- When it is provided, `invalidatePaths` is no longer called for `revalidateTag` and `revalidatePath`. It is still called for `res.revalidate()` in the Pages Router.
36+
- The CDN integration must associate cached entries with the same tags, both hard and soft, for the invalidation to have an effect.
37+
1038
## Included Automatic CDN Invalidation
1139

1240
### dummy

‎pages/aws/config/overrides/converter.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The `aws-cloudfront` Converter is a converter for Cloudfront (i.e. lambda@edge)
2727

2828
The `edge` Converter is the converter for everything that uses `Request` and `Response`. At the moment with the builtin one that's only Cloudflare.
2929

30-
### converter
30+
### node
3131

3232
The `node` Converter is a converter for classic Node.js server.
3333

‎pages/aws/config/overrides/incremental_cache.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ If you want to better understand how to implement your own IncrementalCache, the
88

99
One thing to note is that it is not used at build time, only at runtime. This means that you'll have to upload the cache yourself if you want to use the prebuilt routes/pages (And this is mandatory for ISR/SSG routes with `fallback:false`).
1010
All the cache files are present in the `.open-next/cache` folder. The one under `BUILD_ID` are for the ISR/SSG cache and the one under `__fetch/BUILD_ID` are for the fetch cache.
11+
`BUILD_ID` is the OpenNext build id (`process.env.OPEN_NEXT_BUILD_ID` at runtime): the Next.js [`deploymentId`](https://nextjs.org/docs/app/api-reference/config/next-config-js/deploymentId) when set and the Next.js build id otherwise.
12+
This applies to every supported Next.js version. It matters most on Next.js 16.2+, where the Next.js build id is the same for every deployment when `deploymentId` is set.
1113

1214
## Included IncrementalCache
1315

‎pages/aws/config/overrides/queue.mdx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Callout } from "nextra/components";
22

33
This override is used by OpenNext to trigger the revalidation of stale routes.
4-
Before sending the response to the client, OpenNext will check if the route is stale and if it is, it will call the queue override to revalidate the route.
4+
When a stale route is served, OpenNext calls the `send` method of the queue override to revalidate the route:
5+
6+
- When the route is rendered by the Next server, `send` is called once the response is sent (in `waitUntil` when the wrapper provides one).
7+
- With [cache interception](/aws/inner_workings/cache_interception), `send` is awaited before the stale response is returned. If it throws, the error is logged and the request falls back to the Next server.
58

69
If you want to better understand how to implement your own Queue, the easiest way would be to take a look at the existing [included Queue](https://github.com/opennextjs/opennextjs-aws/tree/main/packages/open-next/src/overrides/queue).
710

811
Couple of things to note :
912

1013
- The default implementation use an SQS queue. This has the main advantage of being able to control the concurrency of the revalidations as well as avoiding trigerring the revalidation multiple times for the same route.
14+
- The promise returned by `send` should resolve once the message is accepted (or handed to `waitUntil`), not once the route is revalidated, so that the response is not delayed.
1115
- You don't have to use a queue at all. You could trigger the revalidation directly in the Queue override itself. You can see a very simple implementation of this [in the `direct` override](https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/overrides/queue/direct.ts).
1216

1317
## Included Queue
@@ -17,6 +21,8 @@ Couple of things to note :
1721
The SQS Queue will send a message to an SQS queue for each route that needs to be revalidated. It is used by default if you don't provide any Queue in your configuration.
1822
It uses the `@aws-sdk/client-sqs` to interact with SQS.
1923

24+
Since `@opennextjs/aws` 4.1.6, the message is sent in the background when the wrapper provides a `waitUntil` (i.e. `aws-lambda-streaming` or `aws-apigw-streaming`) so that it does not delay the response. Otherwise, the response is sent once the message has been sent. The same applies to `sqs-lite`.
25+
2026
#### Requirements
2127

2228
- You need to provide the `REVALIDATION_QUEUE_URL` environment variable to your server.
@@ -35,7 +41,9 @@ This implementation is a lighter version of the `sqs` Queue as it uses `aws4fetc
3541
The Direct Queue will directly trigger the revalidation of the route in the Queue override itself. It is useful for development purposes and when you don't want to use a queue.
3642

3743
<Callout type="warning">
38-
Be careful with this implementation as it could lead to multiple revalidations of the same route.
44+
Be careful with this implementation as it could lead to multiple revalidations of the same route. As `send`
45+
resolves once the route is revalidated, it also delays the stale response when cache interception is
46+
enabled.
3947
</Callout>
4048

4149
### dummy

‎pages/aws/config/overrides/tag_cache.mdx‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Callout } from "nextra/components";
2+
13
This override is used by the [`cache` adapter](https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/adapters/cache.ts) that is provided by OpenNext to the NextServer. It is also used by OpenNext if `enableCacheInterception` is set to `true` in the configuration and by the `initializationFunction` for prepopulating the cache.
24

35
It is used for the cache tags (i.e. `revalidateTag` and `revalidatePath`). It does not handle anything related to the ISR/SSG cache or the fetch cache used by Next.js.
@@ -7,9 +9,33 @@ The current implementation differs from the way Next.js standalone handle cache
79
We choose to go another way. We store tags for every path that exists in the cache. This way, we can easily know if a path is stale by just checking the path instead of every tags used. This approach is more write heavy but it's a tradeoff we are willing to make.
810
This also has the drawback of needing to prepopulate the cache with all the tags that are used in the application. This is done by the `initializationFunction`.
911

10-
We might allow to choose how cache tags are handled in the future depending on the `TagCache` implementation. If you need this feature, feel free to open an issue on the [GitHub repository](https://github.com/opennextjs/opennextjs-aws).
12+
## Modes
13+
14+
A `TagCache` can work in one of two modes, set with its `mode` property:
15+
16+
- `original` (default) - The approach described above. Tags are stored for every path, so a single lookup per path is needed on read. It needs to be prepopulated at build time and is write heavy. It allows the [automatic CDN invalidation](/aws/config/overrides/automatic_cdn_invalidation) to resolve the paths of a tag.
17+
- `nextMode` - Similar to the way Next.js handles tags. Only the time at which a tag has been revalidated is stored, and every tag of an entry is checked on read. It doesn't need to be prepopulated and writes very little, but reads more. Path resolution for the automatic CDN invalidation is only available when the implementation provides the optional `getPathsByTags` method.
18+
19+
## Custom implementations
20+
21+
If you want to better understand how to implement your own TagCache, the easiest way would be to take a look at the existing [included TagCache](https://github.com/opennextjs/opennextjs-aws/tree/main/packages/open-next/src/overrides/tagCache) and at the [types](https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/types/overrides.ts).
22+
23+
### Stale-while-revalidate
24+
25+
Since `@opennextjs/aws` 3.10.0, `revalidateTag(tag, profile)` (Next.js 16+) can mark the entries as stale instead of expiring them right away. Stale entries are still served while they are revalidated in the background.
26+
27+
To support it, a `TagCache` must store the optional `stale` and `expire` timestamps (in milliseconds since epoch) passed to `writeTags` and implement the optional `isStale` method:
28+
29+
- `original` mode: `isStale(path: string, lastModified?: number): Promise<boolean>`
30+
- `nextMode`: `isStale(tags: string[], lastModified?: number): Promise<boolean>`
1131

12-
If you want to better understand how to implement your own TagCache, the easiest way would be to take a look at the existing [included TagCache](https://github.com/opennextjs/opennextjs-aws/tree/main/packages/open-next/src/overrides/tagCache).
32+
Without `isStale`, stale entries are not detected, only expired ones are.
33+
34+
<Callout type="warning">
35+
The signature of `writeTags` changed in `@opennextjs/aws` 3.10.0. It now receives `OriginalTagCacheWriteInput[]`
36+
(`{ tag, path, revalidatedAt?, stale?, expire? }`) in the `original` mode and `NextModeTagCacheWriteInput[]`
37+
(`string | { tag, stale?, expire? }`) in `nextMode`. Custom implementations must be updated.
38+
</Callout>
1339

1440
## Included TagCache
1541

@@ -35,7 +61,13 @@ This implementation is a lighter version of the `dynamodb` TagCache as it uses `
3561

3662
### dynamodb-nextMode
3763

38-
TBA
64+
The DynamoDBNextMode TagCache will store the time at which each tag has been revalidated in a DynamoDB table. It works in `nextMode` and uses `aws4fetch` to interact with DynamoDB.
65+
It doesn't need to be prepopulated by the `initializationFunction`, and it doesn't support resolving the paths of a tag for the automatic CDN invalidation.
66+
67+
##### Requirements
68+
69+
- You need to provide the `CACHE_DYNAMO_TABLE`, `CACHE_BUCKET_REGION`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_SESSION_TOKEN` environment variables to your server.
70+
- DynamoDB table should have a primary key `path` of type `String` and a sort key `tag` of type `String`. (It can reuse the same table as the one used by the default `tagCache`)
3971

4072
### dummy
4173

@@ -44,3 +76,7 @@ The Dummy TagCache is a dummy implementation that will throw an exception. It sh
4476
### fs-dev
4577

4678
The FsDev TagCache is a simple implementation that stores the cache tags in a file in the `.open-next/cache` folder and interact with it using the filesystem. It is useful for development purposes only.
79+
80+
### fs-dev-nextMode
81+
82+
The FsDevNextMode TagCache is a simple implementation that works in `nextMode` and stores the cache tags in memory. It is useful for development purposes only.

‎pages/aws/config/overrides/wrapper.mdx‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Couple of things to note :
77
- If you want to use streaming (for the `node` runtime of next), you'll need to create a `StreamCreator` and pass it to the `handler`. You can find some example in the [node](https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/overrides/wrappers/node.ts) or [aws-lambda-streaming](https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/overrides/wrappers/aws-lambda-streaming.ts) wrapper.
88
- If you don't use streaming (like in the default `aws-lambda` wrapper), you may still need to provide a fake `StreamCreator` to the `handler` to avoid a weird issue with Node itself (see [here](https://github.com/opennextjs/opennextjs-aws/blob/f685ddea8f8a5c82591dc02713aff7138f2d9896/packages/open-next/src/overrides/wrappers/aws-lambda.ts#L49-L65) for an example and a more thorough explanation).
99
- If you use the `edge` runtime of Next (either for the external middleware or for an `edge` route or page), you don't need the `StreamCreator` at all.
10-
- If you are in a serverless environment and it supports `waitUntil`, you should pass it to the handler as it is done in the [cloudflare-edge wrapper](https://github.com/opennextjs/opennextjs-aws/blob/87b59e59a8a0bf1e83d2d1df9efa0ad3bfc912f5/packages/open-next/src/overrides/wrappers/cloudflare-edge.ts#L65-L67). This might not be necessary depending on where you run it (for example the `aws-lambda-streaming` or the `node` wrapper doesn't need it.)
10+
- If you are in a serverless environment and it supports `waitUntil`, you should pass it to the handler as it is done in the [cloudflare-edge wrapper](https://github.com/opennextjs/opennextjs-aws/blob/87b59e59a8a0bf1e83d2d1df9efa0ad3bfc912f5/packages/open-next/src/overrides/wrappers/cloudflare-edge.ts#L65-L67). The `aws-lambda-streaming` and `aws-apigw-streaming` wrappers provide their own `waitUntil` (pending promises are awaited before the response stream is closed). This might not be necessary depending on where you run it (for example the `node` wrapper doesn't need it as it is a long running server.)
1111

1212
## Included Wrappers
1313

@@ -22,12 +22,48 @@ The `aws-lambda` Wrapper is the default wrapper for AWS Lambda. It is used by de
2222

2323
### aws-lambda-streaming
2424

25-
The `aws-lambda-streaming` Wrapper is a wrapper that allows you to use streaming in AWS Lambda. Streaming must be enabled for this lambda.
25+
The `aws-lambda-streaming` Wrapper is a wrapper that allows you to use streaming in AWS Lambda. Streaming must be enabled for this lambda and it must be invoked through a Lambda Function URL.
26+
27+
The response is compressed (`br`, `gzip` or `deflate`) according to the `Accept-Encoding` header of the request.
28+
29+
#### Features
30+
31+
- [x] Streaming
32+
- [x] Proper support for `waitUntil` (available since `@opennextjs/aws` 4.1.6)
33+
- [x] Compression
34+
35+
### aws-apigw-streaming
36+
37+
Available since `@opennextjs/aws` 4.1.0
38+
39+
The `aws-apigw-streaming` Wrapper allows you to use streaming when the server function is behind an [API Gateway REST API with response streaming](https://aws.amazon.com/about-aws/whats-new/2025/11/api-gateway-response-streaming-rest-apis/) (`ResponseTransferMode: STREAM`) instead of a Lambda Function URL.
40+
41+
It should be used with the `aws-apigw-v1` (REST API) or `aws-apigw-v2` (HTTP API) converter.
42+
43+
Unlike `aws-lambda-streaming`, it never compresses the response because API Gateway streaming does not support content encoding. Compression should be handled by the CDN in front of API Gateway (e.g. CloudFront).
2644

2745
#### Features
2846

2947
- [x] Streaming
3048
- [x] Proper support for `waitUntil`
49+
- [ ] Compression
50+
51+
### aws-lambda-compressed
52+
53+
Available since `@opennextjs/aws` 3.5.6
54+
55+
The `aws-lambda-compressed` Wrapper is the same as the `aws-lambda` Wrapper but it also compresses the response (`br`, `gzip` or `deflate`) according to the `Accept-Encoding` header of the request.
56+
It can be useful to reduce the size of the response as AWS Lambda has a limit of 6MB for the response payload.
57+
58+
#### Features
59+
60+
- [ ] Streaming
61+
- [ ] Proper support for `waitUntil`
62+
- [x] Compression
63+
64+
### Compression
65+
66+
The Brotli compression quality used by the `aws-lambda-streaming` and `aws-lambda-compressed` wrappers can be set with the `BROTLI_QUALITY` environment variable (from `0` to `11`). It defaults to `6`.
3167

3268
### cloudflare-edge
3369

0 commit comments

Comments
 (0)