From dd43ce8491f6642cdbb09d4a0d4bccaf29235ef5 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Mon, 24 Jun 2024 16:21:08 +0200 Subject: [PATCH] test: add a case for not awaited res.revalidate --- tests/e2e/page-router.test.ts | 55 ++++++++++++++----- .../pages/api/revalidate-no-await.js | 12 ++++ .../pages/api/revalidate-no-await.js | 12 ++++ 3 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 tests/fixtures/page-router-base-path-i18n/pages/api/revalidate-no-await.js create mode 100644 tests/fixtures/page-router/pages/api/revalidate-no-await.js diff --git a/tests/e2e/page-router.test.ts b/tests/e2e/page-router.test.ts index 09f9e3f2ad..a92f0cbcbb 100644 --- a/tests/e2e/page-router.test.ts +++ b/tests/e2e/page-router.test.ts @@ -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 @@ -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) @@ -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 }) => { @@ -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) @@ -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) @@ -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) diff --git a/tests/fixtures/page-router-base-path-i18n/pages/api/revalidate-no-await.js b/tests/fixtures/page-router-base-path-i18n/pages/api/revalidate-no-await.js new file mode 100644 index 0000000000..253fed3637 --- /dev/null +++ b/tests/fixtures/page-router-base-path-i18n/pages/api/revalidate-no-await.js @@ -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 }) + } +} diff --git a/tests/fixtures/page-router/pages/api/revalidate-no-await.js b/tests/fixtures/page-router/pages/api/revalidate-no-await.js new file mode 100644 index 0000000000..253fed3637 --- /dev/null +++ b/tests/fixtures/page-router/pages/api/revalidate-no-await.js @@ -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 }) + } +}