Unverified Commit 923fdcd2 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update to not trigger revalidation during prefetch (#37201)

Continuation of https://github.com/vercel/next.js/pull/34498 this updates to send a `purpose: prefetch` header (related [w3c discussion](https://github.com/w3c/resource-hints/issues/74)) when prefetching data routes and then on the server we skip revalidating when this header is set. 

When a client-transition is actually made we send a background HEAD (non-blocking) request to the data route without the `purpose: prefetch` header which signals a revalidation should occur if the data is stale.  

This helps alleviate the number of revalidations occurring currently caused by prefetches as a path can be prefetched but not visited during a session so may not need to be revalidated yet. 

Fixes: https://github.com/vercel/next.js/issues/17758
x-ref: https://github.com/vercel/next.js/discussions/20521
x-ref: [slack thread](https://vercel.slack.com/archives/C031QM96T0A/p1645129445819139?thread_ts=1645124478.067049&cid=C031QM96T0A)
parent db805ced
...@@ -1616,6 +1616,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1616,6 +1616,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}, },
{ {
isManualRevalidate, isManualRevalidate,
isPrefetch: req.headers.purpose === 'prefetch',
} }
) )
......
...@@ -99,6 +99,7 @@ export default class ResponseCache { ...@@ -99,6 +99,7 @@ export default class ResponseCache {
responseGenerator: ResponseGenerator, responseGenerator: ResponseGenerator,
context: { context: {
isManualRevalidate?: boolean isManualRevalidate?: boolean
isPrefetch?: boolean
} }
): Promise<ResponseCacheEntry | null> { ): Promise<ResponseCacheEntry | null> {
// ensure manual revalidate doesn't block normal requests // ensure manual revalidate doesn't block normal requests
...@@ -175,7 +176,7 @@ export default class ResponseCache { ...@@ -175,7 +176,7 @@ export default class ResponseCache {
} }
: cachedResponse.value, : cachedResponse.value,
}) })
if (!cachedResponse.isStale) { if (!cachedResponse.isStale || context.isPrefetch) {
// The cached value is still valid, so we don't need // The cached value is still valid, so we don't need
// to update it yet. // to update it yet.
return return
......
...@@ -533,7 +533,7 @@ const SSG_DATA_NOT_FOUND = Symbol('SSG_DATA_NOT_FOUND') ...@@ -533,7 +533,7 @@ const SSG_DATA_NOT_FOUND = Symbol('SSG_DATA_NOT_FOUND')
function fetchRetry( function fetchRetry(
url: string, url: string,
attempts: number, attempts: number,
opts: { text?: boolean } opts: { text?: boolean; isPrefetch?: boolean; method?: string }
): Promise<any> { ): Promise<any> {
return fetch(url, { return fetch(url, {
// Cookies are required to be present for Next.js' SSG "Preview Mode". // Cookies are required to be present for Next.js' SSG "Preview Mode".
...@@ -548,6 +548,12 @@ function fetchRetry( ...@@ -548,6 +548,12 @@ function fetchRetry(
// > option instead of relying on the default. // > option instead of relying on the default.
// https://github.com/github/fetch#caveats // https://github.com/github/fetch#caveats
credentials: 'same-origin', credentials: 'same-origin',
method: opts.method || 'GET',
headers: opts.isPrefetch
? {
purpose: 'prefetch',
}
: {},
}).then((res) => { }).then((res) => {
if (!res.ok) { if (!res.ok) {
if (attempts > 1 && res.status >= 500) { if (attempts > 1 && res.status >= 500) {
...@@ -563,27 +569,30 @@ function fetchRetry( ...@@ -563,27 +569,30 @@ function fetchRetry(
} }
throw new Error(`Failed to load static props`) throw new Error(`Failed to load static props`)
} }
if (opts.method !== 'HEAD') {
return opts.text ? res.text() : res.json() return opts.text ? res.text() : res.json()
}
}) })
} }
const backgroundCache: Record<string, Promise<any>> = {}
function fetchNextData( function fetchNextData(
dataHref: string, dataHref: string,
isServerRender: boolean, isServerRender: boolean,
text: boolean | undefined, text: boolean | undefined,
inflightCache: NextDataCache, inflightCache: NextDataCache,
persistCache: boolean persistCache: boolean,
isPrefetch: boolean
) { ) {
const { href: cacheKey } = new URL(dataHref, window.location.href) const { href: cacheKey } = new URL(dataHref, window.location.href)
const getData = (background = false) =>
if (inflightCache[cacheKey] !== undefined) { fetchRetry(dataHref, isServerRender ? 3 : 1, {
return inflightCache[cacheKey] text,
} isPrefetch,
return (inflightCache[cacheKey] = fetchRetry( method: background ? 'HEAD' : 'GET',
dataHref, })
isServerRender ? 3 : 1,
{ text }
)
.catch((err: Error) => { .catch((err: Error) => {
// We should only trigger a server-side transition if this was caused // We should only trigger a server-side transition if this was caused
// on a client-side transition. Otherwise, we'd get into an infinite // on a client-side transition. Otherwise, we'd get into an infinite
...@@ -603,7 +612,21 @@ function fetchNextData( ...@@ -603,7 +612,21 @@ function fetchNextData(
.catch((err) => { .catch((err) => {
delete inflightCache[cacheKey] delete inflightCache[cacheKey]
throw err throw err
})) })
if (inflightCache[cacheKey] !== undefined) {
// we kick off a HEAD request in the background
// when a non-prefetch request is made to signal revalidation
if (!isPrefetch && persistCache && !backgroundCache[cacheKey]) {
backgroundCache[cacheKey] = getData(true)
.catch(() => {})
.then(() => {
delete backgroundCache[cacheKey]
})
}
return inflightCache[cacheKey]
}
return (inflightCache[cacheKey] = getData())
} }
interface NextDataCache { interface NextDataCache {
...@@ -1626,7 +1649,8 @@ export default class Router implements BaseRouter { ...@@ -1626,7 +1649,8 @@ export default class Router implements BaseRouter {
this.isSsr, this.isSsr,
false, false,
__N_SSG ? this.sdc : this.sdr, __N_SSG ? this.sdc : this.sdr,
!!__N_SSG && !isPreview !!__N_SSG && !isPreview,
false
) )
: this.getInitialProps( : this.getInitialProps(
Component, Component,
...@@ -1860,6 +1884,7 @@ export default class Router implements BaseRouter { ...@@ -1860,6 +1884,7 @@ export default class Router implements BaseRouter {
false, false,
false, // text false, // text
this.sdc, this.sdc,
!this.isPreview,
true true
) )
: false : false
...@@ -1924,7 +1949,7 @@ export default class Router implements BaseRouter { ...@@ -1924,7 +1949,7 @@ export default class Router implements BaseRouter {
_getFlightData(dataHref: string): Promise<object> { _getFlightData(dataHref: string): Promise<object> {
// Do not cache RSC flight response since it's not a static resource // Do not cache RSC flight response since it's not a static resource
return fetchNextData(dataHref, true, true, this.sdc, false).then( return fetchNextData(dataHref, true, true, this.sdc, false, false).then(
(serialized) => { (serialized) => {
return { data: serialized } return { data: serialized }
} }
......
...@@ -67,7 +67,7 @@ describe('Prefetching Links in viewport', () => { ...@@ -67,7 +67,7 @@ describe('Prefetching Links in viewport', () => {
expect( expect(
nextDataRequests.filter((reqUrl) => reqUrl.includes('/ssg/slow.json')) nextDataRequests.filter((reqUrl) => reqUrl.includes('/ssg/slow.json'))
.length .length
).toBe(1) ).toBe(2)
}) })
it('should handle timed out prefetch correctly', async () => { it('should handle timed out prefetch correctly', async () => {
......
export default function Page(props) {
return (
<>
<p id="page">blog/[slug]</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
export function getStaticProps({ params }) {
console.log('revalidating /blog', params.slug)
return {
props: {
params,
now: Date.now(),
},
revalidate: 2,
}
}
export function getStaticPaths() {
return {
paths: ['/blog/first', '/blog/second'],
fallback: false,
}
}
import Link from 'next/link'
export default function Page(props) {
return (
<>
<p id="page">index</p>
<p id="props">{JSON.stringify(props)}</p>
<Link href="/blog/first">
<a id="to-blog-first">/blog/first</a>
</Link>
<br />
<Link href="/blog/second">
<a id="to-blog-second">/blog/second</a>
</Link>
<br />
</>
)
}
export function getStaticProps() {
console.log('revalidating /')
return {
props: {
now: Date.now(),
},
revalidate: 1,
}
}
import { NextInstance } from 'test/lib/next-modes/base'
import { createNext, FileRef } from 'e2e-utils'
import { check, fetchViaHTTP, waitFor } from 'next-test-utils'
import cheerio from 'cheerio'
import { join } from 'path'
import webdriver from 'next-webdriver'
import assert from 'assert'
describe('Prerender prefetch', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'app/pages')),
},
dependencies: {},
})
})
afterAll(() => next.destroy())
it('should not revalidate during prefetching', async () => {
const reqs = {}
// get initial values
for (const path of ['/blog/first', '/blog/second']) {
const res = await fetchViaHTTP(next.url, path)
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
const props = JSON.parse($('#props').text())
reqs[path] = props
}
const browser = await webdriver(next.url, '/')
// wait for prefetch to occur
await check(async () => {
const cache = await browser.eval('JSON.stringify(window.next.router.sdc)')
return cache.includes('/blog/first') && cache.includes('/blog/second')
? 'success'
: cache
}, 'success')
await waitFor(3000)
await browser.refresh()
// reload after revalidate period and wait for prefetch again
await check(async () => {
const cache = await browser.eval('JSON.stringify(window.next.router.sdc)')
return cache.includes('/blog/first') && cache.includes('/blog/second')
? 'success'
: cache
}, 'success')
// ensure revalidate did not occur from prefetch
for (const path of ['/blog/first', '/blog/second']) {
const res = await fetchViaHTTP(next.url, path)
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
const props = JSON.parse($('#props').text())
expect(props).toEqual(reqs[path])
}
})
it('should trigger revalidation after navigation', async () => {
const getData = () =>
fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/blog/first.json`,
undefined,
{
headers: {
purpose: 'prefetch',
},
}
)
const initialDataRes = await getData()
const initialData = await initialDataRes.json()
const browser = await webdriver(next.url, '/')
await browser.elementByCss('#to-blog-first').click()
await check(async () => {
const data = await getData()
assert.notDeepEqual(initialData, data)
return 'success'
}, 'success')
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment