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
f9b281a8
Unverified
Commit
f9b281a8
authored
Oct 25, 2024
by
Hendrik Liebau
Committed by
GitHub
Oct 25, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix fetch caching inside of `"use cache"` (#71793)
parent
bf5ffaf2
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
67 additions
and
3 deletions
+67
-3
base-server.ts
packages/next/src/server/base-server.ts
+7
-1
patch-fetch.ts
packages/next/src/server/lib/patch-fetch.ts
+6
-2
page.tsx
test/e2e/app-dir/use-cache/app/cache-fetch/page.tsx
+18
-0
page.tsx
test/e2e/app-dir/use-cache/app/fetch-revalidate/page.tsx
+18
-0
use-cache.test.ts
test/e2e/app-dir/use-cache/use-cache.test.ts
+18
-0
No files found.
packages/next/src/server/base-server.ts
View file @
f9b281a8
...
...
@@ -3103,9 +3103,15 @@ export default abstract class Server<
)
}
return
runWithCacheScope
({
cache
},
()
=>
const
response
=
runWithCacheScope
({
cache
},
()
=>
originalResponseGenerator
(
state
)
)
// Clear the prefetch cache to ensure a clean slate for the next
// request.
this
.
prefetchCacheScopesDev
.
del
(
urlPathname
)
return
response
}
return
originalResponseGenerator
(
state
)
...
...
packages/next/src/server/lib/patch-fetch.ts
View file @
f9b281a8
...
...
@@ -278,11 +278,15 @@ export function createPatchedFetcher(
?
[]
:
workUnitStore
.
implicitTags
// Inside unstable-cache we treat it the same as force-no-store on the page.
// Inside unstable-cache or "use cache", we treat it the same as
// force-no-store on the page.
const
pageFetchCacheMode
=
workUnitStore
&&
workUnitStore
.
type
===
'unstable-cache'
workUnitStore
&&
(
workUnitStore
.
type
===
'unstable-cache'
||
workUnitStore
.
type
===
'cache'
)
?
'force-no-store'
:
workStore
.
fetchCache
const
isUsingNoStore
=
!!
workStore
.
isUnstableNoStore
let
currentFetchCacheConfig
=
getRequestMeta
(
'cache'
)
...
...
test/e2e/app-dir/use-cache/app/cache-fetch/page.tsx
0 → 100644
View file @
f9b281a8
import
React
from
'react'
async
function
getData
()
{
'use cache'
return
fetch
(
'https://next-data-api-endpoint.vercel.app/api/random'
).
then
(
(
res
)
=>
res
.
text
()
)
}
export
default
async
function
Page
()
{
return
(
<>
<
p
>
index page
</
p
>
<
p
id=
"random"
>
{
await
getData
()
}
</
p
>
</>
)
}
test/e2e/app-dir/use-cache/app/fetch-revalidate/page.tsx
0 → 100644
View file @
f9b281a8
import
React
from
'react'
async
function
getData
()
{
'use cache'
return
fetch
(
'https://next-data-api-endpoint.vercel.app/api/random'
,
{
next
:
{
revalidate
:
0
},
}).
then
((
res
)
=>
res
.
text
())
}
export
default
async
function
Page
()
{
return
(
<>
<
p
>
index page
</
p
>
<
p
id=
"random"
>
{
await
getData
()
}
</
p
>
</>
)
}
test/e2e/app-dir/use-cache/use-cache.test.ts
View file @
f9b281a8
...
...
@@ -267,6 +267,24 @@ describe('use-cache', () => {
// expect(time4).toBe(time3);
})
it
(
'should use revalidate config in fetch'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/fetch-revalidate'
)
const
initialValue
=
await
browser
.
elementByCss
(
'#random'
).
text
()
await
browser
.
refresh
()
expect
(
await
browser
.
elementByCss
(
'#random'
).
text
()).
not
.
toBe
(
initialValue
)
})
it
(
'should cache fetch without no-store'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/cache-fetch'
)
const
initialValue
=
await
browser
.
elementByCss
(
'#random'
).
text
()
await
browser
.
refresh
()
expect
(
await
browser
.
elementByCss
(
'#random'
).
text
()).
toBe
(
initialValue
)
})
it
(
'should override fetch with no-store in use cache properly'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/cache-fetch-no-store'
)
...
...
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