mirror of
https://github.com/cloudflare/vinext.git
synced 2026-09-14 19:04:59 +08:00
f6b5b8a9d6
* test: cover revalidate = 0 route handler never-cache semantics Route handlers with `export const revalidate = 0` currently emit no Cache-Control header at all. Downstream gates treat the 0 value as equivalent to "no revalidate configured" and skip the header write, which lets CDNs and browsers apply heuristic caching to a response the author explicitly opted out of. The existing policy test asserted the buggy collapse (`toBeNull()` for revalidate = 0), protecting the behaviour instead of verifying it. Rewrite the policy test to assert the preserved 0 and add coverage for the read/write/header gates for revalidate = 0. Add a response-level test asserting the exact Cache-Control string Next.js emits for revalidate = 0 (private, no-cache, no-store, max-age=0, must-revalidate). These tests fail against the current implementation and will pass once the policy helper preserves 0 and the header builder branches on 0. * fix: emit no-store Cache-Control for revalidate = 0 route handlers A route handler exporting `export const revalidate = 0` currently sends no Cache-Control header at all. Downstream CDNs and browsers fall back to heuristic caching, so a response the author explicitly opted out of caching can still be cached and replayed. getAppRouteHandlerRevalidateSeconds filtered on `> 0`, collapsing the "never cache" signal (0) and the "no revalidate configured" signal (null) into the same null return. The three gates downstream (shouldReadAppRouteHandlerCache, shouldApplyAppRouteHandlerRevalidateHeader, shouldWriteAppRouteHandlerCache) all short-circuit when revalidateSeconds is null, so the header was never applied and no cache entry was written. That second part was accidentally correct; the first part left the response vulnerable. Preserve 0 as a distinct value, using Number.isFinite to keep NaN and Infinity out of the number branch along with negatives. Keep the ISR read and write gates on `> 0` so a never-cache handler never serves or persists ISR entries. Keep the header gate on `!== null` so 0 still triggers a Cache-Control write, and branch buildRouteHandlerCacheControl on 0 to emit Next.js's canonical no-store string (matches packages/next/src/server/lib/cache-control.ts).
239 lines
7.8 KiB
TypeScript
239 lines
7.8 KiB
TypeScript
import { describe, expect, it } from "vite-plus/test";
|
|
import type { CachedRouteValue } from "../packages/vinext/src/shims/cache.js";
|
|
import {
|
|
applyRouteHandlerMiddlewareContext,
|
|
applyRouteHandlerRevalidateHeader,
|
|
buildAppRouteCacheValue,
|
|
buildRouteHandlerCachedResponse,
|
|
finalizeRouteHandlerResponse,
|
|
markRouteHandlerCacheMiss,
|
|
} from "../packages/vinext/src/server/app-route-handler-response.js";
|
|
|
|
function buildCachedRouteValue(
|
|
body: string,
|
|
headers: Record<string, string> = {},
|
|
): CachedRouteValue {
|
|
return {
|
|
kind: "APP_ROUTE",
|
|
body: new TextEncoder().encode(body).buffer,
|
|
status: 200,
|
|
headers,
|
|
};
|
|
}
|
|
|
|
describe("app route handler response helpers", () => {
|
|
it("returns the original response when no middleware context exists", () => {
|
|
const response = new Response("hello");
|
|
|
|
expect(
|
|
applyRouteHandlerMiddlewareContext(response, {
|
|
headers: null,
|
|
status: null,
|
|
}),
|
|
).toBe(response);
|
|
});
|
|
|
|
it("overrides singular headers and status with middleware values", async () => {
|
|
const response = new Response("hello", {
|
|
status: 200,
|
|
headers: {
|
|
"content-type": "text/plain",
|
|
"cache-control": "s-maxage=60, stale-while-revalidate",
|
|
"x-response": "app",
|
|
},
|
|
});
|
|
|
|
const result = applyRouteHandlerMiddlewareContext(response, {
|
|
headers: new Headers([
|
|
["cache-control", "private, max-age=5"],
|
|
["x-middleware", "mw"],
|
|
["x-response", "middleware-copy"],
|
|
]),
|
|
status: 202,
|
|
});
|
|
|
|
expect(result.status).toBe(202);
|
|
expect(result.headers.get("content-type")).toBe("text/plain");
|
|
expect(result.headers.get("cache-control")).toBe("private, max-age=5");
|
|
expect(result.headers.get("x-response")).toBe("middleware-copy");
|
|
expect(result.headers.get("x-middleware")).toBe("mw");
|
|
await expect(result.text()).resolves.toBe("hello");
|
|
});
|
|
|
|
it("appends additive middleware headers for Set-Cookie and Vary", async () => {
|
|
const response = new Response("hello", {
|
|
status: 200,
|
|
headers: [
|
|
["vary", "RSC, Accept"],
|
|
["set-cookie", "existing=1; Path=/"],
|
|
],
|
|
});
|
|
|
|
const middlewareHeaders = new Headers();
|
|
middlewareHeaders.append("vary", "Next-Router-State-Tree");
|
|
middlewareHeaders.append("set-cookie", "mw=1; Path=/");
|
|
middlewareHeaders.append("set-cookie", "mw=2; Path=/; HttpOnly");
|
|
|
|
const result = applyRouteHandlerMiddlewareContext(response, {
|
|
headers: middlewareHeaders,
|
|
status: null,
|
|
});
|
|
|
|
expect(result.headers.get("vary")).toBe("RSC, Accept, Next-Router-State-Tree");
|
|
expect(result.headers.getSetCookie()).toEqual([
|
|
"existing=1; Path=/",
|
|
"mw=1; Path=/",
|
|
"mw=2; Path=/; HttpOnly",
|
|
]);
|
|
await expect(result.text()).resolves.toBe("hello");
|
|
});
|
|
|
|
it("builds cached HIT and STALE route handler responses", async () => {
|
|
const cachedValue = buildCachedRouteValue("from-cache", {
|
|
"content-type": "text/plain",
|
|
});
|
|
|
|
const hit = buildRouteHandlerCachedResponse(cachedValue, {
|
|
cacheState: "HIT",
|
|
isHead: false,
|
|
revalidateSeconds: 60,
|
|
});
|
|
expect(hit.headers.get("x-vinext-cache")).toBe("HIT");
|
|
expect(hit.headers.get("cache-control")).toBe("s-maxage=60, stale-while-revalidate");
|
|
await expect(hit.text()).resolves.toBe("from-cache");
|
|
|
|
const staleHead = buildRouteHandlerCachedResponse(cachedValue, {
|
|
cacheState: "STALE",
|
|
isHead: true,
|
|
revalidateSeconds: 60,
|
|
});
|
|
expect(staleHead.headers.get("x-vinext-cache")).toBe("STALE");
|
|
expect(staleHead.headers.get("cache-control")).toBe("s-maxage=0, stale-while-revalidate");
|
|
await expect(staleHead.text()).resolves.toBe("");
|
|
});
|
|
|
|
it("serializes APP_ROUTE cache values without cache bookkeeping headers", async () => {
|
|
const response = new Response("cache me", {
|
|
status: 201,
|
|
headers: {
|
|
"content-type": "text/plain",
|
|
"cache-control": "s-maxage=60, stale-while-revalidate",
|
|
"x-vinext-cache": "MISS",
|
|
"x-extra": "kept",
|
|
},
|
|
});
|
|
|
|
const value = await buildAppRouteCacheValue(response);
|
|
|
|
expect(value.kind).toBe("APP_ROUTE");
|
|
expect(value.status).toBe(201);
|
|
expect(value.headers).toEqual({
|
|
"content-type": "text/plain",
|
|
"x-extra": "kept",
|
|
});
|
|
expect(new TextDecoder().decode(value.body)).toBe("cache me");
|
|
});
|
|
|
|
it("preserves multiple Set-Cookie headers when building cache value", async () => {
|
|
const response = new Response("with cookies", {
|
|
status: 200,
|
|
headers: [
|
|
["content-type", "application/json"],
|
|
["set-cookie", "session=abc; Path=/; HttpOnly"],
|
|
["set-cookie", "theme=dark; Path=/"],
|
|
["set-cookie", "lang=en; Path=/; SameSite=Lax"],
|
|
],
|
|
});
|
|
|
|
const value = await buildAppRouteCacheValue(response);
|
|
|
|
expect(value.headers["set-cookie"]).toEqual([
|
|
"session=abc; Path=/; HttpOnly",
|
|
"theme=dark; Path=/",
|
|
"lang=en; Path=/; SameSite=Lax",
|
|
]);
|
|
expect(value.headers["content-type"]).toBe("application/json");
|
|
});
|
|
|
|
it("omits set-cookie key when response has no Set-Cookie headers", async () => {
|
|
const response = new Response("no cookies", {
|
|
status: 200,
|
|
headers: { "content-type": "text/plain" },
|
|
});
|
|
|
|
const value = await buildAppRouteCacheValue(response);
|
|
|
|
expect(value.headers).toEqual({ "content-type": "text/plain" });
|
|
expect(value.headers["set-cookie"]).toBeUndefined();
|
|
});
|
|
|
|
it("round-trips multiple Set-Cookie headers through cache store and restore", async () => {
|
|
const original = new Response("round trip", {
|
|
status: 200,
|
|
headers: [
|
|
["content-type", "text/plain"],
|
|
["set-cookie", "a=1; Path=/"],
|
|
["set-cookie", "b=2; Path=/"],
|
|
],
|
|
});
|
|
|
|
const cached = await buildAppRouteCacheValue(original);
|
|
const restored = buildRouteHandlerCachedResponse(cached, {
|
|
cacheState: "HIT",
|
|
isHead: false,
|
|
revalidateSeconds: 60,
|
|
});
|
|
|
|
expect(restored.headers.getSetCookie()).toEqual(["a=1; Path=/", "b=2; Path=/"]);
|
|
await expect(restored.text()).resolves.toBe("round trip");
|
|
});
|
|
|
|
it("finalizes route handler responses with cookies and auto-head semantics", async () => {
|
|
const response = new Response("body", {
|
|
status: 202,
|
|
statusText: "Accepted",
|
|
headers: {
|
|
"content-type": "text/plain",
|
|
},
|
|
});
|
|
|
|
const result = finalizeRouteHandlerResponse(response, {
|
|
pendingCookies: ["a=1; Path=/"],
|
|
draftCookie: "draft=1; Path=/",
|
|
isHead: true,
|
|
});
|
|
|
|
expect(result.status).toBe(202);
|
|
expect(result.statusText).toBe("Accepted");
|
|
expect(result.headers.getSetCookie?.()).toEqual(["a=1; Path=/", "draft=1; Path=/"]);
|
|
await expect(result.text()).resolves.toBe("");
|
|
});
|
|
|
|
it("applies revalidate and MISS headers separately", () => {
|
|
const response = new Response("hello");
|
|
|
|
applyRouteHandlerRevalidateHeader(response, 30);
|
|
markRouteHandlerCacheMiss(response);
|
|
|
|
expect(response.headers.get("cache-control")).toBe("s-maxage=30, stale-while-revalidate");
|
|
expect(response.headers.get("x-vinext-cache")).toBe("MISS");
|
|
});
|
|
|
|
it("emits a no-store Cache-Control for revalidate = 0 route handlers", () => {
|
|
// A handler exporting `revalidate = 0` opts out of caching entirely.
|
|
// The Cache-Control must tell browsers and CDNs never to store the
|
|
// response. Emitting `s-maxage=0, stale-while-revalidate` (the SWR
|
|
// template) would still permit intermediate caches to serve stale
|
|
// copies, which is the exact opposite of the author's intent.
|
|
// The exact string matches Next.js's own cache-control helper:
|
|
// .nextjs-ref/packages/next/src/server/lib/cache-control.ts:29.
|
|
const response = new Response("no cache me");
|
|
|
|
applyRouteHandlerRevalidateHeader(response, 0);
|
|
|
|
expect(response.headers.get("cache-control")).toBe(
|
|
"private, no-cache, no-store, max-age=0, must-revalidate",
|
|
);
|
|
});
|
|
});
|