-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathpage-router.test.ts
1121 lines (1025 loc) · 52.1 KB
/
page-router.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 { expect } from '@playwright/test'
import { test } from '../utils/playwright-helpers.js'
import { nextVersionSatisfies } from '../utils/next-version-helpers.mjs'
export function waitFor(millis: number) {
return new Promise((resolve) => setTimeout(resolve, millis))
}
/**
* Check for content in 1 second intervals timing out after 30 seconds.
*
* @param {() => Promise<unknown> | unknown} contentFn
* @param {RegExp | string | number} regex
* @param {boolean} hardError
* @param {number} maxRetries
* @returns {Promise<boolean>}
*/
export async function check(
contentFn: () => any | Promise<any>,
regex: any,
hardError = true,
maxRetries = 30,
) {
let content
let lastErr
for (let tries = 0; tries < maxRetries; tries++) {
try {
content = await contentFn()
if (typeof regex !== typeof /regex/) {
if (regex === content) {
return true
}
} else if (regex.test(content)) {
// found the content
return true
}
await waitFor(1000)
} catch (err) {
await waitFor(1000)
lastErr = err
}
}
console.error('TIMED OUT CHECK: ', { regex, content, lastErr })
if (hardError) {
throw new Error('TIMED OUT: ' + regex + '\n\n' + content + '\n\n' + lastErr)
}
return false
}
test.describe('Simple Page Router (no basePath, no i18n)', () => {
test.describe('On-demand revalidate works correctly', () => {
for (const { label, prerendered, pagePath, revalidateApiBasePath, expectedH1Content } of [
{
label: 'prerendered page with static path and awaited res.revalidate()',
prerendered: true,
pagePath: '/static/revalidate-manual',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Show #71',
},
{
label: 'prerendered page with dynamic path and awaited res.revalidate()',
prerendered: true,
pagePath: '/products/prerendered',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Product prerendered',
},
{
label: 'not prerendered page with dynamic path and awaited res.revalidate()',
prerendered: false,
pagePath: '/products/not-prerendered',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Product not-prerendered',
},
{
label: 'not prerendered page with dynamic path and not awaited res.revalidate()',
prerendered: false,
pagePath: '/products/not-prerendered-and-not-awaited-revalidation',
revalidateApiBasePath: '/api/revalidate-no-await',
expectedH1Content: 'Product not-prerendered-and-not-awaited-revalidation',
},
{
label:
'prerendered page with dynamic path and awaited res.revalidate() - non-ASCII variant',
prerendered: true,
pagePath: '/products/事前レンダリング,test',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Product 事前レンダリング,test',
},
{
label:
'not prerendered page with dynamic path and awaited res.revalidate() - non-ASCII variant',
prerendered: false,
pagePath: '/products/事前レンダリングされていない,test',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Product 事前レンダリングされていない,test',
},
]) {
test(label, async ({ page, pollUntilHeadersMatch, pageRouter }) => {
// in case there is retry or some other test did hit that path before
// we want to make sure that cdn cache is not warmed up
const purgeCdnCache = await page.goto(
new URL(`/api/purge-cdn?path=${encodeURI(pagePath)}`, pageRouter.url).href,
)
expect(purgeCdnCache?.status()).toBe(200)
// wait a bit until cdn cache purge propagates
await page.waitForTimeout(500)
const response1 = await pollUntilHeadersMatch(new URL(pagePath, pageRouter.url).href, {
headersToMatch: {
// either first time hitting this route or we invalidated
// just CDN node in earlier step
// we will invoke function and see Next cache hit status
// in the response because it was prerendered at build time
// or regenerated in previous attempt to run this test
'cache-status': [
/"Netlify Edge"; fwd=(miss|stale)/m,
prerendered ? /"Next.js"; hit/m : /"Next.js"; (hit|fwd=miss)/m,
],
},
headersNotMatchedMessage:
'First request to tested page (html) should be a miss or stale on the Edge and hit in Next.js',
})
const headers1 = response1?.headers() || {}
expect(response1?.status()).toBe(200)
expect(headers1['x-nextjs-cache']).toBeUndefined()
expect(headers1['netlify-cache-tag']).toBe(`_n_t_${encodeURI(pagePath).toLowerCase()}`)
expect(headers1['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const date1 = await page.textContent('[data-testid="date-now"]')
const h1 = await page.textContent('h1')
expect(h1).toBe(expectedH1Content)
// check json route
const response1Json = await pollUntilHeadersMatch(
new URL(`_next/data/build-id${pagePath}.json`, pageRouter.url).href,
{
headersToMatch: {
// either first time hitting this route or we invalidated
// just CDN node in earlier step
// we will invoke function and see Next cache hit status \
// in the response because it was prerendered at build time
// or regenerated in previous attempt to run this test
'cache-status': [/"Netlify Edge"; fwd=(miss|stale)/m, /"Next.js"; hit/m],
},
headersNotMatchedMessage:
'First request to tested page (data) should be a miss or stale on the Edge and hit in Next.js',
},
)
const headers1Json = response1Json?.headers() || {}
expect(response1Json?.status()).toBe(200)
expect(headers1Json['x-nextjs-cache']).toBeUndefined()
expect(headers1Json['netlify-cache-tag']).toBe(`_n_t_${encodeURI(pagePath).toLowerCase()}`)
expect(headers1Json['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const data1 = (await response1Json?.json()) || {}
expect(data1?.pageProps?.time).toBe(date1)
const response2 = await pollUntilHeadersMatch(new URL(pagePath, pageRouter.url).href, {
headersToMatch: {
// we are hitting the same page again and we most likely will see
// CDN hit (in this case Next reported cache status is omitted
// as it didn't actually take place in handling this request)
// or we will see CDN miss because different CDN node handled request
'cache-status': /"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m,
},
headersNotMatchedMessage:
'Second request to tested page (html) should most likely be a hit on the Edge (optionally miss or stale if different CDN node)',
})
const headers2 = response2?.headers() || {}
expect(response2?.status()).toBe(200)
expect(headers2['x-nextjs-cache']).toBeUndefined()
if (!headers2['cache-status'].includes('"Netlify Edge"; hit')) {
// if we missed CDN cache, we will see Next cache hit status
// as we reuse cached response
expect(headers2['cache-status']).toMatch(/"Next.js"; hit/m)
}
expect(headers2['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
// the page is cached
const date2 = await page.textContent('[data-testid="date-now"]')
expect(date2).toBe(date1)
// check json route
const response2Json = await pollUntilHeadersMatch(
new URL(`/_next/data/build-id${pagePath}.json`, pageRouter.url).href,
{
headersToMatch: {
// we are hitting the same page again and we most likely will see
// CDN hit (in this case Next reported cache status is omitted
// as it didn't actually take place in handling this request)
// or we will see CDN miss because different CDN node handled request
'cache-status': /"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m,
},
headersNotMatchedMessage:
'Second request to tested page (data) should most likely be a hit on the Edge (optionally miss or stale if different CDN node)',
},
)
const headers2Json = response2Json?.headers() || {}
expect(response2Json?.status()).toBe(200)
expect(headers2Json['x-nextjs-cache']).toBeUndefined()
if (!headers2Json['cache-status'].includes('"Netlify Edge"; hit')) {
// if we missed CDN cache, we will see Next cache hit status
// as we reuse cached response
expect(headers2Json['cache-status']).toMatch(/"Next.js"; hit/m)
}
expect(headers2Json['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const data2 = (await response2Json?.json()) || {}
expect(data2?.pageProps?.time).toBe(date1)
const revalidate = await page.goto(
new URL(`${revalidateApiBasePath}?path=${pagePath}`, pageRouter.url).href,
)
expect(revalidate?.status()).toBe(200)
// wait a bit until the page got regenerated
await page.waitForTimeout(1000)
// now after the revalidation it should have a different date
const response3 = await pollUntilHeadersMatch(new URL(pagePath, pageRouter.url).href, {
headersToMatch: {
// revalidate refreshes Next cache, but not CDN cache
// so our request after revalidation means that Next cache is already
// warmed up with fresh response, but CDN cache just knows that previously
// cached response is stale, so we are hitting our function that serve
// already cached response
'cache-status': [/"Next.js"; hit/m, /"Netlify Edge"; fwd=(miss|stale)/m],
},
headersNotMatchedMessage:
'Third request to tested page (html) should be a miss or stale on the Edge and hit in Next.js after on-demand revalidation',
})
const headers3 = response3?.headers() || {}
expect(response3?.status()).toBe(200)
expect(headers3?.['x-nextjs-cache']).toBeUndefined()
// the page has now an updated date
const date3 = await page.textContent('[data-testid="date-now"]')
expect(date3).not.toBe(date2)
// check json route
const response3Json = await pollUntilHeadersMatch(
new URL(`/_next/data/build-id${pagePath}.json`, pageRouter.url).href,
{
headersToMatch: {
// revalidate refreshes Next cache, but not CDN cache
// so our request after revalidation means that Next cache is already
// warmed up with fresh response, but CDN cache just knows that previously
// cached response is stale, so we are hitting our function that serve
// already cached response
'cache-status': [/"Next.js"; hit/m, /"Netlify Edge"; fwd=(miss|stale)/m],
},
headersNotMatchedMessage:
'Third request to tested page (data) should be a miss or stale on the Edge and hit in Next.js after on-demand revalidation',
},
)
const headers3Json = response3Json?.headers() || {}
expect(response3Json?.status()).toBe(200)
expect(headers3Json['x-nextjs-cache']).toBeUndefined()
expect(headers3Json['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const data3 = (await response3Json?.json()) || {}
expect(data3?.pageProps?.time).toBe(date3)
})
}
})
test('Time based revalidate works correctly', async ({
page,
pollUntilHeadersMatch,
pageRouter,
}) => {
// in case there is retry or some other test did hit that path before
// we want to make sure that cdn cache is not warmed up
const purgeCdnCache = await page.goto(
new URL('/api/purge-cdn?path=/static/revalidate-slow-data', pageRouter.url).href,
)
expect(purgeCdnCache?.status()).toBe(200)
// wait a bit until cdn cache purge propagates and make sure page gets stale (revalidate 10)
await page.waitForTimeout(10_000)
const beforeFetch = new Date().toISOString()
const response1 = await pollUntilHeadersMatch(
new URL('static/revalidate-slow-data', pageRouter.url).href,
{
headersToMatch: {
// either first time hitting this route or we invalidated
// just CDN node in earlier step
// we will invoke function and see Next cache hit status \
// in the response because it was prerendered at build time
// or regenerated in previous attempt to run this test
'cache-status': [/"Netlify Edge"; fwd=(miss|stale)/m, /"Next.js"; hit/m],
},
headersNotMatchedMessage:
'First request to tested page (html) should be a miss or stale on the Edge and stale in Next.js',
},
)
expect(response1?.status()).toBe(200)
const date1 = (await page.textContent('[data-testid="date-now"]')) ?? ''
// ensure response was produced before invocation (served from cache)
expect(date1.localeCompare(beforeFetch)).toBeLessThan(0)
// wait a bit to ensure background work has a chance to finish
// (page is fresh for 10 seconds and it should take at least 5 seconds to regenerate, so we should wait at least more than 15 seconds)
await page.waitForTimeout(20_000)
const response2 = await pollUntilHeadersMatch(
new URL('static/revalidate-slow-data', pageRouter.url).href,
{
headersToMatch: {
// either first time hitting this route or we invalidated
// just CDN node in earlier step
// we will invoke function and see Next cache hit status \
// in the response because it was prerendered at build time
// or regenerated in previous attempt to run this test
'cache-status': [/"Netlify Edge"; fwd=(miss|stale)/m, /"Next.js"; hit;/m],
},
headersNotMatchedMessage:
'Second request to tested page (html) should be a miss or stale on the Edge and hit or stale in Next.js',
},
)
expect(response2?.status()).toBe(200)
const date2 = (await page.textContent('[data-testid="date-now"]')) ?? ''
// ensure response was produced after initial invocation
expect(beforeFetch.localeCompare(date2)).toBeLessThan(0)
})
test('should serve 404 page when requesting non existing page (no matching route)', async ({
page,
pageRouter,
}) => {
// 404 page is built and uploaded to blobs at build time
// when Next.js serves 404 it will try to fetch it from the blob store
// if request handler function is unable to get from blob store it will
// fail request handling and serve 500 error.
// This implicitly tests that request handler function is able to read blobs
// that are uploaded as part of site deploy.
const response = await page.goto(new URL('non-existing', pageRouter.url).href)
const headers = response?.headers() || {}
expect(response?.status()).toBe(404)
expect(await page.textContent('h1')).toBe('404')
// https://github.com/vercel/next.js/pull/69802 made changes to returned cache-control header,
// after that (14.2.10 and canary.147) 404 pages would have `private` directive, before that
// it would not
const shouldHavePrivateDirective = nextVersionSatisfies('^14.2.10 || >=15.0.0-canary.147')
expect(headers['netlify-cdn-cache-control']).toBe(
(shouldHavePrivateDirective ? 'private, ' : '') +
'no-cache, no-store, max-age=0, must-revalidate, durable',
)
expect(headers['cache-control']).toBe(
(shouldHavePrivateDirective ? 'private,' : '') +
'no-cache,no-store,max-age=0,must-revalidate',
)
})
test('should serve 404 page when requesting non existing page (marked with notFound: true in getStaticProps)', async ({
page,
pageRouter,
}) => {
const response = await page.goto(new URL('static/not-found', pageRouter.url).href)
const headers = response?.headers() || {}
expect(response?.status()).toBe(404)
expect(await page.textContent('h1')).toBe('404')
expect(headers['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
expect(headers['cache-control']).toBe('public,max-age=0,must-revalidate')
})
test('requesting a page with a very long name works', async ({ page, pageRouter }) => {
const response = await page.goto(
new URL(
'/products/an-incredibly-long-product-name-thats-impressively-repetetively-needlessly-overdimensioned-and-should-be-shortened-to-less-than-255-characters-for-the-sake-of-seo-and-ux-and-first-and-foremost-for-gods-sake-but-nobody-wont-ever-read-this-anyway',
pageRouter.url,
).href,
)
expect(response?.status()).toBe(200)
})
// adapted from https://github.com/vercel/next.js/blob/89fcf68c6acd62caf91a8cf0bfd3fdc566e75d9d/test/e2e/app-dir/app-static/app-static.test.ts#L108
test('unstable-cache should work', async ({ pageRouter }) => {
const pathname = `${pageRouter.url}/api/unstable-cache-node`
let res = await fetch(`${pageRouter.url}/api/unstable-cache-node`)
expect(res.status).toBe(200)
let prevData = await res.json()
expect(prevData.data.random).toBeTruthy()
await check(async () => {
res = await fetch(pathname)
expect(res.status).toBe(200)
const curData = await res.json()
try {
expect(curData.data.random).toBeTruthy()
expect(curData.data.random).toBe(prevData.data.random)
} finally {
prevData = curData
}
return 'success'
}, 'success')
})
test('Fully static pages should be cached permanently', async ({ page, pageRouter }) => {
const response = await page.goto(new URL('static/fully-static', pageRouter.url).href)
const headers = response?.headers() || {}
expect(headers['netlify-cdn-cache-control']).toBe('max-age=31536000, durable')
expect(headers['cache-control']).toBe('public,max-age=0,must-revalidate')
})
test('environment variables from .env files should be available for functions', async ({
pageRouter,
}) => {
const response = await fetch(`${pageRouter.url}/api/env`)
const data = await response.json()
expect(data).toEqual({
'.env': 'defined in .env',
'.env.local': 'defined in .env.local',
'.env.production': 'defined in .env.production',
'.env.production.local': 'defined in .env.production.local',
})
})
})
test.describe('Page Router with basePath and i18n', () => {
test.describe('Static revalidate works correctly', () => {
for (const { label, prerendered, pagePath, revalidateApiBasePath, expectedH1Content } of [
{
label: 'prerendered page with static path and awaited res.revalidate()',
prerendered: true,
pagePath: '/static/revalidate-manual',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Show #71',
},
{
label: 'prerendered page with dynamic path and awaited res.revalidate()',
prerendered: true,
pagePath: '/products/prerendered',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Product prerendered',
},
{
label: 'not prerendered page with dynamic path and awaited res.revalidate()',
prerendered: false,
pagePath: '/products/not-prerendered',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Product not-prerendered',
},
{
label: 'not prerendered page with dynamic path and not awaited res.revalidate()',
prerendered: false,
pagePath: '/products/not-prerendered-and-not-awaited-revalidation',
revalidateApiBasePath: '/api/revalidate-no-await',
expectedH1Content: 'Product not-prerendered-and-not-awaited-revalidation',
},
{
label:
'prerendered page with dynamic path and awaited res.revalidate() - non-ASCII variant',
prerendered: true,
pagePath: '/products/事前レンダリング,test',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Product 事前レンダリング,test',
},
{
label:
'not prerendered page with dynamic path and awaited res.revalidate() - non-ASCII variant',
prerendered: false,
pagePath: '/products/事前レンダリングされていない,test',
revalidateApiBasePath: '/api/revalidate',
expectedH1Content: 'Product 事前レンダリングされていない,test',
},
]) {
test.describe(label, () => {
test(`default locale`, async ({ page, pollUntilHeadersMatch, pageRouterBasePathI18n }) => {
// in case there is retry or some other test did hit that path before
// we want to make sure that cdn cache is not warmed up
const purgeCdnCache = await page.goto(
new URL(
`/base/path/api/purge-cdn?path=/en${encodeURI(pagePath)}`,
pageRouterBasePathI18n.url,
).href,
)
expect(purgeCdnCache?.status()).toBe(200)
// wait a bit until cdn cache purge propagates
await page.waitForTimeout(500)
const response1ImplicitLocale = await pollUntilHeadersMatch(
new URL(`base/path${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// either first time hitting this route or we invalidated
// just CDN node in earlier step
// we will invoke function and see Next cache hit status
// in the response because it was prerendered at build time
// or regenerated in previous attempt to run this test
'cache-status': [
/"Netlify Edge"; fwd=(miss|stale)/m,
prerendered ? /"Next.js"; hit/m : /"Next.js"; (hit|fwd=miss)/m,
],
},
headersNotMatchedMessage:
'First request to tested page (implicit locale html) should be a miss or stale on the Edge and hit in Next.js',
},
)
const headers1ImplicitLocale = response1ImplicitLocale?.headers() || {}
expect(response1ImplicitLocale?.status()).toBe(200)
expect(headers1ImplicitLocale['x-nextjs-cache']).toBeUndefined()
expect(headers1ImplicitLocale['netlify-cache-tag']).toBe(
`_n_t_/en${encodeURI(pagePath).toLowerCase()}`,
)
expect(headers1ImplicitLocale['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const date1ImplicitLocale = await page.textContent('[data-testid="date-now"]')
const h1ImplicitLocale = await page.textContent('h1')
expect(h1ImplicitLocale).toBe(expectedH1Content)
const response1ExplicitLocale = await pollUntilHeadersMatch(
new URL(`base/path/en${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// either first time hitting this route or we invalidated
// just CDN node in earlier step
// we will invoke function and see Next cache hit status \
// in the response because it was set by previous request that didn't have locale in pathname
'cache-status': [/"Netlify Edge"; fwd=(miss|stale)/m, /"Next.js"; hit/m],
},
headersNotMatchedMessage:
'First request to tested page (explicit locale html) should be a miss or stale on the Edge and hit in Next.js',
},
)
const headers1ExplicitLocale = response1ExplicitLocale?.headers() || {}
expect(response1ExplicitLocale?.status()).toBe(200)
expect(headers1ExplicitLocale['x-nextjs-cache']).toBeUndefined()
expect(headers1ExplicitLocale['netlify-cache-tag']).toBe(
`_n_t_/en${encodeURI(pagePath).toLowerCase()}`,
)
expect(headers1ExplicitLocale['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const date1ExplicitLocale = await page.textContent('[data-testid="date-now"]')
const h1ExplicitLocale = await page.textContent('h1')
expect(h1ExplicitLocale).toBe(expectedH1Content)
// implicit and explicit locale paths should be the same (same cached response)
expect(date1ImplicitLocale).toBe(date1ExplicitLocale)
// check json route
const response1Json = await pollUntilHeadersMatch(
new URL(`base/path/_next/data/build-id/en${pagePath}.json`, pageRouterBasePathI18n.url)
.href,
{
headersToMatch: {
// either first time hitting this route or we invalidated
// just CDN node in earlier step
// we will invoke function and see Next cache hit status \
// in the response because it was prerendered at build time
// or regenerated in previous attempt to run this test
'cache-status': [/"Netlify Edge"; fwd=(miss|stale)/m, /"Next.js"; hit/m],
},
headersNotMatchedMessage:
'First request to tested page (data) should be a miss or stale on the Edge and hit in Next.js',
},
)
const headers1Json = response1Json?.headers() || {}
expect(response1Json?.status()).toBe(200)
expect(headers1Json['x-nextjs-cache']).toBeUndefined()
expect(headers1Json['netlify-cache-tag']).toBe(
`_n_t_/en${encodeURI(pagePath).toLowerCase()}`,
)
expect(headers1Json['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const data1 = (await response1Json?.json()) || {}
expect(data1?.pageProps?.time).toBe(date1ImplicitLocale)
const response2ImplicitLocale = await pollUntilHeadersMatch(
new URL(`base/path${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// we are hitting the same page again and we most likely will see
// CDN hit (in this case Next reported cache status is omitted
// as it didn't actually take place in handling this request)
// or we will see CDN miss because different CDN node handled request
'cache-status': /"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m,
},
headersNotMatchedMessage:
'Second request to tested page (implicit locale html) should most likely be a hit on the Edge (optionally miss or stale if different CDN node)',
},
)
const headers2ImplicitLocale = response2ImplicitLocale?.headers() || {}
expect(response2ImplicitLocale?.status()).toBe(200)
expect(headers2ImplicitLocale['x-nextjs-cache']).toBeUndefined()
if (!headers2ImplicitLocale['cache-status'].includes('"Netlify Edge"; hit')) {
// if we missed CDN cache, we will see Next cache hit status
// as we reuse cached response
expect(headers2ImplicitLocale['cache-status']).toMatch(/"Next.js"; hit/m)
}
expect(headers2ImplicitLocale['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
// the page is cached
const date2ImplicitLocale = await page.textContent('[data-testid="date-now"]')
expect(date2ImplicitLocale).toBe(date1ImplicitLocale)
const response2ExplicitLocale = await pollUntilHeadersMatch(
new URL(`base/path${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// we are hitting the same page again and we most likely will see
// CDN hit (in this case Next reported cache status is omitted
// as it didn't actually take place in handling this request)
// or we will see CDN miss because different CDN node handled request
'cache-status': /"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m,
},
headersNotMatchedMessage:
'Second request to tested page (implicit locale html) should most likely be a hit on the Edge (optionally miss or stale if different CDN node)',
},
)
const headers2ExplicitLocale = response2ExplicitLocale?.headers() || {}
expect(response2ExplicitLocale?.status()).toBe(200)
expect(headers2ExplicitLocale['x-nextjs-cache']).toBeUndefined()
if (!headers2ExplicitLocale['cache-status'].includes('"Netlify Edge"; hit')) {
// if we missed CDN cache, we will see Next cache hit status
// as we reuse cached response
expect(headers2ExplicitLocale['cache-status']).toMatch(/"Next.js"; hit/m)
}
expect(headers2ExplicitLocale['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
// the page is cached
const date2ExplicitLocale = await page.textContent('[data-testid="date-now"]')
expect(date2ExplicitLocale).toBe(date1ExplicitLocale)
// check json route
const response2Json = await pollUntilHeadersMatch(
new URL(`base/path/_next/data/build-id/en${pagePath}.json`, pageRouterBasePathI18n.url)
.href,
{
headersToMatch: {
// we are hitting the same page again and we most likely will see
// CDN hit (in this case Next reported cache status is omitted
// as it didn't actually take place in handling this request)
// or we will see CDN miss because different CDN node handled request
'cache-status': /"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m,
},
headersNotMatchedMessage:
'Second request to tested page (data) should most likely be a hit on the Edge (optionally miss or stale if different CDN node)',
},
)
const headers2Json = response2Json?.headers() || {}
expect(response2Json?.status()).toBe(200)
if (!headers2Json['cache-status'].includes('"Netlify Edge"; hit')) {
// if we missed CDN cache, we will see Next cache hit status
// as we reuse cached response
expect(headers2Json['cache-status']).toMatch(/"Next.js"; hit/m)
}
expect(headers2Json['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const data2 = (await response2Json?.json()) || {}
expect(data2?.pageProps?.time).toBe(date1ImplicitLocale)
// revalidate implicit locale path
const revalidateImplicit = await page.goto(
new URL(
`/base/path${revalidateApiBasePath}?path=${pagePath}`,
pageRouterBasePathI18n.url,
).href,
)
expect(revalidateImplicit?.status()).toBe(200)
// wait a bit until the page got regenerated
await page.waitForTimeout(1000)
// now after the revalidation it should have a different date
const response3ImplicitLocale = await pollUntilHeadersMatch(
new URL(`base/path${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// revalidate refreshes Next cache, but not CDN cache
// so our request after revalidation means that Next cache is already
// warmed up with fresh response, but CDN cache just knows that previously
// cached response is stale, so we are hitting our function that serve
// already cached response
'cache-status': [/"Next.js"; hit/m, /"Netlify Edge"; fwd=(miss|stale)/m],
},
headersNotMatchedMessage:
'Third request to tested page (implicit locale html) should be a miss or stale on the Edge and hit in Next.js after on-demand revalidation',
},
)
const headers3ImplicitLocale = response3ImplicitLocale?.headers() || {}
expect(response3ImplicitLocale?.status()).toBe(200)
expect(headers3ImplicitLocale?.['x-nextjs-cache']).toBeUndefined()
// the page has now an updated date
const date3ImplicitLocale = await page.textContent('[data-testid="date-now"]')
expect(date3ImplicitLocale).not.toBe(date2ImplicitLocale)
const response3ExplicitLocale = await pollUntilHeadersMatch(
new URL(`base/path/en${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// revalidate refreshes Next cache, but not CDN cache
// so our request after revalidation means that Next cache is already
// warmed up with fresh response, but CDN cache just knows that previously
// cached response is stale, so we are hitting our function that serve
// already cached response
'cache-status': [/"Next.js"; hit/m, /"Netlify Edge"; fwd=(miss|stale)/m],
},
headersNotMatchedMessage:
'Third request to tested page (explicit locale html) should be a miss or stale on the Edge and hit in Next.js after on-demand revalidation',
},
)
const headers3ExplicitLocale = response3ExplicitLocale?.headers() || {}
expect(response3ExplicitLocale?.status()).toBe(200)
expect(headers3ExplicitLocale?.['x-nextjs-cache']).toBeUndefined()
// the page has now an updated date
const date3ExplicitLocale = await page.textContent('[data-testid="date-now"]')
expect(date3ExplicitLocale).not.toBe(date2ExplicitLocale)
// implicit and explicit locale paths should be the same (same cached response)
expect(date3ImplicitLocale).toBe(date3ExplicitLocale)
// check json route
const response3Json = await pollUntilHeadersMatch(
new URL(`base/path/_next/data/build-id/en${pagePath}.json`, pageRouterBasePathI18n.url)
.href,
{
headersToMatch: {
// revalidate refreshes Next cache, but not CDN cache
// so our request after revalidation means that Next cache is already
// warmed up with fresh response, but CDN cache just knows that previously
// cached response is stale, so we are hitting our function that serve
// already cached response
'cache-status': [/"Next.js"; hit/m, /"Netlify Edge"; fwd=(miss|stale)/m],
},
headersNotMatchedMessage:
'Third request to tested page (data) should be a miss or stale on the Edge and hit in Next.js after on-demand revalidation',
},
)
const headers3Json = response3Json?.headers() || {}
expect(response3Json?.status()).toBe(200)
expect(headers3Json['x-nextjs-cache']).toBeUndefined()
if (!headers3Json['cache-status'].includes('"Netlify Edge"; hit')) {
// if we missed CDN cache, we will see Next cache hit status
// as we reuse cached response
expect(headers3Json['cache-status']).toMatch(/"Next.js"; hit/m)
}
expect(headers3Json['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const data3 = (await response3Json?.json()) || {}
expect(data3?.pageProps?.time).toBe(date3ImplicitLocale)
// revalidate implicit locale path
const revalidateExplicit = await page.goto(
new URL(
`/base/path${revalidateApiBasePath}?path=/en${pagePath}`,
pageRouterBasePathI18n.url,
).href,
)
expect(revalidateExplicit?.status()).toBe(200)
// wait a bit until the page got regenerated
await page.waitForTimeout(1000)
// now after the revalidation it should have a different date
const response4ImplicitLocale = await pollUntilHeadersMatch(
new URL(`base/path${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// revalidate refreshes Next cache, but not CDN cache
// so our request after revalidation means that Next cache is already
// warmed up with fresh response, but CDN cache just knows that previously
// cached response is stale, so we are hitting our function that serve
// already cached response
'cache-status': [/"Next.js"; hit/m, /"Netlify Edge"; fwd=(miss|stale)/m],
},
headersNotMatchedMessage:
'Fourth request to tested page (implicit locale html) should be a miss or stale on the Edge and hit in Next.js after on-demand revalidation',
},
)
const headers4ImplicitLocale = response4ImplicitLocale?.headers() || {}
expect(response4ImplicitLocale?.status()).toBe(200)
expect(headers4ImplicitLocale?.['x-nextjs-cache']).toBeUndefined()
// the page has now an updated date
const date4ImplicitLocale = await page.textContent('[data-testid="date-now"]')
expect(date4ImplicitLocale).not.toBe(date3ImplicitLocale)
const response4ExplicitLocale = await pollUntilHeadersMatch(
new URL(`base/path/en${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// revalidate refreshes Next cache, but not CDN cache
// so our request after revalidation means that Next cache is already
// warmed up with fresh response, but CDN cache just knows that previously
// cached response is stale, so we are hitting our function that serve
// already cached response
'cache-status': [/"Next.js"; hit/m, /"Netlify Edge"; fwd=(miss|stale)/m],
},
headersNotMatchedMessage:
'Fourth request to tested page (explicit locale html) should be a miss or stale on the Edge and hit in Next.js after on-demand revalidation',
},
)
const headers4ExplicitLocale = response4ExplicitLocale?.headers() || {}
expect(response4ExplicitLocale?.status()).toBe(200)
expect(headers4ExplicitLocale?.['x-nextjs-cache']).toBeUndefined()
// the page has now an updated date
const date4ExplicitLocale = await page.textContent('[data-testid="date-now"]')
expect(date4ExplicitLocale).not.toBe(date3ExplicitLocale)
// implicit and explicit locale paths should be the same (same cached response)
expect(date4ImplicitLocale).toBe(date4ExplicitLocale)
// check json route
const response4Json = await pollUntilHeadersMatch(
new URL(`base/path/_next/data/build-id/en${pagePath}.json`, pageRouterBasePathI18n.url)
.href,
{
headersToMatch: {
// revalidate refreshes Next cache, but not CDN cache
// so our request after revalidation means that Next cache is already
// warmed up with fresh response, but CDN cache just knows that previously
// cached response is stale, so we are hitting our function that serve
// already cached response
'cache-status': [/"Next.js"; hit/m, /"Netlify Edge"; fwd=(miss|stale)/m],
},
headersNotMatchedMessage:
'Fourth request to tested page (data) should be a miss or stale on the Edge and hit in Next.js after on-demand revalidation',
},
)
const headers4Json = response4Json?.headers() || {}
expect(response4Json?.status()).toBe(200)
expect(headers4Json['x-nextjs-cache']).toBeUndefined()
expect(headers4Json['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const data4 = (await response4Json?.json()) || {}
expect(data4?.pageProps?.time).toBe(date4ImplicitLocale)
})
test('non-default locale', async ({
page,
pollUntilHeadersMatch,
pageRouterBasePathI18n,
}) => {
// in case there is retry or some other test did hit that path before
// we want to make sure that cdn cache is not warmed up
const purgeCdnCache = await page.goto(
new URL(`/base/path/api/purge-cdn?path=/de${pagePath}`, pageRouterBasePathI18n.url)
.href,
)
expect(purgeCdnCache?.status()).toBe(200)
// wait a bit until cdn cache purge propagates
await page.waitForTimeout(500)
const response1 = await pollUntilHeadersMatch(
new URL(`/base/path/de${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// either first time hitting this route or we invalidated
// just CDN node in earlier step
// we will invoke function and see Next cache hit status
// in the response because it was prerendered at build time
// or regenerated in previous attempt to run this test
'cache-status': [
/"Netlify Edge"; fwd=(miss|stale)/m,
prerendered ? /"Next.js"; hit/m : /"Next.js"; (hit|fwd=miss)/m,
],
},
headersNotMatchedMessage:
'First request to tested page (html) should be a miss or stale on the Edge and hit in Next.js',
},
)
const headers1 = response1?.headers() || {}
expect(response1?.status()).toBe(200)
expect(headers1['x-nextjs-cache']).toBeUndefined()
expect(headers1['netlify-cache-tag']).toBe(`_n_t_/de${encodeURI(pagePath).toLowerCase()}`)
expect(headers1['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const date1 = await page.textContent('[data-testid="date-now"]')
const h1 = await page.textContent('h1')
expect(h1).toBe(expectedH1Content)
// check json route
const response1Json = await pollUntilHeadersMatch(
new URL(`base/path/_next/data/build-id/de${pagePath}.json`, pageRouterBasePathI18n.url)
.href,
{
headersToMatch: {
// either first time hitting this route or we invalidated
// just CDN node in earlier step
// we will invoke function and see Next cache hit status \
// in the response because it was prerendered at build time
// or regenerated in previous attempt to run this test
'cache-status': [/"Netlify Edge"; fwd=(miss|stale)/m, /"Next.js"; hit/m],
},
headersNotMatchedMessage:
'First request to tested page (data) should be a miss or stale on the Edge and hit in Next.js',
},
)
const headers1Json = response1Json?.headers() || {}
expect(response1Json?.status()).toBe(200)
expect(headers1Json['x-nextjs-cache']).toBeUndefined()
expect(headers1Json['netlify-cache-tag']).toBe(
`_n_t_/de${encodeURI(pagePath).toLowerCase()}`,
)
expect(headers1Json['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
const data1 = (await response1Json?.json()) || {}
expect(data1?.pageProps?.time).toBe(date1)
const response2 = await pollUntilHeadersMatch(
new URL(`base/path/de${pagePath}`, pageRouterBasePathI18n.url).href,
{
headersToMatch: {
// we are hitting the same page again and we most likely will see
// CDN hit (in this case Next reported cache status is omitted
// as it didn't actually take place in handling this request)
// or we will see CDN miss because different CDN node handled request
'cache-status': /"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m,
},
headersNotMatchedMessage:
'Second request to tested page (html) should most likely be a hit on the Edge (optionally miss or stale if different CDN node)',
},
)
const headers2 = response2?.headers() || {}
expect(response2?.status()).toBe(200)
expect(headers2['x-nextjs-cache']).toBeUndefined()
if (!headers2['cache-status'].includes('"Netlify Edge"; hit')) {
// if we missed CDN cache, we will see Next cache hit status
// as we reuse cached response
expect(headers2['cache-status']).toMatch(/"Next.js"; hit/m)
}
expect(headers2['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)
// the page is cached
const date2 = await page.textContent('[data-testid="date-now"]')
expect(date2).toBe(date1)
// check json route
const response2Json = await pollUntilHeadersMatch(
new URL(`base/path/_next/data/build-id/de${pagePath}.json`, pageRouterBasePathI18n.url)
.href,
{
headersToMatch: {
// we are hitting the same page again and we most likely will see
// CDN hit (in this case Next reported cache status is omitted
// as it didn't actually take place in handling this request)
// or we will see CDN miss because different CDN node handled request
'cache-status': /"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m,
},
headersNotMatchedMessage:
'Second request to tested page (data) should most likely be a hit on the Edge (optionally miss or stale if different CDN node)',
},
)
const headers2Json = response2Json?.headers() || {}
expect(response2Json?.status()).toBe(200)
expect(headers2Json['x-nextjs-cache']).toBeUndefined()
if (!headers2Json['cache-status'].includes('"Netlify Edge"; hit')) {