From b6b44b0986f35d81efbcf75723f3bed794b41d05 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 26 Jul 2022 11:11:43 -0500 Subject: [PATCH 1/2] Update to use HEAD request for On-Demand ISR --- packages/next/server/api-utils/node.ts | 1 + test/e2e/prerender.test.ts | 106 +++++++++++++------------ 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/packages/next/server/api-utils/node.ts b/packages/next/server/api-utils/node.ts index 01476fa422ea..e86cca14e05b 100644 --- a/packages/next/server/api-utils/node.ts +++ b/packages/next/server/api-utils/node.ts @@ -316,6 +316,7 @@ async function revalidate( try { if (context.trustHostHeader) { const res = await fetch(`https://${req.headers.host}${urlPath}`, { + method: 'HEAD', headers: { ...revalidateHeaders, cookie: req.headers.cookie || '', diff --git a/test/e2e/prerender.test.ts b/test/e2e/prerender.test.ts index c7753e0f9768..695fec8928e1 100644 --- a/test/e2e/prerender.test.ts +++ b/test/e2e/prerender.test.ts @@ -2002,6 +2002,62 @@ describe('Prerender', () => { }) } + if (!isDev) { + it('should handle manual revalidate for fallback: blocking', async () => { + const beforeRevalidate = Date.now() + const res = await fetchViaHTTP( + next.url, + '/blocking-fallback/test-manual-1' + ) + await waitForCacheWrite( + '/blocking-fallback/test-manual-1', + beforeRevalidate + ) + const html = await res.text() + const $ = cheerio.load(html) + const initialTime = $('#time').text() + + expect(res.headers.get('x-nextjs-cache')).toMatch(/MISS/) + expect($('p').text()).toMatch(/Post:.*?test-manual-1/) + + const res2 = await fetchViaHTTP( + next.url, + '/blocking-fallback/test-manual-1' + ) + const html2 = await res2.text() + const $2 = cheerio.load(html2) + const cacheHeader = isDeploy ? 'x-vercel-cache' : 'x-nextjs-cache' + + expect(res2.headers.get(cacheHeader)).toMatch(/(HIT|STALE)/) + expect(initialTime).toBe($2('#time').text()) + + const res3 = await fetchViaHTTP( + next.url, + '/api/manual-revalidate', + { + pathname: '/blocking-fallback/test-manual-1', + }, + { redirect: 'manual' } + ) + + expect(res3.status).toBe(200) + const revalidateData = await res3.json() + expect(revalidateData.revalidated).toBe(true) + + await check(async () => { + const res4 = await fetchViaHTTP( + next.url, + '/blocking-fallback/test-manual-1' + ) + const html4 = await res4.text() + const $4 = cheerio.load(html4) + expect($4('#time').text()).not.toBe(initialTime) + expect(res4.headers.get(cacheHeader)).toMatch(/(HIT|STALE)/) + return 'success' + }, 'success') + }) + } + if (!isDev && !isDeploy) { it('should automatically reset cache TTL when an error occurs and build cache was available', async () => { await next.patchFile('error.txt', 'yes') @@ -2056,56 +2112,6 @@ describe('Prerender', () => { ) }) - it('should handle manual revalidate for fallback: blocking', async () => { - const beforeRevalidate = Date.now() - const res = await fetchViaHTTP( - next.url, - '/blocking-fallback/test-manual-1' - ) - await waitForCacheWrite( - '/blocking-fallback/test-manual-1', - beforeRevalidate - ) - const html = await res.text() - const $ = cheerio.load(html) - const initialTime = $('#time').text() - - expect(res.headers.get('x-nextjs-cache')).toMatch(/MISS/) - expect($('p').text()).toMatch(/Post:.*?test-manual-1/) - - const res2 = await fetchViaHTTP( - next.url, - '/blocking-fallback/test-manual-1' - ) - const html2 = await res2.text() - const $2 = cheerio.load(html2) - - expect(res2.headers.get('x-nextjs-cache')).toMatch(/(HIT|STALE)/) - expect(initialTime).toBe($2('#time').text()) - - const res3 = await fetchViaHTTP( - next.url, - '/api/manual-revalidate', - { - pathname: '/blocking-fallback/test-manual-1', - }, - { redirect: 'manual' } - ) - - expect(res3.status).toBe(200) - const revalidateData = await res3.json() - expect(revalidateData.revalidated).toBe(true) - - const res4 = await fetchViaHTTP( - next.url, - '/blocking-fallback/test-manual-1' - ) - const html4 = await res4.text() - const $4 = cheerio.load(html4) - expect($4('#time').text()).not.toBe(initialTime) - expect(res4.headers.get('x-nextjs-cache')).toMatch(/(HIT|STALE)/) - }) - it('should not manual revalidate for fallback: blocking with onlyGenerated if not generated', async () => { const res = await fetchViaHTTP( next.url, From 9291d5b9d835a2d183c0e0fdf034079ba349f61a Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 26 Jul 2022 14:55:25 -0500 Subject: [PATCH 2/2] update test for deploy --- test/e2e/prerender.test.ts | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/test/e2e/prerender.test.ts b/test/e2e/prerender.test.ts index 695fec8928e1..ed06c391e93e 100644 --- a/test/e2e/prerender.test.ts +++ b/test/e2e/prerender.test.ts @@ -2009,27 +2009,32 @@ describe('Prerender', () => { next.url, '/blocking-fallback/test-manual-1' ) - await waitForCacheWrite( - '/blocking-fallback/test-manual-1', - beforeRevalidate - ) + + if (!isDeploy) { + await waitForCacheWrite( + '/blocking-fallback/test-manual-1', + beforeRevalidate + ) + } const html = await res.text() const $ = cheerio.load(html) const initialTime = $('#time').text() + const cacheHeader = isDeploy ? 'x-vercel-cache' : 'x-nextjs-cache' - expect(res.headers.get('x-nextjs-cache')).toMatch(/MISS/) + expect(res.headers.get(cacheHeader)).toMatch(/MISS/) expect($('p').text()).toMatch(/Post:.*?test-manual-1/) - const res2 = await fetchViaHTTP( - next.url, - '/blocking-fallback/test-manual-1' - ) - const html2 = await res2.text() - const $2 = cheerio.load(html2) - const cacheHeader = isDeploy ? 'x-vercel-cache' : 'x-nextjs-cache' + if (!isDeploy) { + const res2 = await fetchViaHTTP( + next.url, + '/blocking-fallback/test-manual-1' + ) + const html2 = await res2.text() + const $2 = cheerio.load(html2) - expect(res2.headers.get(cacheHeader)).toMatch(/(HIT|STALE)/) - expect(initialTime).toBe($2('#time').text()) + expect(res2.headers.get(cacheHeader)).toMatch(/(HIT|STALE)/) + expect(initialTime).toBe($2('#time').text()) + } const res3 = await fetchViaHTTP( next.url,