-
Notifications
You must be signed in to change notification settings - Fork 27k
/
app-static.test.ts
4297 lines (3889 loc) · 150 KB
/
app-static.test.ts
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import globOrig from 'glob'
import cheerio from 'cheerio'
import { promisify } from 'node:util'
import { join } from 'node:path'
import { nextTestSetup } from 'e2e-utils'
import {
check,
fetchViaHTTP,
normalizeRegEx,
retry,
waitFor,
} from 'next-test-utils'
import stripAnsi from 'strip-ansi'
const glob = promisify(globOrig)
describe('app-dir static/dynamic handling', () => {
const { next, isNextDev, isNextStart, isNextDeploy, isTurbopack } =
nextTestSetup({
files: __dirname,
env: {
NEXT_DEBUG_BUILD: '1',
...(process.env.CUSTOM_CACHE_HANDLER
? {
CUSTOM_CACHE_HANDLER: process.env.CUSTOM_CACHE_HANDLER,
}
: {}),
},
})
let prerenderManifest
let buildCliOutputIndex = 0
beforeAll(async () => {
if (isNextStart) {
prerenderManifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json')
)
buildCliOutputIndex = next.cliOutput.length
}
})
it('should use auto no cache when no fetch config', async () => {
const res = await next.fetch('/no-config-fetch')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const data = $('#data').text()
expect(data).toBeTruthy()
const res2 = await next.fetch('/no-config-fetch')
const html2 = await res2.text()
const data2 = cheerio.load(html2)('#data').text()
if (isNextDev) {
expect(data).not.toBe(data2)
} else {
const pageCache = (
res.headers.get('x-vercel-cache') || res.headers.get('x-nextjs-cache')
).toLowerCase()
expect(pageCache).toBeTruthy()
expect(pageCache).not.toBe('MISS')
expect(data).toBe(data2)
}
})
it('should correctly handle "default" cache statuses', async () => {
const res = await next.fetch('/default-config-fetch')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const data = $('#data').text()
expect(data).toBeTruthy()
const res2 = await next.fetch('/default-config-fetch')
const html2 = await res2.text()
const data2 = cheerio.load(html2)('#data').text()
if (isNextDev) {
expect(data).not.toBe(data2)
} else {
// "default" cache does not impact ISR handling on a page, similar to the above test
// case for no fetch config
const pageCache = (
res.headers.get('x-vercel-cache') || res.headers.get('x-nextjs-cache')
).toLowerCase()
expect(pageCache).toBeTruthy()
expect(pageCache).not.toBe('MISS')
expect(data).toBe(data2)
}
// route handlers should not automatically cache fetches with "default" cache
const routeRes = await next.fetch('/default-config-fetch/api')
const initialRouteData = (await routeRes.json()).data
const nextRes = await next.fetch('/default-config-fetch/api')
const newRouteData = (await nextRes.json()).data
expect(initialRouteData).not.toEqual(newRouteData)
})
it('should still cache even though the `traceparent` header was different', async () => {
const res = await next.fetch('/strip-header-traceparent')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const data1 = $('#data1').text()
const data2 = $('#data2').text()
expect(data1).toBeTruthy()
expect(data1).toBe(data2)
const echoedHeaders = JSON.parse($('#echoedHeaders').text())
expect(echoedHeaders.headers.traceparent).toEqual('C')
})
// Runtime logs aren't queryable in deploy mode
if (!isNextDeploy) {
it('should warn for too many cache tags', async () => {
const res = await next.fetch('/too-many-cache-tags')
expect(res.status).toBe(200)
await retry(() => {
expect(next.cliOutput).toContain('exceeded max tag count for')
expect(next.cliOutput).toContain('tag-65')
})
})
}
if (isNextDeploy) {
describe('new tags have been specified on subsequent fetch', () => {
it('should not fetch from memory cache', async () => {
const res1 = await next.fetch('/specify-new-tags/one-tag')
expect(res1.status).toBe(200)
const res2 = await next.fetch('/specify-new-tags/two-tags')
expect(res2.status).toBe(200)
const html1 = await res1.text()
const html2 = await res2.text()
const $1 = cheerio.load(html1)
const $2 = cheerio.load(html2)
const data1 = $1('#page-data').text()
const data2 = $2('#page-data').text()
expect(data1).not.toBe(data2)
})
it('should not fetch from memory cache after revalidateTag is used', async () => {
const res1 = await next.fetch('/specify-new-tags/one-tag')
expect(res1.status).toBe(200)
const revalidateRes = await next.fetch(
'/api/revalidate-tag-node?tag=thankyounext'
)
expect((await revalidateRes.json()).revalidated).toBe(true)
const res2 = await next.fetch('/specify-new-tags/two-tags')
expect(res2.status).toBe(200)
const html1 = await res1.text()
const html2 = await res2.text()
const $1 = cheerio.load(html1)
const $2 = cheerio.load(html2)
const data1 = $1('#page-data').text()
const data2 = $2('#page-data').text()
expect(data1).not.toBe(data2)
})
})
}
if (isNextStart) {
it('should propagate unstable_cache tags correctly', async () => {
const meta = JSON.parse(
await next.readFile(
'.next/server/app/variable-revalidate/revalidate-360-isr.meta'
)
)
expect(meta.headers['x-next-cache-tags']).toContain('unstable_cache_tag1')
})
it('should infer a fetchCache of force-no-store when force-dynamic is used', async () => {
const $ = await next.render$('/force-dynamic-fetch-cache/no-fetch-cache')
const initData = $('#data').text()
await retry(async () => {
const $2 = await next.render$(
'/force-dynamic-fetch-cache/no-fetch-cache'
)
expect($2('#data').text()).toBeTruthy()
expect($2('#data').text()).not.toBe(initData)
})
// Check route handlers as well
const initFetchData = await (
await next.fetch('/force-dynamic-fetch-cache/no-fetch-cache/route')
).json()
await retry(async () => {
const newFetchData = await (
await next.fetch('/force-dynamic-fetch-cache/no-fetch-cache/route')
).json()
expect(newFetchData).toBeTruthy()
expect(newFetchData).not.toEqual(initFetchData)
})
})
it('should infer a fetch cache of "force-cache" when force-dynamic is used on a fetch with revalidate', async () => {
let currentData: string | undefined
await retry(async () => {
const $ = await next.render$('/force-dynamic-fetch-cache/revalidate')
const initialData = $('#data').text()
expect($('#data').text()).toBeTruthy()
const $2 = await next.render$('/force-dynamic-fetch-cache/revalidate')
currentData = $2('#data').text()
expect(currentData).toBeTruthy()
expect(currentData).toBe(initialData)
})
// wait for revalidation
await waitFor(3000)
await retry(async () => {
const $3 = await next.render$('/force-dynamic-fetch-cache/revalidate')
const finalValue = $3('#data').text()
expect(finalValue).toBeTruthy()
expect(finalValue).not.toBe(currentData)
})
})
it('force-dynamic should supercede a "default" cache value', async () => {
const $ = await next.render$('/force-dynamic-fetch-cache/default-cache')
const initData = $('#data').text()
await retry(async () => {
const $2 = await next.render$(
'/force-dynamic-fetch-cache/default-cache'
)
expect($2('#data').text()).toBeTruthy()
expect($2('#data').text()).not.toBe(initData)
})
// Check route handlers as well
const initFetchData = await (
await next.fetch('/force-dynamic-fetch-cache/default-cache/route')
).json()
await retry(async () => {
const newFetchData = await (
await next.fetch('/force-dynamic-fetch-cache/default-cache/route')
).json()
expect(newFetchData).toBeTruthy()
expect(newFetchData).not.toEqual(initFetchData)
})
})
it('fetchCache config should supercede dynamic config when force-dynamic is used', async () => {
const $ = await next.render$(
'/force-dynamic-fetch-cache/with-fetch-cache'
)
const initData = $('#data').text()
await retry(async () => {
const $2 = await next.render$(
'/force-dynamic-fetch-cache/with-fetch-cache'
)
expect($2('#data').text()).toBeTruthy()
expect($2('#data').text()).toBe(initData)
})
// Check route handlers as well
const initFetchData = await (
await next.fetch('/force-dynamic-fetch-cache/with-fetch-cache/route')
).json()
await retry(async () => {
const newFetchData = await (
await next.fetch('/force-dynamic-fetch-cache/with-fetch-cache/route')
).json()
expect(newFetchData).toBeTruthy()
expect(newFetchData).toEqual(initFetchData)
})
})
it('fetch `cache` should supercede dynamic config when force-dynamic is used', async () => {
const $ = await next.render$('/force-dynamic-fetch-cache/force-cache')
const initData = $('#data').text()
await retry(async () => {
const $2 = await next.render$('/force-dynamic-fetch-cache/force-cache')
expect($2('#data').text()).toBeTruthy()
expect($2('#data').text()).toBe(initData)
})
// Check route handlers as well
const initFetchData = await (
await next.fetch('/force-dynamic-fetch-cache/force-cache/route')
).json()
await retry(async () => {
const newFetchData = await (
await next.fetch('/force-dynamic-fetch-cache/force-cache/route')
).json()
expect(newFetchData).toBeTruthy()
expect(newFetchData).toEqual(initFetchData)
})
})
if (!process.env.CUSTOM_CACHE_HANDLER) {
it('should honor force-static with fetch cache: no-store correctly', async () => {
const res = await next.fetch('/force-static-fetch-no-store')
expect(res.status).toBe(200)
expect(res.headers.get('x-nextjs-cache')?.toLowerCase()).toBe('hit')
})
}
}
it('should correctly include headers instance in cache key', async () => {
const res = await next.fetch('/variable-revalidate/headers-instance')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const data1 = $('#page-data').text()
const data2 = $('#page-data2').text()
expect(data1).not.toBe(data2)
expect(data1).toBeTruthy()
expect(data2).toBeTruthy()
})
it.skip.each([
{
path: '/react-fetch-deduping-node',
},
{
path: '/react-fetch-deduping-edge',
},
])(
'should correctly de-dupe fetch without next cache $path',
async ({ path }) => {
for (let i = 0; i < 5; i++) {
const res = await next.fetch(path, {
redirect: 'manual',
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const data1 = $('#data-1').text()
const data2 = $('#data-2').text()
expect(data1).toBeTruthy()
expect(data1).toBe(data2)
await waitFor(250)
}
}
)
it.each([
{ pathname: '/unstable-cache-node' },
{ pathname: '/unstable-cache-edge' },
{ pathname: '/api/unstable-cache-node' },
{ pathname: '/api/unstable-cache-edge' },
])('unstable-cache should work in pages$pathname', async ({ pathname }) => {
let res = await next.fetch(pathname)
expect(res.status).toBe(200)
const isApi = pathname.startsWith('/api')
let prevData
if (isApi) {
prevData = await res.json()
} else {
const $ = isApi ? undefined : cheerio.load(await res.text())
prevData = JSON.parse($('#props').text())
}
expect(prevData.data.random).toBeTruthy()
await retry(async () => {
res = await next.fetch(pathname)
expect(res.status).toBe(200)
let curData
if (isApi) {
curData = await res.json()
} else {
const $ = cheerio.load(await res.text())
curData = JSON.parse($('#props').text())
}
try {
expect(curData.data.random).toBeTruthy()
expect(curData.data.random).toBe(prevData.data.random)
} finally {
prevData = curData
}
})
})
it('should not have cache tags header for non-minimal mode', async () => {
for (const path of [
'/ssr-forced',
'/ssr-forced',
'/variable-revalidate/revalidate-3',
'/variable-revalidate/revalidate-360',
'/variable-revalidate/revalidate-360-isr',
]) {
const res = await fetchViaHTTP(next.url, path, undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
}
})
if (isNextDev) {
it('should error correctly for invalid params from generateStaticParams', async () => {
await next.patchFile(
'app/invalid/[slug]/page.js',
`
export function generateStaticParams() {
return [{slug: { invalid: true }}]
}
`
)
// The page may take a moment to compile, so try it a few times.
await check(async () => {
return next.render('/invalid/first')
}, /A required parameter \(slug\) was not provided as a string received object/)
await next.deleteFile('app/invalid/[slug]/page.js')
})
it('should correctly handle multi-level generateStaticParams when some levels are missing', async () => {
const browser = await next.browser('/flight/foo/bar')
const v = ~~(Math.random() * 1000)
await browser.eval(`document.cookie = "test-cookie=${v}"`)
await browser.elementByCss('button').click()
await check(async () => {
return await browser.elementByCss('h1').text()
}, v.toString())
})
}
it('should correctly skip caching POST fetch for POST handler', async () => {
const res = await next.fetch('/route-handler/post', {
method: 'POST',
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data).toBeTruthy()
for (let i = 0; i < 5; i++) {
const res2 = await next.fetch('/route-handler/post', {
method: 'POST',
})
expect(res2.status).toBe(200)
const newData = await res2.json()
expect(newData).toBeTruthy()
expect(newData).not.toEqual(data)
}
})
if (!isNextDev && !process.env.CUSTOM_CACHE_HANDLER) {
// TODO: Temporarily disabling this test for Turbopack. The test is failing
// quite often (see https://app.datadoghq.com/ci/test-runs?query=test_level%3Atest%20env%3Aci%20%40git.repository.id%3Agit.luolix.top%2Fvercel%2Fnext.js%20%40test.service%3Anextjs%20%40test.status%3Afail%20%40test.name%3A%22app-dir%20static%2Fdynamic%20handling%20should%20properly%20revalidate%20a%20route%20handler%20that%20triggers%20dynamic%20usage%20with%20force-static%22&agg_m=count&agg_m_source=base&agg_t=count¤tTab=overview&eventStack=&fromUser=false&index=citest&start=1720993078523&end=1728769078523&paused=false).
// Since this is also reproducible when manually recreating the scenario, it
// might actually be a bug with ISR, which needs to be investigated.
if (!isTurbopack) {
it('should properly revalidate a route handler that triggers dynamic usage with force-static', async () => {
// wait for the revalidation period
let res = await next.fetch('/route-handler/no-store-force-static')
let data = await res.json()
// grab the initial timestamp
const initialTimestamp = data.now
// confirm its cached still
res = await next.fetch('/route-handler/no-store-force-static')
data = await res.json()
expect(data.now).toBe(initialTimestamp)
// wait for the revalidation time
await waitFor(3000)
// verify fresh data
res = await next.fetch('/route-handler/no-store-force-static')
data = await res.json()
expect(data.now).not.toBe(initialTimestamp)
})
}
}
if (!process.env.CUSTOM_CACHE_HANDLER) {
it.each([
{
type: 'edge route handler',
revalidateApi: '/api/revalidate-tag-edge',
},
{
type: 'node route handler',
revalidateApi: '/api/revalidate-tag-node',
},
])(
'it should revalidate tag correctly with $type',
async ({ revalidateApi }) => {
const initRes = await next.fetch('/variable-revalidate/revalidate-360')
const html = await initRes.text()
const $ = cheerio.load(html)
const initLayoutData = $('#layout-data').text()
const initPageData = $('#page-data').text()
const initNestedCacheData = $('#nested-cache').text()
const routeHandlerRes = await next.fetch(
'/route-handler/revalidate-360'
)
const initRouteHandlerData = await routeHandlerRes.json()
const edgeRouteHandlerRes = await next.fetch(
'/route-handler-edge/revalidate-360'
)
const initEdgeRouteHandlerRes = await edgeRouteHandlerRes.json()
expect(initLayoutData).toBeTruthy()
expect(initPageData).toBeTruthy()
await check(async () => {
const revalidateRes = await next.fetch(
`${revalidateApi}?tag=thankyounext`
)
expect((await revalidateRes.json()).revalidated).toBe(true)
const newRes = await next.fetch('/variable-revalidate/revalidate-360')
const cacheHeader = newRes.headers.get('x-nextjs-cache')
if ((global as any).isNextStart && cacheHeader) {
expect(cacheHeader).toBe('MISS')
}
const newHtml = await newRes.text()
const new$ = cheerio.load(newHtml)
const newLayoutData = new$('#layout-data').text()
const newPageData = new$('#page-data').text()
const newNestedCacheData = new$('#nested-cache').text()
const newRouteHandlerRes = await next.fetch(
'/route-handler/revalidate-360'
)
const newRouteHandlerData = await newRouteHandlerRes.json()
const newEdgeRouteHandlerRes = await next.fetch(
'/route-handler-edge/revalidate-360'
)
const newEdgeRouteHandlerData = await newEdgeRouteHandlerRes.json()
expect(newLayoutData).toBeTruthy()
expect(newPageData).toBeTruthy()
expect(newRouteHandlerData).toBeTruthy()
expect(newEdgeRouteHandlerData).toBeTruthy()
expect(newLayoutData).not.toBe(initLayoutData)
expect(newPageData).not.toBe(initPageData)
expect(newNestedCacheData).not.toBe(initNestedCacheData)
expect(newRouteHandlerData).not.toEqual(initRouteHandlerData)
expect(newEdgeRouteHandlerData).not.toEqual(initEdgeRouteHandlerRes)
return 'success'
}, 'success')
}
)
}
// On-Demand Revalidate has not effect in dev since app routes
// aren't considered static until prerendering
if (!(global as any).isNextDev && !process.env.CUSTOM_CACHE_HANDLER) {
it('should not revalidate / when revalidate is not used', async () => {
let prevData
for (let i = 0; i < 5; i++) {
const res = await next.fetch('/')
const html = await res.text()
const $ = cheerio.load(html)
const data = $('#page-data').text()
expect(res.status).toBe(200)
if (prevData) {
expect(prevData).toBe(data)
prevData = data
}
await waitFor(500)
}
if (isNextStart) {
expect(next.cliOutput.substring(buildCliOutputIndex)).not.toContain(
'rendering index'
)
}
})
it.each([
{
type: 'edge route handler',
revalidateApi: '/api/revalidate-path-edge',
},
{
type: 'node route handler',
revalidateApi: '/api/revalidate-path-node',
},
])(
'it should revalidate correctly with $type',
async ({ revalidateApi }) => {
const initRes = await next.fetch(
'/variable-revalidate/revalidate-360-isr'
)
const html = await initRes.text()
const $ = cheerio.load(html)
const initLayoutData = $('#layout-data').text()
const initPageData = $('#page-data').text()
expect(initLayoutData).toBeTruthy()
expect(initPageData).toBeTruthy()
await check(async () => {
const revalidateRes = await next.fetch(
`${revalidateApi}?path=/variable-revalidate/revalidate-360-isr`
)
expect((await revalidateRes.json()).revalidated).toBe(true)
const newRes = await next.fetch(
'/variable-revalidate/revalidate-360-isr'
)
const newHtml = await newRes.text()
const new$ = cheerio.load(newHtml)
const newLayoutData = new$('#layout-data').text()
const newPageData = new$('#page-data').text()
expect(newLayoutData).toBeTruthy()
expect(newPageData).toBeTruthy()
expect(newLayoutData).not.toBe(initLayoutData)
expect(newPageData).not.toBe(initPageData)
return 'success'
}, 'success')
}
)
}
// On-Demand Revalidate has not effect in dev
if (!(global as any).isNextDev && !process.env.CUSTOM_CACHE_HANDLER) {
it('should revalidate all fetches during on-demand revalidate', async () => {
const initRes = await next.fetch(
'/variable-revalidate/revalidate-360-isr'
)
const html = await initRes.text()
const $ = cheerio.load(html)
const initLayoutData = $('#layout-data').text()
const initPageData = $('#page-data').text()
expect(initLayoutData).toBeTruthy()
expect(initPageData).toBeTruthy()
await check(async () => {
const revalidateRes = await next.fetch(
'/api/revalidate-path-node?path=/variable-revalidate/revalidate-360-isr'
)
expect((await revalidateRes.json()).revalidated).toBe(true)
const newRes = await next.fetch(
'/variable-revalidate/revalidate-360-isr'
)
const newHtml = await newRes.text()
const new$ = cheerio.load(newHtml)
const newLayoutData = new$('#layout-data').text()
const newPageData = new$('#page-data').text()
expect(newLayoutData).toBeTruthy()
expect(newPageData).toBeTruthy()
expect(newLayoutData).not.toBe(initLayoutData)
expect(newPageData).not.toBe(initPageData)
return 'success'
}, 'success')
})
}
it('should correctly handle fetchCache = "force-no-store"', async () => {
const initRes = await next.fetch('/force-no-store')
const html = await initRes.text()
const $ = cheerio.load(html)
const initPageData = $('#page-data').text()
expect(initPageData).toBeTruthy()
const newRes = await next.fetch('/force-no-store')
const newHtml = await newRes.text()
const new$ = cheerio.load(newHtml)
const newPageData = new$('#page-data').text()
expect(newPageData).toBeTruthy()
expect(newPageData).not.toBe(initPageData)
})
if (!process.env.CUSTOM_CACHE_HANDLER) {
it('should revalidate correctly with config and fetch revalidate', async () => {
const initial$ = await next.render$(
'/variable-config-revalidate/revalidate-3'
)
const initialDate = initial$('#date').text()
const initialRandomData = initial$('#random-data').text()
expect(initialDate).toBeTruthy()
expect(initialRandomData).toBeTruthy()
let prevInitialDate
let prevInitialRandomData
// wait for a fresh revalidation
await check(async () => {
const $ = await next.render$('/variable-config-revalidate/revalidate-3')
prevInitialDate = $('#date').text()
prevInitialRandomData = $('#random-data').text()
expect(prevInitialDate).not.toBe(initialDate)
expect(prevInitialRandomData).not.toBe(initialRandomData)
return 'success'
}, 'success')
// the date should revalidate first after 3 seconds
// while the fetch data stays in place for 9 seconds
await check(async () => {
const $ = await next.render$('/variable-config-revalidate/revalidate-3')
const curDate = $('#date').text()
const curRandomData = $('#random-data').text()
expect(curDate).not.toBe(prevInitialDate)
expect(curRandomData).not.toBe(prevInitialRandomData)
prevInitialDate = curDate
prevInitialRandomData = curRandomData
return 'success'
}, 'success')
})
}
it('should not cache non-ok statusCode', async () => {
await check(async () => {
const $ = await next.render$('/variable-revalidate/status-code')
const origData = JSON.parse($('#page-data').text())
expect(origData.status).toBe(404)
const new$ = await next.render$('/variable-revalidate/status-code')
const newData = JSON.parse(new$('#page-data').text())
expect(newData.status).toBe(origData.status)
expect(newData.text).not.toBe(origData.text)
return 'success'
}, 'success')
})
if (isNextStart) {
it('should not encode dynamic parameters as search parameters in RSC data', async () => {
const data = process.env.__NEXT_EXPERIMENTAL_PPR
? await next.readFile('.next/server/app/blog/seb.prefetch.rsc')
: await next.readFile('.next/server/app/blog/seb.rsc')
// During SSG, pages that correspond with dynamic routes shouldn't have any search
// parameters in the `__PAGE__` segment string. The only time we expect to see
// search parameters in the `__PAGE__` segment string is when the RSC data is
// requested from the client with search parameters.
expect(data).not.toContain('__PAGE__?')
expect(data).toContain('__PAGE__')
})
it('should output HTML/RSC files for static paths', async () => {
const files = (
await glob('**/*', {
cwd: join(next.testDir, '.next/server/app'),
})
)
.filter((file) => file.match(/.*\.(js|html|rsc)$/))
.map((file) => {
return file.replace(
/partial-gen-params-no-additional-([\w]{1,})\/([\w]{1,})\/([\d]{1,})/,
'partial-gen-params-no-additional-$1/$2/RAND'
)
})
expect(files.sort()).toMatchInlineSnapshot(`
[
"(new)/custom/page.js",
"(new)/custom/page_client-reference-manifest.js",
"(new)/default-config-fetch/api/route.js",
"(new)/default-config-fetch/api/route_client-reference-manifest.js",
"(new)/default-config-fetch/page.js",
"(new)/default-config-fetch/page_client-reference-manifest.js",
"(new)/no-config-fetch/page.js",
"(new)/no-config-fetch/page_client-reference-manifest.js",
"_not-found.html",
"_not-found.rsc",
"_not-found/page.js",
"_not-found/page_client-reference-manifest.js",
"api/draft-mode/route.js",
"api/draft-mode/route_client-reference-manifest.js",
"api/large-data/route.js",
"api/large-data/route_client-reference-manifest.js",
"api/revalidate-path-edge/route.js",
"api/revalidate-path-edge/route_client-reference-manifest.js",
"api/revalidate-path-node/route.js",
"api/revalidate-path-node/route_client-reference-manifest.js",
"api/revalidate-tag-edge/route.js",
"api/revalidate-tag-edge/route_client-reference-manifest.js",
"api/revalidate-tag-node/route.js",
"api/revalidate-tag-node/route_client-reference-manifest.js",
"articles/[slug]/page.js",
"articles/[slug]/page_client-reference-manifest.js",
"articles/works.html",
"articles/works.rsc",
"blog/[author]/[slug]/page.js",
"blog/[author]/[slug]/page_client-reference-manifest.js",
"blog/[author]/page.js",
"blog/[author]/page_client-reference-manifest.js",
"blog/seb.html",
"blog/seb.rsc",
"blog/seb/second-post.html",
"blog/seb/second-post.rsc",
"blog/styfle.html",
"blog/styfle.rsc",
"blog/styfle/first-post.html",
"blog/styfle/first-post.rsc",
"blog/styfle/second-post.html",
"blog/styfle/second-post.rsc",
"blog/tim.html",
"blog/tim.rsc",
"blog/tim/first-post.html",
"blog/tim/first-post.rsc",
"default-cache-search-params/page.js",
"default-cache-search-params/page_client-reference-manifest.js",
"default-cache/page.js",
"default-cache/page_client-reference-manifest.js",
"default-config-fetch.html",
"default-config-fetch.rsc",
"dynamic-error/[id]/page.js",
"dynamic-error/[id]/page_client-reference-manifest.js",
"dynamic-no-gen-params-ssr/[slug]/page.js",
"dynamic-no-gen-params-ssr/[slug]/page_client-reference-manifest.js",
"dynamic-no-gen-params/[slug]/page.js",
"dynamic-no-gen-params/[slug]/page_client-reference-manifest.js",
"dynamic-param-edge/[slug]/page.js",
"dynamic-param-edge/[slug]/page_client-reference-manifest.js",
"fetch-no-cache/page.js",
"fetch-no-cache/page_client-reference-manifest.js",
"flight/[slug]/[slug2]/page.js",
"flight/[slug]/[slug2]/page_client-reference-manifest.js",
"force-cache-revalidate/page.js",
"force-cache-revalidate/page_client-reference-manifest.js",
"force-cache.html",
"force-cache.rsc",
"force-cache/large-data/page.js",
"force-cache/large-data/page_client-reference-manifest.js",
"force-cache/page.js",
"force-cache/page_client-reference-manifest.js",
"force-dynamic-catch-all/[slug]/[[...id]]/page.js",
"force-dynamic-catch-all/[slug]/[[...id]]/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/default-cache/page.js",
"force-dynamic-fetch-cache/default-cache/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/default-cache/route/route.js",
"force-dynamic-fetch-cache/default-cache/route/route_client-reference-manifest.js",
"force-dynamic-fetch-cache/force-cache/page.js",
"force-dynamic-fetch-cache/force-cache/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/force-cache/route/route.js",
"force-dynamic-fetch-cache/force-cache/route/route_client-reference-manifest.js",
"force-dynamic-fetch-cache/no-fetch-cache/page.js",
"force-dynamic-fetch-cache/no-fetch-cache/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/no-fetch-cache/route/route.js",
"force-dynamic-fetch-cache/no-fetch-cache/route/route_client-reference-manifest.js",
"force-dynamic-fetch-cache/revalidate/page.js",
"force-dynamic-fetch-cache/revalidate/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/with-fetch-cache/page.js",
"force-dynamic-fetch-cache/with-fetch-cache/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/with-fetch-cache/route/route.js",
"force-dynamic-fetch-cache/with-fetch-cache/route/route_client-reference-manifest.js",
"force-dynamic-no-prerender/[id]/page.js",
"force-dynamic-no-prerender/[id]/page_client-reference-manifest.js",
"force-dynamic-prerender/[slug]/page.js",
"force-dynamic-prerender/[slug]/page_client-reference-manifest.js",
"force-no-store-bailout/page.js",
"force-no-store-bailout/page_client-reference-manifest.js",
"force-no-store/page.js",
"force-no-store/page_client-reference-manifest.js",
"force-static-fetch-no-store.html",
"force-static-fetch-no-store.rsc",
"force-static-fetch-no-store/page.js",
"force-static-fetch-no-store/page_client-reference-manifest.js",
"force-static/[slug]/page.js",
"force-static/[slug]/page_client-reference-manifest.js",
"force-static/first.html",
"force-static/first.rsc",
"force-static/page.js",
"force-static/page_client-reference-manifest.js",
"force-static/second.html",
"force-static/second.rsc",
"gen-params-dynamic-revalidate/[slug]/page.js",
"gen-params-dynamic-revalidate/[slug]/page_client-reference-manifest.js",
"gen-params-dynamic-revalidate/one.html",
"gen-params-dynamic-revalidate/one.rsc",
"gen-params-dynamic/[slug]/page.js",
"gen-params-dynamic/[slug]/page_client-reference-manifest.js",
"hooks/use-pathname/[slug]/page.js",
"hooks/use-pathname/[slug]/page_client-reference-manifest.js",
"hooks/use-pathname/slug.html",
"hooks/use-pathname/slug.rsc",
"hooks/use-search-params/force-static.html",
"hooks/use-search-params/force-static.rsc",
"hooks/use-search-params/force-static/page.js",
"hooks/use-search-params/force-static/page_client-reference-manifest.js",
"hooks/use-search-params/with-suspense.html",
"hooks/use-search-params/with-suspense.rsc",
"hooks/use-search-params/with-suspense/page.js",
"hooks/use-search-params/with-suspense/page_client-reference-manifest.js",
"index.html",
"index.rsc",
"isr-error-handling.html",
"isr-error-handling.rsc",
"isr-error-handling/page.js",
"isr-error-handling/page_client-reference-manifest.js",
"no-config-fetch.html",
"no-config-fetch.rsc",
"no-store/dynamic/page.js",
"no-store/dynamic/page_client-reference-manifest.js",
"no-store/static.html",
"no-store/static.rsc",
"no-store/static/page.js",
"no-store/static/page_client-reference-manifest.js",
"page.js",
"page_client-reference-manifest.js",
"partial-gen-params-no-additional-lang/[lang]/[slug]/page.js",
"partial-gen-params-no-additional-lang/[lang]/[slug]/page_client-reference-manifest.js",
"partial-gen-params-no-additional-lang/en/RAND.html",
"partial-gen-params-no-additional-lang/en/RAND.rsc",
"partial-gen-params-no-additional-lang/en/first.html",
"partial-gen-params-no-additional-lang/en/first.rsc",
"partial-gen-params-no-additional-lang/en/second.html",
"partial-gen-params-no-additional-lang/en/second.rsc",
"partial-gen-params-no-additional-lang/fr/RAND.html",
"partial-gen-params-no-additional-lang/fr/RAND.rsc",
"partial-gen-params-no-additional-lang/fr/first.html",
"partial-gen-params-no-additional-lang/fr/first.rsc",
"partial-gen-params-no-additional-lang/fr/second.html",
"partial-gen-params-no-additional-lang/fr/second.rsc",
"partial-gen-params-no-additional-slug/[lang]/[slug]/page.js",
"partial-gen-params-no-additional-slug/[lang]/[slug]/page_client-reference-manifest.js",
"partial-gen-params-no-additional-slug/en/RAND.html",
"partial-gen-params-no-additional-slug/en/RAND.rsc",
"partial-gen-params-no-additional-slug/en/first.html",
"partial-gen-params-no-additional-slug/en/first.rsc",
"partial-gen-params-no-additional-slug/en/second.html",
"partial-gen-params-no-additional-slug/en/second.rsc",
"partial-gen-params-no-additional-slug/fr/RAND.html",
"partial-gen-params-no-additional-slug/fr/RAND.rsc",
"partial-gen-params-no-additional-slug/fr/first.html",
"partial-gen-params-no-additional-slug/fr/first.rsc",
"partial-gen-params-no-additional-slug/fr/second.html",
"partial-gen-params-no-additional-slug/fr/second.rsc",
"partial-gen-params/[lang]/[slug]/page.js",
"partial-gen-params/[lang]/[slug]/page_client-reference-manifest.js",
"react-fetch-deduping-edge/page.js",
"react-fetch-deduping-edge/page_client-reference-manifest.js",
"react-fetch-deduping-node/page.js",
"react-fetch-deduping-node/page_client-reference-manifest.js",
"response-url/page.js",
"response-url/page_client-reference-manifest.js",
"route-handler-edge/revalidate-360/route.js",
"route-handler-edge/revalidate-360/route_client-reference-manifest.js",
"route-handler/no-store-force-static/route.js",
"route-handler/no-store-force-static/route_client-reference-manifest.js",
"route-handler/no-store/route.js",
"route-handler/no-store/route_client-reference-manifest.js",
"route-handler/post/route.js",
"route-handler/post/route_client-reference-manifest.js",
"route-handler/revalidate-360-isr/route.js",
"route-handler/revalidate-360-isr/route_client-reference-manifest.js",
"route-handler/revalidate-360/route.js",
"route-handler/revalidate-360/route_client-reference-manifest.js",
"route-handler/static-cookies/route.js",
"route-handler/static-cookies/route_client-reference-manifest.js",
"specify-new-tags/one-tag/page.js",
"specify-new-tags/one-tag/page_client-reference-manifest.js",
"specify-new-tags/two-tags/page.js",
"specify-new-tags/two-tags/page_client-reference-manifest.js",
"ssg-draft-mode.html",
"ssg-draft-mode.rsc",
"ssg-draft-mode/[[...route]]/page.js",
"ssg-draft-mode/[[...route]]/page_client-reference-manifest.js",