Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dd43ce8

Browse files
committedJun 24, 2024·
test: add a case for not awaited res.revalidate
1 parent 08f666d commit dd43ce8

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed
 

‎tests/e2e/page-router.test.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,35 @@ export async function check(
5050

5151
test.describe('Simple Page Router (no basePath, no i18n)', () => {
5252
test.describe('On-demand revalidate works correctly', () => {
53-
for (const { label, prerendered, pagePath, expectedH1Content } of [
53+
for (const { label, prerendered, pagePath, revalidateApiBasePath, expectedH1Content } of [
5454
{
55-
label: 'prerendered page with static path',
55+
label: 'prerendered page with static path and awaited res.revalidate()',
5656
prerendered: true,
5757
pagePath: '/static/revalidate-manual',
58+
revalidateApiBasePath: '/api/revalidate',
5859
expectedH1Content: 'Show #71',
5960
},
6061
{
61-
label: 'prerendered page with dynamic path',
62+
label: 'prerendered page with dynamic path and awaited res.revalidate()',
6263
prerendered: true,
6364
pagePath: '/products/prerendered',
65+
revalidateApiBasePath: '/api/revalidate',
6466
expectedH1Content: 'Product prerendered',
6567
},
6668
{
67-
label: 'not prerendered page with dynamic path',
69+
label: 'not prerendered page with dynamic path and awaited res.revalidate()',
6870
prerendered: false,
6971
pagePath: '/products/not-prerendered',
72+
revalidateApiBasePath: '/api/revalidate',
7073
expectedH1Content: 'Product not-prerendered',
7174
},
75+
{
76+
label: 'not prerendered page with dynamic path and not awaited res.revalidate()',
77+
prerendered: false,
78+
pagePath: '/products/not-prerendered-and-not-awaited-revalidation',
79+
revalidateApiBasePath: '/api/revalidate-no-await',
80+
expectedH1Content: 'Product not-prerendered-and-not-awaited-revalidation',
81+
},
7282
]) {
7383
test(label, async ({ page, pollUntilHeadersMatch, pageRouter }) => {
7484
// in case there is retry or some other test did hit that path before
@@ -192,7 +202,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
192202
expect(data2?.pageProps?.time).toBe(date1)
193203

194204
const revalidate = await page.goto(
195-
new URL(`/api/revalidate?path=${pagePath}`, pageRouter.url).href,
205+
new URL(`${revalidateApiBasePath}?path=${pagePath}`, pageRouter.url).href,
196206
)
197207
expect(revalidate?.status()).toBe(200)
198208

@@ -411,25 +421,35 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
411421

412422
test.describe('Page Router with basePath and i18n', () => {
413423
test.describe('Static revalidate works correctly', () => {
414-
for (const { label, prerendered, pagePath, expectedH1Content } of [
424+
for (const { label, prerendered, pagePath, revalidateApiBasePath, expectedH1Content } of [
415425
{
416-
label: 'prerendered page with static path',
426+
label: 'prerendered page with static path and awaited res.revalidate()',
417427
prerendered: true,
418428
pagePath: '/static/revalidate-manual',
429+
revalidateApiBasePath: '/api/revalidate',
419430
expectedH1Content: 'Show #71',
420431
},
421432
{
422-
label: 'prerendered page with dynamic path',
433+
label: 'prerendered page with dynamic path and awaited res.revalidate()',
423434
prerendered: true,
424435
pagePath: '/products/prerendered',
436+
revalidateApiBasePath: '/api/revalidate',
425437
expectedH1Content: 'Product prerendered',
426438
},
427439
{
428-
label: 'not prerendered page with dynamic path',
440+
label: 'not prerendered page with dynamic path and awaited res.revalidate()',
429441
prerendered: false,
430442
pagePath: '/products/not-prerendered',
443+
revalidateApiBasePath: '/api/revalidate',
431444
expectedH1Content: 'Product not-prerendered',
432445
},
446+
{
447+
label: 'not prerendered page with dynamic path and not awaited res.revalidate()',
448+
prerendered: false,
449+
pagePath: '/products/not-prerendered-and-not-awaited-revalidation',
450+
revalidateApiBasePath: '/api/revalidate-no-await',
451+
expectedH1Content: 'Product not-prerendered-and-not-awaited-revalidation',
452+
},
433453
]) {
434454
test.describe(label, () => {
435455
test(`default locale`, async ({ page, pollUntilHeadersMatch, pageRouterBasePathI18n }) => {
@@ -622,7 +642,10 @@ test.describe('Page Router with basePath and i18n', () => {
622642

623643
// revalidate implicit locale path
624644
const revalidateImplicit = await page.goto(
625-
new URL(`/base/path/api/revalidate?path=${pagePath}`, pageRouterBasePathI18n.url).href,
645+
new URL(
646+
`/base/path${revalidateApiBasePath}?path=${pagePath}`,
647+
pageRouterBasePathI18n.url,
648+
).href,
626649
)
627650
expect(revalidateImplicit?.status()).toBe(200)
628651

@@ -713,8 +736,10 @@ test.describe('Page Router with basePath and i18n', () => {
713736

714737
// revalidate implicit locale path
715738
const revalidateExplicit = await page.goto(
716-
new URL(`/base/path/api/revalidate?path=/en${pagePath}`, pageRouterBasePathI18n.url)
717-
.href,
739+
new URL(
740+
`/base/path${revalidateApiBasePath}?path=/en${pagePath}`,
741+
pageRouterBasePathI18n.url,
742+
).href,
718743
)
719744
expect(revalidateExplicit?.status()).toBe(200)
720745

@@ -934,8 +959,10 @@ test.describe('Page Router with basePath and i18n', () => {
934959
expect(data2?.pageProps?.time).toBe(date1)
935960

936961
const revalidate = await page.goto(
937-
new URL(`/base/path/api/revalidate?path=/de${pagePath}`, pageRouterBasePathI18n.url)
938-
.href,
962+
new URL(
963+
`/base/path${revalidateApiBasePath}?path=/de${pagePath}`,
964+
pageRouterBasePathI18n.url,
965+
).href,
939966
)
940967
expect(revalidate?.status()).toBe(200)
941968

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default async function handler(req, res) {
2+
try {
3+
const pathToPurge = req.query.path ?? '/static/revalidate-manual'
4+
// res.revalidate returns a promise that can be awaited to wait for the revalidation to complete
5+
// if user doesn't await it, we still want to ensure the revalidation is completed, so we internally track
6+
// this as "background work" to ensure it completes before function suspends execution
7+
res.revalidate(pathToPurge)
8+
return res.json({ code: 200, message: 'success' })
9+
} catch (err) {
10+
return res.status(500).send({ code: 500, message: err.message })
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default async function handler(req, res) {
2+
try {
3+
const pathToPurge = req.query.path ?? '/static/revalidate-manual'
4+
// res.revalidate returns a promise that can be awaited to wait for the revalidation to complete
5+
// if user doesn't await it, we still want to ensure the revalidation is completed, so we internally track
6+
// this as "background work" to ensure it completes before function suspends execution
7+
res.revalidate(pathToPurge)
8+
return res.json({ code: 200, message: 'success' })
9+
} catch (err) {
10+
return res.status(500).send({ code: 500, message: err.message })
11+
}
12+
}

0 commit comments

Comments
 (0)
Please sign in to comment.