From 881c313b39dc2b975d6c5da89f223a6b2ca6c541 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 26 Jul 2022 15:18:38 -0500 Subject: [PATCH] Update to use HEAD request for On-Demand ISR (#39038) * Update to use HEAD request for On-Demand ISR * update test for deploy --- packages/next/server/api-utils/node.ts | 1 + test/e2e/prerender.test.ts | 111 ++++++++++++++----------- 2 files changed, 62 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..ed06c391e93e 100644 --- a/test/e2e/prerender.test.ts +++ b/test/e2e/prerender.test.ts @@ -2002,6 +2002,67 @@ 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' + ) + + 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(cacheHeader)).toMatch(/MISS/) + expect($('p').text()).toMatch(/Post:.*?test-manual-1/) + + 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()) + } + + 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 +2117,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,