Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
b434ea40
Unverified
Commit
b434ea40
authored
Mar 29, 2024
by
JJ Kasper
Committed by
GitHub
Mar 29, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure we dedupe fetch requests properly during page revalidate (#63849)
parent
749c8e72
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
11 deletions
+51
-11
patch-fetch.ts
packages/next/src/server/lib/patch-fetch.ts
+35
-3
app-static.test.ts
test/e2e/app-dir/app-static/app-static.test.ts
+3
-0
page.js
...r/app-static/app/variable-revalidate/revalidate-3/page.js
+13
-8
No files found.
packages/next/src/server/lib/patch-fetch.ts
View file @
b434ea40
...
...
@@ -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
)
}
}
)
}
...
...
test/e2e/app-dir/app-static/app-static.test.ts
View file @
b434ea40
...
...
@@ -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'
)
...
...
test/e2e/app-dir/app-static/app/variable-revalidate/revalidate-3/page.js
View file @
b434ea40
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
>
<
/
>
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment