Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/eleven-phones-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

remove prefetch header for next 14.1+
3 changes: 3 additions & 0 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
patchNextServer,
patchUnstableCacheForISR,
} from "./patch/patches/index.js";
import { patchBackgroundRevalidation } from "./patch/patches/patchBackgroundRevalidation.js";

interface CodeCustomization {
// These patches are meant to apply on user and next generated code
Expand Down Expand Up @@ -204,6 +205,7 @@ async function generateBundle(
patchUnstableCacheForISR,
patchNextServer,
patchEnvVars,
patchBackgroundRevalidation,
...additionalCodePatches,
]);

Expand Down Expand Up @@ -251,6 +253,7 @@ async function generateBundle(
...(disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : []),
...(disableRouting ? ["withRouting"] : []),
...(isAfter142 ? ["patchAsyncStorage"] : []),
...(isAfter141 ? ["appendPrefetch"] : []),
],
}),
openNextReplacementPlugin({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getCrossPlatformPathRegex } from "utils/regex.js";
import { createPatchCode } from "../astCodePatcher.js";
import type { CodePatcher } from "../codePatcher.js";

export const rule = `
rule:
kind: binary_expression
all:
- has:
kind: unary_expression
regex: "!cachedResponse.isStale"
- has:
kind: member_expression
regex: "context.isPrefetch"
fix:
'true'`;

export const patchBackgroundRevalidation = {
name: "patchBackgroundRevalidation",
patches: [
{
// TODO: test for earlier versions of Next
versions: ">=14.1.0",
field: {
pathFilter: getCrossPlatformPathRegex("server/response-cache/index.js"),
patchCode: createPatchCode(rule),
},
},
],
} satisfies CodePatcher;
7 changes: 6 additions & 1 deletion packages/open-next/src/core/requestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ export async function openNextHandler(
// 1. We could just let the revalidation go as normal, but due to race conditions 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
// 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)
headers: { ...headers, purpose: "prefetch" },
headers: {
...headers,
//#override appendPrefetch
purpose: "prefetch",
//#endOverride
},
body: preprocessedEvent.body,
remoteAddress: preprocessedEvent.remoteAddress,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
import { rule } from "@opennextjs/aws/build/patch/patches/patchBackgroundRevalidation.js";
import { describe, it } from "vitest";

const codeToPatch = `if (cachedResponse && !isOnDemandRevalidate) {
var _cachedResponse_value;
if (((_cachedResponse_value = cachedResponse.value) == null ? void 0 : _cachedResponse_value.kind) === _types.CachedRouteKind.FETCH) {
throw new Error(\`invariant: unexpected cachedResponse of kind fetch in response cache\`);
}
resolve({
...cachedResponse,
revalidate: cachedResponse.curRevalidate
});
resolved = true;
if (!cachedResponse.isStale || context.isPrefetch) {
// The cached value is still valid, so we don't need
// to update it yet.
return null;
}
}`;

describe("patchBackgroundRevalidation", () => {
it("Should patch code for Next 14+", () => {
expect(
patchCode(codeToPatch, rule),
).toMatchInlineSnapshot(`"if (cachedResponse && !isOnDemandRevalidate) {
var _cachedResponse_value;
if (((_cachedResponse_value = cachedResponse.value) == null ? void 0 : _cachedResponse_value.kind) === _types.CachedRouteKind.FETCH) {
throw new Error(\`invariant: unexpected cachedResponse of kind fetch in response cache\`);
}
resolve({
...cachedResponse,
revalidate: cachedResponse.curRevalidate
});
resolved = true;
if (true) {
// The cached value is still valid, so we don't need
// to update it yet.
return null;
}
}"`);
});
});
Loading