Skip to content

Commit 010edc2

Browse files
authored
Revert "server-adapter: prevent to replace correct swr header (#162)" (#215)
This reverts commit 2301413.
1 parent 2301413 commit 010edc2

File tree

8 files changed

+193
-263
lines changed

8 files changed

+193
-263
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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
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:*",
4342
"@tsconfig/node18": "^1.0.1",
4443
"esbuild": "^0.18.18",
4544
"@esbuild-plugins/node-resolve": "0.2.2",

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

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

1817
//#override processInternalEvent
@@ -22,7 +21,7 @@ export async function processInternalEvent(
2221
const reqProps = {
2322
method: internalEvent.method,
2423
url: internalEvent.url,
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
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
2625
// There is 3 way we can handle revalidation:
2726
// 1. We could just let the revalidation go as normal, but due to race condtions the revalidation will be unreliable
2827
// 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,6 +54,16 @@ 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+
5767
export function addOpenNextHeader(headers: Record<string, string | undefined>) {
5868
headers["X-OpenNext"] = process.env.OPEN_NEXT_VERSION;
5969
}
@@ -111,13 +121,3 @@ export async function revalidateIfRequired(
111121
debug(e);
112122
}
113123
}
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: 0 additions & 1 deletion
This file was deleted.

packages/tests-unit/tests/utils.test.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/utils/src/index.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,3 @@ 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)