Skip to content

Commit

Permalink
test: add a case for not awaited res.revalidate
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jun 24, 2024
1 parent 08f666d commit dd43ce8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
55 changes: 41 additions & 14 deletions tests/e2e/page-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,35 @@ export async function check(

test.describe('Simple Page Router (no basePath, no i18n)', () => {
test.describe('On-demand revalidate works correctly', () => {
for (const { label, prerendered, pagePath, expectedH1Content } of [
for (const { label, prerendered, pagePath, revalidateApiBasePath, expectedH1Content } of [
{
label: 'prerendered page with static path',
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',
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',
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',
},
]) {
test(label, async ({ page, pollUntilHeadersMatch, pageRouter }) => {
// in case there is retry or some other test did hit that path before
Expand Down Expand Up @@ -192,7 +202,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
expect(data2?.pageProps?.time).toBe(date1)

const revalidate = await page.goto(
new URL(`/api/revalidate?path=${pagePath}`, pageRouter.url).href,
new URL(`${revalidateApiBasePath}?path=${pagePath}`, pageRouter.url).href,
)
expect(revalidate?.status()).toBe(200)

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

test.describe('Page Router with basePath and i18n', () => {
test.describe('Static revalidate works correctly', () => {
for (const { label, prerendered, pagePath, expectedH1Content } of [
for (const { label, prerendered, pagePath, revalidateApiBasePath, expectedH1Content } of [
{
label: 'prerendered page with static path',
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',
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',
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',
},
]) {
test.describe(label, () => {
test(`default locale`, async ({ page, pollUntilHeadersMatch, pageRouterBasePathI18n }) => {
Expand Down Expand Up @@ -622,7 +642,10 @@ test.describe('Page Router with basePath and i18n', () => {

// revalidate implicit locale path
const revalidateImplicit = await page.goto(
new URL(`/base/path/api/revalidate?path=${pagePath}`, pageRouterBasePathI18n.url).href,
new URL(
`/base/path${revalidateApiBasePath}?path=${pagePath}`,
pageRouterBasePathI18n.url,
).href,
)
expect(revalidateImplicit?.status()).toBe(200)

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

// revalidate implicit locale path
const revalidateExplicit = await page.goto(
new URL(`/base/path/api/revalidate?path=/en${pagePath}`, pageRouterBasePathI18n.url)
.href,
new URL(
`/base/path${revalidateApiBasePath}?path=/en${pagePath}`,
pageRouterBasePathI18n.url,
).href,
)
expect(revalidateExplicit?.status()).toBe(200)

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

const revalidate = await page.goto(
new URL(`/base/path/api/revalidate?path=/de${pagePath}`, pageRouterBasePathI18n.url)
.href,
new URL(
`/base/path${revalidateApiBasePath}?path=/de${pagePath}`,
pageRouterBasePathI18n.url,
).href,
)
expect(revalidate?.status()).toBe(200)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default async function handler(req, res) {
try {
const pathToPurge = req.query.path ?? '/static/revalidate-manual'
// res.revalidate returns a promise that can be awaited to wait for the revalidation to complete
// if user doesn't await it, we still want to ensure the revalidation is completed, so we internally track
// this as "background work" to ensure it completes before function suspends execution
res.revalidate(pathToPurge)
return res.json({ code: 200, message: 'success' })
} catch (err) {
return res.status(500).send({ code: 500, message: err.message })
}
}
12 changes: 12 additions & 0 deletions tests/fixtures/page-router/pages/api/revalidate-no-await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default async function handler(req, res) {
try {
const pathToPurge = req.query.path ?? '/static/revalidate-manual'
// res.revalidate returns a promise that can be awaited to wait for the revalidation to complete
// if user doesn't await it, we still want to ensure the revalidation is completed, so we internally track
// this as "background work" to ensure it completes before function suspends execution
res.revalidate(pathToPurge)
return res.json({ code: 200, message: 'success' })
} catch (err) {
return res.status(500).send({ code: 500, message: err.message })
}
}

0 comments on commit dd43ce8

Please sign in to comment.