Unverified Commit b434ea40 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure we dedupe fetch requests properly during page revalidate (#63849)

parent 749c8e72
......@@ -558,6 +558,7 @@ export function patchFetch({
let handleUnlock = () => Promise.resolve()
let cacheReasonOverride
let isForegroundRevalidate = false
if (cacheKey && staticGenerationStore.incrementalCache) {
handleUnlock = await staticGenerationStore.incrementalCache.lock(
......@@ -585,12 +586,21 @@ export function patchFetch({
if (entry?.value && entry.value.kind === 'FETCH') {
// when stale and is revalidating we wait for fresh data
// so the revalidated entry has the updated data
if (!(staticGenerationStore.isRevalidate && entry.isStale)) {
if (staticGenerationStore.isRevalidate && entry.isStale) {
isForegroundRevalidate = true
} else {
if (entry.isStale) {
staticGenerationStore.pendingRevalidates ??= {}
if (!staticGenerationStore.pendingRevalidates[cacheKey]) {
staticGenerationStore.pendingRevalidates[cacheKey] =
doOriginalFetch(true).catch(console.error)
doOriginalFetch(true)
.catch(console.error)
.finally(() => {
staticGenerationStore.pendingRevalidates ??= {}
delete staticGenerationStore.pendingRevalidates[
cacheKey || ''
]
})
}
}
const resData = entry.value.data
......@@ -682,7 +692,29 @@ export function patchFetch({
if (hasNextConfig) delete init.next
}
return doOriginalFetch(false, cacheReasonOverride).finally(handleUnlock)
// if we are revalidating the whole page via time or on-demand and
// the fetch cache entry is stale we should still de-dupe the
// origin hit if it's a cache-able entry
if (cacheKey && isForegroundRevalidate) {
staticGenerationStore.pendingRevalidates ??= {}
const pendingRevalidate =
staticGenerationStore.pendingRevalidates[cacheKey]
if (pendingRevalidate) {
const res: Response = await pendingRevalidate
return res.clone()
}
return (staticGenerationStore.pendingRevalidates[cacheKey] =
doOriginalFetch(true, cacheReasonOverride).finally(async () => {
staticGenerationStore.pendingRevalidates ??= {}
delete staticGenerationStore.pendingRevalidates[cacheKey || '']
await handleUnlock()
}))
} else {
return doOriginalFetch(false, cacheReasonOverride).finally(
handleUnlock
)
}
}
)
}
......
......@@ -2289,6 +2289,7 @@ createNextDescribe(
const layoutData = $('#layout-data').text()
const pageData = $('#page-data').text()
const pageData2 = $('#page-data-2').text()
const res2 = await fetchViaHTTP(
next.url,
......@@ -2300,6 +2301,8 @@ createNextDescribe(
expect($2('#layout-data').text()).toBe(layoutData)
expect($2('#page-data').text()).toBe(pageData)
expect($2('#page-data-2').text()).toBe(pageData2)
expect(pageData).toBe(pageData2)
return 'success'
}, 'success')
......
import { cache, use } from 'react'
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?page',
{
next: { revalidate: 3 },
}
).then((res) => res.text())
export default function Page() {
const getData = cache(() =>
fetch('https://next-data-api-endpoint.vercel.app/api/random?page', {
const data2 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?page',
{
next: { revalidate: 3 },
}).then((res) => res.text())
)
const dataPromise = getData()
const data = use(dataPromise)
}
).then((res) => res.text())
return (
<>
<p id="page">/variable-revalidate/revalidate-3</p>
<p id="page-data">revalidate 3: {data}</p>
<p id="page-data-2">revalidate 3: {data2}</p>
<p id="now">{Date.now()}</p>
</>
)
......
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