Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/vinext/src/server/app-rsc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ async function handleAppRscRequest<TRoute extends AppRscHandlerRoute>(
actionError: normalizedProgressiveActionError,
actionFailed,
handlerStart,
interceptionContext: interceptionContextHeader,
interceptionContext: isRscRequest ? interceptionContextHeader : null,
interceptionPathname: cleanPathnameIsRequestPathname ? requestCleanPathname : cleanPathname,
isProgressiveActionRender,
isRscRequest,
Expand Down
90 changes: 90 additions & 0 deletions tests/app-page-dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,49 @@ describe("app page dispatch", () => {
expect(cachePolicy.cacheControl.expire).toBeUndefined();
});

it("writes HTML-captured RSC data under the plain key when interception context is absent", async () => {
const isrSet = vi.fn<DispatchOptions["isrSet"]>(async () => {});
const waitUntilPromises: Promise<unknown>[] = [];
const executionContext = {
waitUntil(promise) {
waitUntilPromises.push(promise);
},
} satisfies ExecutionContextLike;
const { options } = createDispatchOptions({
cleanPathname: "/photos/123",
interceptionContext: null,
isProduction: true,
isrRscKey(pathname, mountedSlotsHeader, _renderMode, interceptionContext) {
return `rsc:${pathname}:${mountedSlotsHeader ?? "none"}:${interceptionContext ?? "none"}`;
},
isrSet,
loadSsrHandler: async () => ({
async handleSsr(_rscStream, _navigationContext, _fontData, captureOptions) {
if (captureOptions?.capturedRscDataRef) {
captureOptions.capturedRscDataRef.value = Promise.resolve(
new TextEncoder().encode("direct-flight").buffer,
);
}
void captureOptions?.sideStream?.cancel().catch(() => {});
return createStream(["<html>direct</html>"]);
},
}),
revalidateSeconds: 60,
});

const response = await runWithExecutionContext(executionContext, () =>
dispatchAppPage(options),
);
await response.text();
await Promise.all(waitUntilPromises.splice(0));

const writtenKeys = isrSet.mock.calls.map(([key]) => key);
expect(writtenKeys).toHaveLength(2);
expect(writtenKeys).toEqual(
expect.arrayContaining(["html:/photos/123", "rsc:/photos/123:none:none"]),
);
});

it("does not reuse queryless HTML when the page reads searchParams", async () => {
let pageExecutions = 0;
async function Page(props: Record<string, unknown>): Promise<React.ReactNode> {
Expand Down Expand Up @@ -2806,6 +2849,53 @@ describe("app page dispatch", () => {
expect(afterRan).toBe(true);
});

it("regenerates stale HTML-captured RSC data under the plain key without interception context", async () => {
let scheduledRender: unknown = null;
const isrSet = vi.fn<DispatchOptions["isrSet"]>(async () => {});
const { options } = createDispatchOptions({
cleanPathname: "/photos/123",
interceptionContext: null,
isProduction: true,
isrGet: vi.fn(async () =>
buildISRCacheEntry(buildCachedAppPageValue("<html>stale direct</html>"), true),
),
isrRscKey(pathname, mountedSlotsHeader, _renderMode, interceptionContext) {
return `rsc:${pathname}:${mountedSlotsHeader ?? "none"}:${interceptionContext ?? "none"}`;
},
isrSet,
loadSsrHandler: async () => ({
async handleSsr(_rscStream, _navigationContext, _fontData, captureOptions) {
if (captureOptions?.capturedRscDataRef) {
captureOptions.capturedRscDataRef.value = Promise.resolve(
new TextEncoder().encode("regenerated-direct-flight").buffer,
);
}
void captureOptions?.sideStream?.cancel().catch(() => {});
return createStream(["<html>regenerated direct</html>"]);
},
}),
revalidateSeconds: 60,
scheduleBackgroundRegeneration(_key, renderFn) {
scheduledRender = renderFn;
},
});

const response = await dispatchAppPage(options);
await expect(response.text()).resolves.toBe("<html>stale direct</html>");
expect(typeof scheduledRender).toBe("function");
if (typeof scheduledRender !== "function") {
throw new Error("expected stale HTML response to schedule regeneration");
}

await scheduledRender();

const writtenKeys = isrSet.mock.calls.map(([key]) => key);
expect(writtenKeys).toHaveLength(2);
expect(writtenKeys).toEqual(
expect.arrayContaining(["html:/photos/123", "rsc:/photos/123:none:none"]),
);
});

it.each(["page", "metadata"] as const)(
"records searchParams access when stale regeneration reads them in %s",
async (reader) => {
Expand Down
19 changes: 19 additions & 0 deletions tests/app-rsc-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,25 @@ describe("createAppRscHandler", () => {
);
});

// Next.js renders the full page rather than an intercepted tree on hard refresh:
// https://github.com/vercel/next.js/blob/canary/test/e2e/app-dir/dynamic-interception-route-revalidate/dynamic-interception-route-revalidate.test.ts
it("ignores client-supplied interception context on HTML page dispatch", async () => {
const dispatchMatchedPage = vi.fn(async () => new Response("page", { status: 200 }));
const handler = createHandler({ configHeaders: [], dispatchMatchedPage });

const response = await handler(
new Request("https://example.test/docs/about", {
headers: { "X-Vinext-Interception-Context": "/feed" },
}),
null,
);

expect(response.status).toBe(200);
expect(dispatchMatchedPage).toHaveBeenCalledWith(
expect.objectContaining({ interceptionContext: null, isRscRequest: false }),
);
});

// Interception renders the source route's tree, so that route must clear the
// same middleware boundary a direct request to it would. Next.js never renders
// the source for this request (its rewrite targets the intercepting route and
Expand Down
Loading