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
3cd122d8
Unverified
Commit
3cd122d8
authored
May 07, 2025
by
Hendrik Liebau
Committed by
GitHub
May 07, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Scope `runInCleanSnapshot` to Work Store (#78930)
parent
4452ca2b
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
129 additions
and
34 deletions
+129
-34
settings.json
.vscode/settings.json
+0
-1
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+0
-1
clean-async-snapshot-instance.ts
...xt/src/server/app-render/clean-async-snapshot-instance.ts
+0
-6
clean-async-snapshot.external.ts
...xt/src/server/app-render/clean-async-snapshot.external.ts
+0
-4
work-async-storage.external.ts
...next/src/server/app-render/work-async-storage.external.ts
+12
-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
+1
-2
als.js
test/production/custom-server/als.js
+3
-0
page.js
test/production/custom-server/app/use-cache/page.js
+19
-0
cache-handler.js
test/production/custom-server/cache-handler.js
+35
-0
custom-server.test.ts
test/production/custom-server/custom-server.test.ts
+19
-0
next.config.js
test/production/custom-server/next.config.js
+13
-0
server.js
test/production/custom-server/server.js
+25
-20
No files found.
.vscode/settings.json
View file @
3cd122d8
...
...
@@ -59,7 +59,6 @@
//
singleton
modules
should
always
use
"*.external"
instead
of
"*-instance"
"packages/next/src/server/app-render/action-async-storage-instance.ts"
,
"packages/next/src/server/app-render/after-task-async-storage-instance.ts"
,
"packages/next/src/server/app-render/clean-async-snapshot-instance.ts"
,
"packages/next/src/server/app-render/work-async-storage-instance.ts"
,
"packages/next/src/server/app-render/work-unit-async-storage-instance.ts"
,
"packages/next/src/client/components/segment-cache-impl/*"
...
...
packages/next/src/server/app-render/app-render.tsx
View file @
3cd122d8
...
...
@@ -178,7 +178,6 @@ import { CacheSignal } from './cache-signal'
import
{
getTracedMetadata
}
from
'../lib/trace/utils'
import
{
InvariantError
}
from
'../../shared/lib/invariant-error'
import
'./clean-async-snapshot.external'
import
{
INFINITE_CACHE
}
from
'../../lib/constants'
import
{
createComponentStylesAndScripts
}
from
'./create-component-styles-and-scripts'
import
{
parseLoaderTree
}
from
'./parse-loader-tree'
...
...
packages/next/src/server/app-render/clean-async-snapshot-instance.ts
deleted
100644 → 0
View file @
4452ca2b
import
{
createSnapshot
}
from
'../app-render/async-local-storage'
export
const
runInCleanSnapshot
:
<
R
,
TArgs
extends
any
[]
>
(
fn
:
(...
args
:
TArgs
)
=>
R
,
...
args
:
TArgs
)
=>
R
=
createSnapshot
()
packages/next/src/server/app-render/clean-async-snapshot.external.ts
deleted
100644 → 0
View file @
4452ca2b
// Share the instance module in the next-shared layer
import
{
runInCleanSnapshot
}
from
'./clean-async-snapshot-instance'
with
{
'turbopack-transition'
:
'next-shared'
}
export
{
runInCleanSnapshot
}
packages/next/src/server/app-render/work-async-storage.external.ts
View file @
3cd122d8
...
...
@@ -98,6 +98,18 @@ export interface WorkStore {
dynamicIOEnabled
:
boolean
dev
:
boolean
/**
* Run the given function inside a clean AsyncLocalStorage snapshot. This is
* useful when generating cache entries, to ensure that the cache generation
* cannot read anything from the context we're currently executing in, which
* might include request-specific things like `cookies()` inside a
* `React.cache()`.
*/
runInCleanSnapshot
:
<
R
,
TArgs
extends
any
[]
>
(
fn
:
(...
args
:
TArgs
)
=>
R
,
...
args
:
TArgs
)
=>
R
}
export
type
WorkAsyncStorage
=
AsyncLocalStorage
<
WorkStore
>
...
...
packages/next/src/server/async-storage/work-store.ts
View file @
3cd122d8
...
...
@@ -12,6 +12,7 @@ import { AfterContext } from '../after/after-context'
import
{
normalizeAppPath
}
from
'../../shared/lib/router/utils/app-paths'
import
{
createLazyResult
,
type
LazyResult
}
from
'../lib/lazy-result'
import
{
getCacheHandlerEntries
}
from
'../use-cache/handlers'
import
{
createSnapshot
}
from
'../app-render/async-local-storage'
export
type
WorkStoreContext
=
{
/**
...
...
@@ -138,6 +139,7 @@ export function createWorkStore({
dev
:
renderOpts
.
dev
??
false
,
previouslyRevalidatedTags
,
refreshTagsByCacheKind
:
createRefreshTagsByCacheKind
(),
runInCleanSnapshot
:
createSnapshot
(),
}
// 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 @
3cd122d8
...
...
@@ -27,7 +27,6 @@ import {
workUnitAsyncStorage
,
getDraftModeProviderForCacheScope
,
}
from
'../app-render/work-unit-async-storage.external'
import
{
runInCleanSnapshot
}
from
'../app-render/clean-async-snapshot.external'
import
{
makeHangingPromise
}
from
'../dynamic-rendering-utils'
...
...
@@ -83,7 +82,7 @@ function generateCacheEntry(
// might include request specific things like cookies() inside a React.cache().
// Note: It is important that we await at least once before this because it lets us
// pop out of any stack specific contexts as well - aka "Sync" Local Storage.
return
runInCleanSnapshot
(
return
workStore
.
runInCleanSnapshot
(
generateCacheEntryWithRestoredWorkStore
,
workStore
,
outerWorkUnitStore
,
...
...
test/production/custom-server/als.js
0 → 100644
View file @
3cd122d8
const
{
AsyncLocalStorage
}
=
require
(
'async_hooks'
)
exports
.
requestIdStorage
=
new
AsyncLocalStorage
()
test/production/custom-server/app/use-cache/page.js
0 → 100644
View file @
3cd122d8
import
{
connection
}
from
'next/server'
async
function
Inner
({
id
})
{
'use cache'
return
<
p
>
inner
cache
<
/p
>
}
async
function
Outer
({
id
})
{
'use cache'
return
<
Inner
id
=
"inner"
/>
}
export
default
async
function
Page
()
{
await
connection
()
return
<
Outer
id
=
"outer"
/>
}
test/production/custom-server/cache-handler.js
0 → 100644
View file @
3cd122d8
// @ts-check
const
{
requestIdStorage
}
=
require
(
'./als'
)
const
defaultCacheHandler
=
require
(
'next/dist/server/lib/cache-handlers/default'
).
default
/**
* @type {import('next/dist/server/lib/cache-handlers/types').CacheHandlerV2}
*/
const
cacheHandler
=
{
async
get
(
cacheKey
)
{
return
defaultCacheHandler
.
get
(
cacheKey
)
},
async
set
(
cacheKey
,
pendingEntry
)
{
const
requestId
=
requestIdStorage
.
getStore
()
console
.
log
(
'set cache'
,
cacheKey
,
'requestId:'
,
requestId
)
return
defaultCacheHandler
.
set
(
cacheKey
,
pendingEntry
)
},
async
refreshTags
()
{
return
defaultCacheHandler
.
refreshTags
()
},
async
getExpiration
(...
tags
)
{
return
defaultCacheHandler
.
getExpiration
(...
tags
)
},
async
expireTags
(...
tags
)
{
return
defaultCacheHandler
.
expireTags
(...
tags
)
},
}
module
.
exports
=
cacheHandler
test/production/custom-server/custom-server.test.ts
View file @
3cd122d8
...
...
@@ -36,6 +36,17 @@ describe('custom server', () => {
expect
(
$
(
'body'
).
text
()).
toMatch
(
/pages: 19
\.\d
+
\.\d
+/
)
}
})
describe
(
'when using "use cache" with a custom cache handler'
,
()
=>
{
it
(
"should not unset the custom server's ALS context"
,
async
()
=>
{
const
cliOutputLength
=
next
.
cliOutput
.
length
const
$
=
await
next
.
render$
(
'/use-cache'
)
expect
(
$
(
'p'
).
text
()).
toBe
(
'inner cache'
)
const
cliOutput
=
next
.
cliOutput
.
slice
(
cliOutputLength
)
expect
(
cliOutput
).
toMatch
(
createCacheSetLogRegExp
(
'outer'
))
expect
(
cliOutput
).
toMatch
(
createCacheSetLogRegExp
(
'inner'
))
})
})
})
})
...
...
@@ -55,3 +66,11 @@ describe('custom server with quiet setting', () => {
expect
(
next
.
cliOutput
).
not
.
toInclude
(
'Server side error'
)
})
})
function
createCacheSetLogRegExp
(
id
:
string
)
{
// Expect a requestId, that's provided through ALS, to be present in the log
// message for the cache handler set call.
return
new
RegExp
(
`set cache \\["[A-Za-z0-9_-]{21}","(?:[0-9a-f]{2})+",\\[{"id":"
${
id
}
"},"\\$undefined"\\]\\] requestId: \\d+`
)
}
test/production/custom-server/next.config.js
0 → 100644
View file @
3cd122d8
/**
* @type {import('next').NextConfig}
*/
const
nextConfig
=
{
experimental
:
{
useCache
:
true
,
cacheHandlers
:
{
default
:
require
.
resolve
(
'./cache-handler.js'
),
},
},
}
module
.
exports
=
nextConfig
test/production/custom-server/server.js
View file @
3cd122d8
...
...
@@ -2,8 +2,11 @@ const { createServer } = require('http')
const
{
parse
}
=
require
(
'url'
)
const
next
=
require
(
'next'
)
const
getPort
=
require
(
'get-port'
)
const
{
requestIdStorage
}
=
require
(
'./als'
)
const
quiet
=
process
.
env
.
USE_QUIET
===
'true'
let
requestId
=
0
async
function
main
()
{
const
port
=
await
getPort
()
const
hostname
=
'localhost'
...
...
@@ -12,28 +15,30 @@ async function main() {
const
handle
=
app
.
getRequestHandler
()
app
.
prepare
().
then
(()
=>
{
createServer
(
async
(
req
,
res
)
=>
{
try
{
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const
parsedUrl
=
parse
(
req
.
url
,
true
)
const
{
pathname
,
query
}
=
parsedUrl
createServer
((
req
,
res
)
=>
requestIdStorage
.
run
(
requestId
++
,
async
()
=>
{
try
{
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const
parsedUrl
=
parse
(
req
.
url
,
true
)
const
{
pathname
,
query
}
=
parsedUrl
if
(
pathname
===
'/a'
)
{
await
app
.
render
(
req
,
res
,
'/a'
,
query
)
}
else
if
(
pathname
===
'/b'
)
{
await
app
.
render
(
req
,
res
,
'/page-b'
,
query
)
}
else
if
(
pathname
===
'/error'
)
{
await
app
.
render
(
req
,
res
,
'/page-error'
)
}
else
{
await
handle
(
req
,
res
,
parsedUrl
)
if
(
pathname
===
'/a'
)
{
await
app
.
render
(
req
,
res
,
'/a'
,
query
)
}
else
if
(
pathname
===
'/b'
)
{
await
app
.
render
(
req
,
res
,
'/page-b'
,
query
)
}
else
if
(
pathname
===
'/error'
)
{
await
app
.
render
(
req
,
res
,
'/page-error'
)
}
else
{
await
handle
(
req
,
res
,
parsedUrl
)
}
}
catch
(
err
)
{
console
.
error
(
'Error occurred handling'
,
req
.
url
,
err
)
res
.
statusCode
=
500
res
.
end
(
'Internal Server Error'
)
}
}
catch
(
err
)
{
console
.
error
(
'Error occurred handling'
,
req
.
url
,
err
)
res
.
statusCode
=
500
res
.
end
(
'Internal Server Error'
)
}
}).
listen
(
port
,
undefined
,
(
err
)
=>
{
})
).
listen
(
port
,
undefined
,
(
err
)
=>
{
if
(
err
)
throw
err
// Start mode
console
.
log
(
`- Local: http://
${
hostname
}
:
${
port
}
`
)
...
...
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