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
b0ae0b28
Unverified
Commit
b0ae0b28
authored
Feb 10, 2025
by
Hendrik Liebau
Committed by
GitHub
Feb 10, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Respect `no-store` request header for `"use cache"` entries in dev mode (#75754)
parent
8daa66b6
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
73 additions
and
8 deletions
+73
-8
work-async-storage.external.ts
...next/src/server/app-render/work-async-storage.external.ts
+1
-0
work-unit-async-storage.external.ts
...src/server/app-render/work-unit-async-storage.external.ts
+1
-0
work-store.ts
packages/next/src/server/async-storage/work-store.ts
+2
-0
use-cache-wrapper.ts
packages/next/src/server/use-cache/use-cache-wrapper.ts
+25
-4
layout.tsx
test/development/app-dir/use-cache-dev/app/layout.tsx
+0
-0
page.tsx
test/development/app-dir/use-cache-dev/app/page.tsx
+12
-3
next.config.js
test/development/app-dir/use-cache-dev/next.config.js
+0
-0
use-cache-dev.test.ts
test/development/app-dir/use-cache-dev/use-cache-dev.test.ts
+32
-1
No files found.
packages/next/src/server/app-render/work-async-storage.external.ts
View file @
b0ae0b28
...
@@ -73,6 +73,7 @@ export interface WorkStore {
...
@@ -73,6 +73,7 @@ export interface WorkStore {
rootParams
:
Params
rootParams
:
Params
dynamicIOEnabled
:
boolean
dynamicIOEnabled
:
boolean
dev
:
boolean
}
}
export
type
WorkAsyncStorage
=
AsyncLocalStorage
<
WorkStore
>
export
type
WorkAsyncStorage
=
AsyncLocalStorage
<
WorkStore
>
...
...
packages/next/src/server/app-render/work-unit-async-storage.external.ts
View file @
b0ae0b28
...
@@ -163,6 +163,7 @@ export type UseCacheStore = {
...
@@ -163,6 +163,7 @@ export type UseCacheStore = {
readonly
hmrRefreshHash
:
string
|
undefined
readonly
hmrRefreshHash
:
string
|
undefined
readonly
isHmrRefresh
:
boolean
readonly
isHmrRefresh
:
boolean
readonly
serverComponentsHmrCache
:
ServerComponentsHmrCache
|
undefined
readonly
serverComponentsHmrCache
:
ServerComponentsHmrCache
|
undefined
readonly
forceRevalidate
:
boolean
}
&
PhasePartial
}
&
PhasePartial
export
type
UnstableCacheStore
=
{
export
type
UnstableCacheStore
=
{
...
...
packages/next/src/server/async-storage/work-store.ts
View file @
b0ae0b28
...
@@ -60,6 +60,7 @@ export type WorkStoreContext = {
...
@@ -60,6 +60,7 @@ export type WorkStoreContext = {
|
'nextExport'
|
'nextExport'
|
'isDraftMode'
|
'isDraftMode'
|
'isDebugDynamicAccesses'
|
'isDebugDynamicAccesses'
|
'dev'
>
&
>
&
RequestLifecycleOpts
&
RequestLifecycleOpts
&
Partial
<
Pick
<
RenderOpts
,
'reactLoadableManifest'
>>
Partial
<
Pick
<
RenderOpts
,
'reactLoadableManifest'
>>
...
@@ -127,6 +128,7 @@ export function createWorkStore({
...
@@ -127,6 +128,7 @@ export function createWorkStore({
afterContext
:
createAfterContext
(
renderOpts
),
afterContext
:
createAfterContext
(
renderOpts
),
dynamicIOEnabled
:
renderOpts
.
experimental
.
dynamicIO
,
dynamicIOEnabled
:
renderOpts
.
experimental
.
dynamicIO
,
dev
:
renderOpts
.
dev
??
false
,
}
}
// TODO: remove this when we resolve accessing the store outside the execution context
// TODO: remove this when we resolve accessing the store outside the execution context
...
...
packages/next/src/server/use-cache/use-cache-wrapper.ts
View file @
b0ae0b28
...
@@ -168,6 +168,7 @@ function generateCacheEntryWithCacheContext(
...
@@ -168,6 +168,7 @@ function generateCacheEntryWithCacheContext(
hmrRefreshHash
:
outerWorkUnitStore
&&
getHmrRefreshHash
(
outerWorkUnitStore
),
hmrRefreshHash
:
outerWorkUnitStore
&&
getHmrRefreshHash
(
outerWorkUnitStore
),
isHmrRefresh
:
useCacheOrRequestStore
?.
isHmrRefresh
??
false
,
isHmrRefresh
:
useCacheOrRequestStore
?.
isHmrRefresh
??
false
,
serverComponentsHmrCache
:
useCacheOrRequestStore
?.
serverComponentsHmrCache
,
serverComponentsHmrCache
:
useCacheOrRequestStore
?.
serverComponentsHmrCache
,
forceRevalidate
:
shouldForceRevalidate
(
workStore
,
outerWorkUnitStore
),
}
}
return
workUnitAsyncStorage
.
run
(
return
workUnitAsyncStorage
.
run
(
...
@@ -697,10 +698,13 @@ export function cache(
...
@@ -697,10 +698,13 @@ export function cache(
workUnitStore
===
undefined
||
workUnitStore
.
type
===
'unstable-cache'
workUnitStore
===
undefined
||
workUnitStore
.
type
===
'unstable-cache'
?
[]
?
[]
:
workUnitStore
.
implicitTags
:
workUnitStore
.
implicitTags
const
entry
:
undefined
|
CacheEntry
=
await
cacheHandler
.
get
(
serializedCacheKey
,
const
forceRevalidate
=
shouldForceRevalidate
(
workStore
,
workUnitStore
)
implicitTags
)
const
entry
=
forceRevalidate
?
undefined
:
await
cacheHandler
.
get
(
serializedCacheKey
,
implicitTags
)
const
currentTime
=
performance
.
timeOrigin
+
performance
.
now
()
const
currentTime
=
performance
.
timeOrigin
+
performance
.
now
()
if
(
if
(
workUnitStore
!==
undefined
&&
workUnitStore
!==
undefined
&&
...
@@ -901,3 +905,20 @@ function isPageComponent(
...
@@ -901,3 +905,20 @@ function isPageComponent(
(
props
as
UseCachePageComponentProps
).
$$isPageComponent
(
props
as
UseCachePageComponentProps
).
$$isPageComponent
)
)
}
}
function
shouldForceRevalidate
(
workStore
:
WorkStore
,
workUnitStore
:
WorkUnitStore
|
undefined
):
boolean
{
if
(
workStore
.
dev
&&
workUnitStore
)
{
if
(
workUnitStore
.
type
===
'request'
)
{
return
workUnitStore
.
headers
.
get
(
'cache-control'
)
===
'no-cache'
}
if
(
workUnitStore
.
type
===
'cache'
)
{
return
workUnitStore
.
forceRevalidate
}
}
return
false
}
test/development/app-dir/use-cache-
hmr
/app/layout.tsx
→
test/development/app-dir/use-cache-
dev
/app/layout.tsx
View file @
b0ae0b28
File moved
test/development/app-dir/use-cache-
hmr
/app/page.tsx
→
test/development/app-dir/use-cache-
dev
/app/page.tsx
View file @
b0ae0b28
async
function
getRandomValue
()
{
'use cache'
return
Math
.
random
()
}
async
function
getData
()
{
async
function
getData
()
{
'use cache'
'use cache'
return
fetch
(
'https://next-data-api-endpoint.vercel.app/api/random'
).
then
(
return
fetch
(
'https://next-data-api-endpoint.vercel.app/api/random'
).
then
(
(
res
)
=>
res
.
text
().
then
((
text
)
=>
[
text
,
'foo'
,
Math
.
random
()]
as
const
)
(
res
)
=>
res
.
text
()
.
then
(
async
(
text
)
=>
[
text
,
'foo'
,
await
getRandomValue
()]
as
const
)
)
)
}
}
...
@@ -10,10 +19,10 @@ export default async function Page() {
...
@@ -10,10 +19,10 @@ export default async function Page() {
const
[
fetchedRandom
,
text
,
mathRandom
]
=
await
getData
()
const
[
fetchedRandom
,
text
,
mathRandom
]
=
await
getData
()
return
(
return
(
<>
<
div
id=
"container"
>
<
p
id=
"fetchedRandom"
>
{
fetchedRandom
}
</
p
>
<
p
id=
"fetchedRandom"
>
{
fetchedRandom
}
</
p
>
<
p
id=
"text"
>
{
text
}
</
p
>
<
p
id=
"text"
>
{
text
}
</
p
>
<
p
id=
"mathRandom"
>
{
mathRandom
}
</
p
>
<
p
id=
"mathRandom"
>
{
mathRandom
}
</
p
>
</>
</
div
>
)
)
}
}
test/development/app-dir/use-cache-
hmr
/next.config.js
→
test/development/app-dir/use-cache-
dev
/next.config.js
View file @
b0ae0b28
File moved
test/development/app-dir/use-cache-
hmr/use-cache-hmr
.test.ts
→
test/development/app-dir/use-cache-
dev/use-cache-dev
.test.ts
View file @
b0ae0b28
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
retry
}
from
'next-test-utils'
import
{
retry
}
from
'next-test-utils'
describe
(
'use-cache-
hmr
'
,
()
=>
{
describe
(
'use-cache-
dev
'
,
()
=>
{
const
{
next
,
isTurbopack
}
=
nextTestSetup
({
const
{
next
,
isTurbopack
}
=
nextTestSetup
({
files
:
__dirname
,
files
:
__dirname
,
})
})
...
@@ -102,6 +102,37 @@ describe('use-cache-hmr', () => {
...
@@ -102,6 +102,37 @@ describe('use-cache-hmr', () => {
)
)
})
})
it
(
'should return cached data after reload'
,
async
()
=>
{
let
$
=
await
next
.
render$
(
'/'
)
const
initialContent
=
$
(
'#container'
).
text
()
$
=
await
next
.
render$
(
'/'
)
const
content
=
$
(
'#container'
).
text
()
expect
(
content
).
toEqual
(
initialContent
)
})
it
(
'should return fresh data after hard reload'
,
async
()
=>
{
let
$
=
await
next
.
render$
(
'/'
)
const
initialContent
=
$
(
'#container'
).
text
()
$
=
await
next
.
render$
(
'/'
,
{},
{
headers
:
{
'cache-control'
:
'no-cache'
}
}
)
const
hardReloadContent
=
$
(
'#container'
).
text
()
expect
(
hardReloadContent
).
not
.
toEqual
(
initialContent
)
// After a subsequent soft reload, cached data from the hard reload should
// be returned.
const
softReloadContent
=
$
(
'#container'
).
text
()
expect
(
softReloadContent
).
toEqual
(
hardReloadContent
)
})
it
(
'should successfully finish compilation when "use cache" directive is added/removed'
,
async
()
=>
{
it
(
'should successfully finish compilation when "use cache" directive is added/removed'
,
async
()
=>
{
await
next
.
browser
(
'/'
)
await
next
.
browser
(
'/'
)
let
cliOutputLength
=
next
.
cliOutput
.
length
let
cliOutputLength
=
next
.
cliOutput
.
length
...
...
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