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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,15 @@ public, max-age=0, s-maxage=31536000, must-revalidate
`NextServer` does not seem to set an appropriate value for the `stale-while-revalidate` cache header. For example, the header might look like this:

```
s-maxage=600, stale-while-revalidate
s-maxage=600 stale-while-revalidate
```

This prevents CloudFront from caching the stale data.

To work around the issue, the server function checks if the response includes the `stale-while-revalidate` header. If found, it sets the value to 30 days:

```
s-maxage=600, stale-while-revalidate=2592000
s-maxage=600 stale-while-revalidate=2592000
```

#### WORKAROUND: Set `NextServer` working directory (AWS specific)
Expand Down
1 change: 0 additions & 1 deletion packages/open-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@aws-sdk/client-sqs": "^3.398.0",
"@node-minify/core": "^8.0.6",
"@node-minify/terser": "^8.0.6",
"@open-next/utils": "workspace:*",
"@tsconfig/node18": "^1.0.1",
"esbuild": "^0.18.18",
"@esbuild-plugins/node-resolve": "0.2.2",
Expand Down
3 changes: 1 addition & 2 deletions packages/open-next/src/adapters/plugins/routing/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
revalidateIfRequired,
} from "./util";
import { convertRes } from "../../routing/util";

//#endOverride

//#override processInternalEvent
Expand All @@ -22,7 +21,7 @@ export async function processInternalEvent(
const reqProps = {
method: internalEvent.method,
url: internalEvent.url,
// WORKAROUND: We pass this header to the serverless function to mimic a prefetch request which will not trigger revalidation since we handle revalidation differently
//WORKAROUND: We pass this header to the serverless function to mimic a prefetch request which will not trigger revalidation since we handle revalidation differently
// There is 3 way we can handle revalidation:
// 1. We could just let the revalidation go as normal, but due to race condtions the revalidation will be unreliable
// 2. We could alter the lastModified time of our cache to make next believe that the cache is fresh, but this could cause issues with stale data since the cdn will cache the stale data as if it was fresh
Expand Down
20 changes: 10 additions & 10 deletions packages/open-next/src/adapters/plugins/routing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ export function fixCacheHeaderForHtmlPages(
}
}

export function fixSWRCacheHeader(headers: Record<string, string | undefined>) {
// WORKAROUND: `NextServer` does not set correct SWR cache headers — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-correct-swr-cache-headers
if (headers["cache-control"]?.includes("stale-while-revalidate")) {
headers["cache-control"] = headers["cache-control"].replace(
"stale-while-revalidate",
"stale-while-revalidate=2592000", // 30 days
);
}
}

export function addOpenNextHeader(headers: Record<string, string | undefined>) {
headers["X-OpenNext"] = process.env.OPEN_NEXT_VERSION;
}
Expand Down Expand Up @@ -111,13 +121,3 @@ export async function revalidateIfRequired(
debug(e);
}
}

export function fixSWRCacheHeader(headers: Record<string, string | undefined>) {
// WORKAROUND: `NextServer` does not set correct SWR cache headers — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-correct-swr-cache-headers
if (headers["cache-control"]) {
headers["cache-control"] = headers["cache-control"].replace(
/\bstale-while-revalidate(?!=)/,
"stale-while-revalidate=2592000", // 30 days
);
}
}
1 change: 0 additions & 1 deletion packages/tests-unit/.gitignore

This file was deleted.

38 changes: 0 additions & 38 deletions packages/tests-unit/tests/utils.test.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,3 @@ export async function wait(n: number = 1000) {
setTimeout(res, n);
});
}

export function fixSWRCacheHeader(headers: Record<string, string | undefined>) {
// WORKAROUND: `NextServer` does not set correct SWR cache headers — https://github.com/serverless-stack/open-next#workaround-nextserver-does-not-set-correct-swr-cache-headers
if (headers["cache-control"]) {
headers["cache-control"] = headers["cache-control"].replace(
/\bstale-while-revalidate(?!=)/,
"stale-while-revalidate=2592000", // 30 days
);
}
}
Loading