8000 Fix remove prefetch header for Next 14.1+ (#825) · opennextjs/opennextjs-aws@5f5d8a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f5d8a6

Browse files
conico974vicb
andauthored
Fix remove prefetch header for Next 14.1+ (#825)
* patch to disable background revalidation * remove prefetch header from request * changeset * lint * Update packages/tests-unit/tests/build/patch/patches/patchBackgroundRevalidation.test.ts Co-authored-by: Victor Berchet <victor@suumit.com> --------- Co-authored-by: Victor Berchet <victor@suumit.com>
1 parent 26cdc59 commit 5f5d8a6

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

.changeset/eleven-phones-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
remove prefetch header for next 14.1+

packages/open-next/src/build/createServerBundle.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
patchNextServer,
2929
patchUnstableCacheForISR,
3030
} from "./patch/patches/index.js";
31+
import { patchBackgroundRevalidation } from "./patch/patches/patchBackgroundRevalidation.js";
3132

3233
interface CodeCustomization {
3334
// These patches are meant to apply on user and next generated code
@@ -204,6 +205,7 @@ async function generateBundle(
204205
patchUnstableCacheForISR,
205206
patchNextServer,
206207
patchEnvVars,
208+
patchBackgroundRevalidation,
207209
...additionalCodePatches,
208210
]);
209211

@@ -251,6 +253,7 @@ async function generateBundle(
251253
...(disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : []),
252254
...(disableRouting ? ["withRouting"] : []),
253255
...(isAfter142 ? ["patchAsyncStorage"] : []),
256+
...(isAfter141 ? ["appendPrefetch"] : []),
254257
],
255258
}),
256259
openNextReplacementPlugin({
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getCrossPlatformPathRegex } from "utils/regex.js";
2+
import { createPatchCode } from "../astCodePatcher.js";
3+
import type { CodePatcher } from "../codePatcher.js";
4+
5+
export const rule = `
6+
rule:
7+
kind: binary_expression
8+
all:
9+
- has:
10+
kind: unary_expression
11+
regex: "!cachedResponse.isStale"
12+
- has:
13+
kind: member_expression
14+
regex: "context.isPrefetch"
15+
fix:
16+
'true'`;
17+
18+
export const patchBackgroundRevalidation = {
19+
name: "patchBackgroundRevalidation",
20+
patches: [
21+
{
22+
// TODO: test for earlier versions of Next
23+
versions: ">=14.1.0",
24+
field: {
25+
pathFilter: getCrossPlatformPathRegex("server/response-cache/index.js"),
26+
patchCode: createPatchCode(rule),
27+
},
28+
},
29+
],
30+
} satisfies CodePatcher;

packages/open-next/src/core/requestHandler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ export async function openNextHandler(
161161
// 1. We could just let the revalidation go as normal, but due to race conditions the revalidation will be unreliable
162162
// 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
163163
// 3. OUR CHOICE: We could pass a purpose prefetch header to the serverless function to make next believe that the request is a prefetch request and not trigger revalidation (This could potentially break in the future if next changes the behavior of prefetch requests)
164-
headers: { ...headers, purpose: "prefetch" },
164+
headers: {
165+
...headers,
166+
//#override appendPrefetch
167+
purpose: "prefetch",
168+
//#endOverride
169+
},
165170
body: preprocessedEvent.body,
166171
remoteAddress: preprocessedEvent.remoteAddress,
167172
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
2+
import { rule } from "@opennextjs/aws/build/patch/patches/patchBackgroundRevalidation.js";
3+
import { describe, it } from "vitest";
4+
5+
const codeToPatch = `if (cachedResponse && !isOnDemandRevalidate) {
6+
var _cachedResponse_value;
7+
if (((_cachedResponse_value = cachedResponse.value) == null ? void 0 : _cachedResponse_value.kind) === _types.CachedRouteKind.FETCH) {
8+
throw new Error(\`invariant: unexpected cachedResponse of kind fetch in response cache\`);
9+
}
10+
resolve({
11+
...cachedResponse,
12+
revalidate: cachedResponse.curRevalidate
13+
});
14+
resolved = true;
15+
if (!cachedResponse.isStale || context.isPrefetch) {
16+
// The cached value is still valid, so we don't need
17+
// to update it yet.
18+
return null;
19+
}
20+
}`;
21+
22+
describe("patchBackgroundRevalidation", () => {
23+
it("Should patch code", () => {
24+
expect(
25+
patchCode(codeToPatch, rule),
26+
).toMatchInlineSnapshot(`"if (cachedResponse && !isOnDemandRevalidate) {
27+
var _cachedResponse_value;
28+
if (((_cachedResponse_value = cachedResponse.value) == null ? void 0 : _cachedResponse_value.kind) === _types.CachedRouteKind.FETCH) {
29+
throw new Error(\`invariant: unexpected cachedResponse of kind fetch in response cache\`);
30+
}
31+
resolve({
32+
...cachedResponse,
33+
revalidate: cachedResponse.curRevalidate
34+
});
35+
resolved = true;
36+
if (true) {
37+
// The cached value is still valid, so we don't need
38+
// to update it yet.
39+
return null;
40+
}
41+
}"`);
42+
});
43+
});

0 commit comments

Comments
 (0)
0