Skip to content

Commit 2301413

Browse files
authored
server-adapter: prevent to replace correct swr header (#162)
* prevent to replace correct swr header
1 parent fdbeb4d commit 2301413

File tree

8 files changed

+263
-193
lines changed

8 files changed

+263
-193
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,15 +509,15 @@ public, max-age=0, s-maxage=31536000, must-revalidate
509509
`NextServer` does not seem to set an appropriate value for the `stale-while-revalidate` cache header. For example, the header might look like this:
510510

511511
```
512-
s-maxage=600 stale-while-revalidate
512+
s-maxage=600, stale-while-revalidate
513513
```
514514

515515
This prevents CloudFront from caching the stale data.
516516

517517
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:
518518

519519
```
520-
s-maxage=600 stale-while-revalidate=2592000
520+
s-maxage=600, stale-while-revalidate=2592000
521521
```
522522

523523
#### WORKAROUND: Set `NextServer` working directory (AWS specific)

packages/open-next/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@aws-sdk/client-sqs": "^3.398.0",
4040
"@node-minify/core": "^8.0.6",
4141
"@node-minify/terser": "^8.0.6",
42+
"@open-next/utils": "workspace:*",
4243
"@tsconfig/node18": "^1.0.1",
4344
"esbuild": "^0.18.18",
4445
"@esbuild-plugins/node-resolve": "0.2.2",

packages/open-next/src/adapters/plugins/routing/default.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
revalidateIfRequired,
1313
} from "./util";
1414
import { convertRes } from "../../routing/util";
15+
1516
//#endOverride
1617

1718
//#override processInternalEvent
@@ -21,7 +22,7 @@ export async function processInternalEvent(
2122
const reqProps = {
2223
method: internalEvent.method,
2324
url: internalEvent.url,
24-
//WORKAROUND: We pass this header to the serverless function to mimic a prefetch request which will not trigger revalidation since we handle revalidation differently
25+
// WORKAROUND: We pass this header to the serverless function to mimic a prefetch request which will not trigger revalidation since we handle revalidation differently
2526
// There is 3 way we can handle revalidation:
2627
// 1. We could just let the revalidation go as normal, but due to race condtions the revalidation will be unreliable
2728
// 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

packages/open-next/src/adapters/plugins/routing/util.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ export function fixCacheHeaderForHtmlPages(
5454
}
5555
}
5656

57-
export function fixSWRCacheHeader(headers: Record<string, string | undefined>) {
58-
// 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
59-
if (headers["cache-control"]?.includes("stale-while-revalidate")) {
60-
headers["cache-control"] = headers["cache-control"].replace(
61-
"stale-while-revalidate",
62-
"stale-while-revalidate=2592000", // 30 days
63-
);
64-
}
65-
}
66-
6757
export function addOpenNextHeader(headers: Record<string, string | undefined>) {
6858
headers["X-OpenNext"] = process.env.OPEN_NEXT_VERSION;
6959
}
@@ -121,3 +111,13 @@ export async function revalidateIfRequired(
121111
debug(e);
122112
}
123113
}
114+
115+
export function fixSWRCacheHeader(headers: Record<string, string | undefined>) {
116+
// 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
117+
if (headers["cache-control"]) {
118+
headers["cache-control"] = headers["cache-control"].replace(
119+
/\bstale-while-revalidate(?!=)/,
120+
"stale-while-revalidate=2592000", // 30 days
121+
);
122+
}
123+
}

packages/tests-unit/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { fixSWRCacheHeader } from "@open-next/utils";
2+
3+
describe("Utils", () => {
4+
describe("fixSWRCacheHeader", () => {
5+
it("Does not update swr if already set", () => {
6+
const headers = {
7+
"cache-control": "maxage=69,stale-while-revalidate=11",
8+
};
9+
fixSWRCacheHeader(headers);
10+
11+
expect(headers).toEqual({
12+
"cache-control": "maxage=69,stale-while-revalidate=11",
13+
});
14+
});
15+
16+
it("Does not add swr if not exists", () => {
17+
const headers = {
18+
"cache-control": "maxage=69",
19+
};
20+
fixSWRCacheHeader(headers);
21+
22+
expect(headers).toEqual({
23+
"cache-control": "maxage=69",
24+
});
25+
});
26+
27+
it("Sets time on swf if not defined", () => {
28+
const headers = {
29+
"cache-control": "maxage=69,stale-while-revalidate",
30+
};
31+
fixSWRCacheHeader(headers);
32+
33+
expect(headers).toEqual({
34+
"cache-control": "maxage=69,stale-while-revalidate=2592000",
35+
});
36+
});
37+
});
38+
});

packages/utils/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ export async function wait(n: number = 1000) {
1414
setTimeout(res, n);
1515
});
1616
}
17+
18+
export function fixSWRCacheHeader(headers: Record<string, string | undefined>) {
19+
// 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
20+
if (headers["cache-control"]) {
21+
headers["cache-control"] = headers["cache-control"].replace(
22+
/\bstale-while-revalidate(?!=)/,
23+
"stale-while-revalidate=2592000", // 30 days
24+
);
25+
}
26+
}

0 commit comments

Comments
 (0)