1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397import { test, expect } from "@playwright/test";
const BASE = "http://localhost:4174";
test.describe("App Router ISR", () => {
// ISR cache lifecycle tests (MISS โ HIT โ STALE โ regen) are skipped in the
// dev server project because ISR caching is disabled in dev mode to match
// Next.js behavior (every request re-renders fresh). These tests should run
// against a production server E2E project instead.
test("first request returns ISR page", async ({
request,
}) => {
const res = await request.get(`${BASE}/isr-test`);
expect(res.status()).toBe(200);
const html = await res.text();
expect(html).toContain("App Router ISR Test");
expect(html).toContain("Hello from ISR");
});
test.skip("second request within TTL is a cache HIT with same timestamp", async ({
request,
}) => {
// SKIP: ISR cache is disabled in dev mode โ no HIT/MISS/STALE semantics.
// This test should run against a production server.
const res1 = await request.get(`${BASE}/isr-test`);
const html1 = await res1.text();
const ts1 = html1.match(/data-testid="timestamp">(\d+)</)?.[1];
expect(ts1).toBeDefined();
const res2 = await request.get(`${BASE}/isr-test`);
const html2 = await res2.text();
const ts2 = html2.match(/data-testid="timestamp">(\d+)</)?.[1];
const cacheHeader = res2.headers()["x-vinext-cache"];
expect(cacheHeader).toBe("HIT");
expect(ts2).toBe(ts1);
});
test.skip("request after TTL expires returns STALE with same cached content", async ({
request,
}) => {
// SKIP: ISR cache is disabled in dev mode.
const res1 = await request.get(`${BASE}/isr-test`);
const html1 = await res1.text();
const ts1 = html1.match(/data-testid="timestamp">(\d+)</)?.[1];
expect(ts1).toBeDefined();
await new Promise((r) => setTimeout(r, 1500));
const res2 = await request.get(`${BASE}/isr-test`);
const html2 = await res2.text();
const ts2 = html2.match(/data-testid="timestamp">(\d+)</)?.[1];
const cacheHeader2 = res2.headers()["x-vinext-cache"];
expect(cacheHeader2).toBe("STALE");
expect(ts2).toBe(ts1);
});
test.skip("after STALE triggers regen, subsequent request is HIT", async ({
request,
}) => {
// SKIP: ISR cache is disabled in dev mode.
await request.get(`${BASE}/isr-test`);
await new Promise((r) => setTimeout(r, 1500));
const staleRes = await request.get(`${BASE}/isr-test`);
expect(staleRes.headers()["x-vinext-cache"]).toBe("STALE");
await new Promise((r) => setTimeout(r, 500));
const hitRes = await request.get(`${BASE}/isr-test`);
expect(hitRes.headers()["x-vinext-cache"]).toBe("HIT");
});
test("Cache-Control header includes s-maxage and stale-while-revalidate", async ({
request,
}) => {
const res = await request.get(`${BASE}/isr-test`);
const cc = res.headers()["cache-control"];
expect(cc).toBeDefined();
expect(cc).toContain("s-maxage=1");
expect(cc).toContain("stale-while-revalidate");
});
test("non-ISR page does not have ISR cache headers", async ({ request }) => {
const res = await request.get(`${BASE}/about`);
// About page has no `export const revalidate`, so no ISR headers
const cacheHeader = res.headers()["x-vinext-cache"];
// May be undefined or not present โ either way, should not be MISS/HIT/STALE
if (cacheHeader) {
expect(["MISS", "HIT", "STALE"]).not.toContain(cacheHeader);
}
});
test("ISR page renders correctly in browser", async ({ page }) => {
await page.goto(`${BASE}/isr-test`);
await expect(page.getByTestId("isr-test-page")).toBeVisible();
await expect(page.locator("h1")).toHaveText("App Router ISR Test");
await expect(page.getByTestId("message")).toHaveText("Hello from ISR");
const tsText = await page.getByTestId("timestamp").textContent();
expect(Number(tsText)).toBeGreaterThan(0);
});
test("existing revalidate-test page (60s TTL) has correct Cache-Control", async ({
request,
}) => {
// The revalidate-test fixture uses revalidate=60
const res = await request.get(`${BASE}/revalidate-test`);
const cc = res.headers()["cache-control"];
expect(cc).toBeDefined();
expect(cc).toContain("s-maxage=60");
expect(cc).toContain("stale-while-revalidate");
});
});
/**
* OpenNext Compat: ISR dynamicParams cache header tests
*
* Ported from: https://github.com/opennextjs/opennextjs-cloudflare/blob/main/examples/e2e/app-router/e2e/isr.test.ts
*
* OpenNext verifies that `dynamicParams=true` pages return HIT for prebuilt paths,
* MISS for non-prebuilt, and 404 for notFound(). `dynamicParams=false` returns 404
* for unknown params. These tests verify the same cache header semantics in vinext.
*/
test.describe("ISR dynamicParams cache headers", () => {
test.describe("dynamicParams=false (products)", () => {
// Ref: opennextjs-cloudflare isr.test.ts "dynamicParams set to false"
test("should return 200 on a prebuilt path", async ({ request }) => {
// Products fixture uses dynamicParams=false with generateStaticParams [1, 2, 3]
// Note: products page has no `export const revalidate`, so ISR is not active
// and x-vinext-cache may not be set. We verify the page renders correctly.
const res = await request.get(`${BASE}/products/1`);
expect(res.status()).toBe(200);
const html = await res.text();
// React SSR inserts <!-- --> comment nodes between text and expressions,
// so "Product 1" may appear as "Product <!-- -->1" in raw HTML.
// lgtm[js/redos] โ applied to trusted SSR output, not user input
expect(html).toMatch(/Product\s*(?:<!--.*?-->)*\s*1/);
});
test("should return 404 for a path not in generateStaticParams", async ({
request,
}) => {
// Ref: opennextjs-cloudflare isr.test.ts "should 404 for a path that is not found"
const res = await request.get(`${BASE}/products/999`);
expect(res.status()).toBe(404);
const cc = res.headers()["cache-control"];
if (cc) {
expect(cc).toContain("no-cache");
}
});
});
test.describe("force-dynamic page", () => {
// Ref: opennextjs-cloudflare โ force-dynamic pages should never have ISR cache headers
test("should not have ISR cache header", async ({ request }) => {
const res = await request.get(`${BASE}/dynamic-test`);
expect(res.status()).toBe(200);
const cacheHeader = res.headers()["x-vinext-cache"];
expect(cacheHeader).toBeUndefined();
const cc = res.headers()["cache-control"];
if (cc) {
expect(cc).toContain("no-store");
}
});
test("should return different timestamps on each request", async ({
request,
}) => {
const res1 = await request.get(`${BASE}/dynamic-test`);
const html1 = await res1.text();
const ts1 = html1.match(/data-testid="timestamp">(\d+)</)?.[1];
expect(ts1).toBeDefined();
await new Promise((r) => setTimeout(r, 10));
const res2 = await request.get(`${BASE}/dynamic-test`);
const html2 = await res2.text();
const ts2 = html2.match(/data-testid="timestamp">(\d+)</)?.[1];
expect(Number(ts2)).toBeGreaterThan(Number(ts1));
});
});
test("404 response has private no-cache Cache-Control", async ({
request,
}) => {
// Ref: opennextjs-cloudflare isr.test.ts โ 404 responses should have
// "private, no-cache, no-store, max-age=0, must-revalidate"
const res = await request.get(`${BASE}/products/999`);
expect(res.status()).toBe(404);
const cc = res.headers()["cache-control"];
if (cc) {
// Should not have s-maxage or stale-while-revalidate on 404
expect(cc).not.toContain("s-maxage");
expect(cc).not.toContain("stale-while-revalidate");
}
});
});
/**
* OpenNext Compat: revalidateTag / revalidatePath E2E lifecycle tests.
*
* Ported from: https://github.com/opennextjs/opennextjs-cloudflare/blob/main/examples/e2e/app-router/e2e/revalidateTag.test.ts
* Tests: ON-2 in TRACKING.md
*
* OpenNext verifies the full tag-based cache invalidation lifecycle:
* 1. Load tagged ISR page -> cached (HIT)
* 2. Call /api/revalidate-tag -> tag invalidated
* 3. Reload -> content changed (MISS)
* 4. Subsequent request -> back to HIT
* They also verify nested pages sharing the same tag are also invalidated.
*/
test.describe("revalidateTag / revalidatePath lifecycle (OpenNext compat)", () => {
// SKIP: These tests depend on ISR caching (MISS/HIT/STALE semantics) which is
// disabled in dev mode. They should run against a production server E2E project.
test.skip("revalidateTag invalidates cached page and regenerates", async ({
request,
}) => {
// Ref: opennextjs-cloudflare revalidateTag.test.ts "Revalidate tag"
test.setTimeout(30_000);
// Load the tagged ISR page to populate cache
const res1 = await request.get(`${BASE}/revalidate-tag-test`);
expect(res1.status()).toBe(200);
const html1 = await res1.text();
// React SSR may insert <!-- --> comment nodes between text and expressions,
// so use a flexible regex that allows anything between the tag and content.
// lgtm[js/redos] โ applied to trusted SSR output, not user input
const reqId1 = html1.match(/data-testid="request-id"[^>]*>(?:<!--.*?-->)*RequestID:\s*(?:<!--.*?-->)*([a-z0-9]+)/)?.[1]
?? html1.match(/request-id[^>]*>[^<]*?([a-z0-9]{6,})/)?.[1];
expect(reqId1).toBeDefined();
// Load again to confirm it's cached (same request ID)
const res2 = await request.get(`${BASE}/revalidate-tag-test`);
const html2 = await res2.text();
// lgtm[js/redos] โ applied to trusted SSR output, not user input
const reqId2 = html2.match(/data-testid="request-id"[^>]*>(?:<!--.*?-->)*RequestID:\s*(?:<!--.*?-->)*([a-z0-9]+)/)?.[1]
?? html2.match(/request-id[^>]*>[^<]*?([a-z0-9]{6,})/)?.[1];
const cacheHeader = res2.headers()["x-vinext-cache"];
if (cacheHeader) {
expect(["HIT", "STALE"]).toContain(cacheHeader);
}
expect(reqId2).toBe(reqId1);
// Call revalidateTag API
const tagRes = await request.get(`${BASE}/api/revalidate-tag`);
expect(tagRes.status()).toBe(200);
const tagText = await tagRes.text();
expect(tagText).toBe("ok");
// Reload โ content should be different (cache was invalidated)
const res3 = await request.get(`${BASE}/revalidate-tag-test`);
const html3 = await res3.text();
// lgtm[js/redos] โ applied to trusted SSR output, not user input
const reqId3 = html3.match(/data-testid="request-id"[^>]*>(?:<!--.*?-->)*RequestID:\s*(?:<!--.*?-->)*([a-z0-9]+)/)?.[1]
?? html3.match(/request-id[^>]*>[^<]*?([a-z0-9]{6,})/)?.[1];
// After invalidation, should get fresh content
expect(reqId3).not.toBe(reqId1);
// Cache header should be MISS after invalidation
const cacheHeader3 = res3.headers()["x-vinext-cache"];
if (cacheHeader3) {
expect(cacheHeader3).toBe("MISS");
}
});
test.skip("revalidatePath invalidates specific path", async ({ request }) => {
// Ref: opennextjs-cloudflare revalidateTag.test.ts "Revalidate path"
test.setTimeout(30_000);
// Load the page to populate cache
const res1 = await request.get(`${BASE}/revalidate-tag-test`);
expect(res1.status()).toBe(200);
const html1 = await res1.text();
// lgtm[js/redos] โ applied to trusted SSR output, not user input
const reqId1 = html1.match(/data-testid="request-id"[^>]*>(?:<!--.*?-->)*RequestID:\s*(?:<!--.*?-->)*([a-z0-9]+)/)?.[1]
?? html1.match(/request-id[^>]*>[^<]*?([a-z0-9]{6,})/)?.[1];
expect(reqId1).toBeDefined();
// Wait a moment, then call revalidatePath
await new Promise((r) => setTimeout(r, 500));
const pathRes = await request.get(`${BASE}/api/revalidate-path`);
expect(pathRes.status()).toBe(200);
expect(await pathRes.text()).toBe("ok");
// Reload โ content should be different
const res2 = await request.get(`${BASE}/revalidate-tag-test`);
const html2 = await res2.text();
// lgtm[js/redos] โ applied to trusted SSR output, not user input
const reqId2 = html2.match(/data-testid="request-id"[^>]*>(?:<!--.*?-->)*RequestID:\s*(?:<!--.*?-->)*([a-z0-9]+)/)?.[1]
?? html2.match(/request-id[^>]*>[^<]*?([a-z0-9]{6,})/)?.[1];
expect(reqId2).not.toBe(reqId1);
});
test.skip("after invalidation + regen, subsequent request is HIT", async ({
request,
}) => {
// Ref: opennextjs-cloudflare revalidateTag.test.ts โ after MISS, next request should be HIT
test.setTimeout(30_000);
// Populate cache
await request.get(`${BASE}/revalidate-tag-test`);
// Invalidate
await request.get(`${BASE}/api/revalidate-tag`);
// First request after invalidation โ MISS (regen)
await request.get(`${BASE}/revalidate-tag-test`);
// Second request โ should be HIT now
const hitRes = await request.get(`${BASE}/revalidate-tag-test`);
const cacheHeader = hitRes.headers()["x-vinext-cache"];
if (cacheHeader) {
expect(cacheHeader).toBe("HIT");
}
});
// Ref: opennextjs-cloudflare revalidateTag.test.ts โ "nested page shares tag"
// Tests: ON-2 #2 in TRACKING.md
// Same blocker as parent: revalidateTag does not invalidate ISR cache in dev server.
test.fixme(
"nested page sharing same tag is also invalidated",
async ({ request }) => {
test.setTimeout(30_000);
// Load nested page to populate cache
const res1 = await request.get(`${BASE}/revalidate-tag-test/nested`);
expect(res1.status()).toBe(200);
const html1 = await res1.text();
const ts1 = html1.match(/Fetched time:\s*(\d+)/)?.[1];
expect(ts1).toBeDefined();
// Invalidate "test-data" tag (shared between parent and nested pages)
const tagRes = await request.get(`${BASE}/api/revalidate-tag`);
expect(tagRes.status()).toBe(200);
// Reload nested page โ should get fresh content
const res2 = await request.get(`${BASE}/revalidate-tag-test/nested`);
const html2 = await res2.text();
const ts2 = html2.match(/Fetched time:\s*(\d+)/)?.[1];
expect(ts2).not.toBe(ts1);
},
);
});
/**
* OpenNext Compat: ISR data cache (unstable_cache) separation from page cache.
*
* Ported from: https://github.com/opennextjs/opennextjs-cloudflare/blob/main/examples/e2e/app-router/e2e/isr.test.ts
* Tests: ON-1 #8 in TRACKING.md
*
* Verifies that unstable_cache (data cache) works alongside ISR page caching.
*/
test.describe("unstable_cache data cache (OpenNext compat)", () => {
test("unstable_cache returns consistent data across requests", async ({
request,
}) => {
// Ref: opennextjs-cloudflare isr.test.ts โ data cache separate from page cache
const res1 = await request.get(`${BASE}/unstable-cache-test`);
expect(res1.status()).toBe(200);
const html1 = await res1.text();
// React SSR inserts <!-- --> comment nodes between text and expressions
const value1 = html1.match(
/CachedValue:\s*(?:<!--[^>]*-->)*\s*([a-z0-9]{4,})/,
)?.[1];
expect(value1).toBeDefined();
// Second request should return the same cached value
const res2 = await request.get(`${BASE}/unstable-cache-test`);
const html2 = await res2.text();
const value2 = html2.match(
/CachedValue:\s*(?:<!--[^>]*-->)*\s*([a-z0-9]{4,})/,
)?.[1];
expect(value2).toBe(value1);
});
});