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
17257e64
Commit
17257e64
authored
Dec 07, 2025
by
Jiachi Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
json
parent
84172a6b
Hide whitespace changes
Inline
Side-by-side
Showing
24 changed files
with
726 additions
and
25 deletions
+726
-25
define-env.ts
packages/next/src/build/define-env.ts
+3
-0
options.ts
packages/next/src/build/swc/options.ts
+1
-1
app-page.ts
packages/next/src/build/templates/app-page.ts
+3
-0
edge-ssr-app.ts
packages/next/src/build/templates/edge-ssr-app.ts
+3
-0
index.ts
packages/next/src/export/index.ts
+2
-0
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+32
-19
make-get-server-inserted-html.tsx
...t/src/server/app-render/make-get-server-inserted-html.tsx
+10
-1
profiling-suspense.tsx
packages/next/src/server/app-render/profiling-suspense.tsx
+103
-0
suspense-boundary-collector.ts
...next/src/server/app-render/suspense-boundary-collector.ts
+91
-0
suspense-boundary-injector.tsx
...next/src/server/app-render/suspense-boundary-injector.tsx
+47
-0
types.ts
packages/next/src/server/app-render/types.ts
+1
-0
work-store.ts
packages/next/src/server/async-storage/work-store.ts
+1
-1
base-server.ts
packages/next/src/server/base-server.ts
+2
-0
config-schema.ts
packages/next/src/server/config-schema.ts
+1
-0
config-shared.ts
packages/next/src/server/config-shared.ts
+13
-0
react.ts
...t/src/server/route-modules/app-page/vendored/rsc/react.ts
+10
-1
react.ts
...t/src/server/route-modules/app-page/vendored/ssr/react.ts
+10
-1
adapter.ts
packages/next/src/server/web/adapter.ts
+2
-0
types.ts
packages/next/src/server/web/types.ts
+4
-1
layout.tsx
test/e2e/app-dir/suspense-profiling/app/layout.tsx
+11
-0
page.tsx
test/e2e/app-dir/suspense-profiling/app/nested/page.tsx
+88
-0
page.tsx
test/e2e/app-dir/suspense-profiling/app/page.tsx
+74
-0
next.config.js
test/e2e/app-dir/suspense-profiling/next.config.js
+6
-0
suspense-profiling.test.ts
...e2e/app-dir/suspense-profiling/suspense-profiling.test.ts
+208
-0
No files found.
packages/next/src/build/define-env.ts
View file @
17257e64
...
@@ -166,6 +166,9 @@ export function getDefineEnv({
...
@@ -166,6 +166,9 @@ export function getDefineEnv({
'process.env.__NEXT_PPR'
:
isPPREnabled
,
'process.env.__NEXT_PPR'
:
isPPREnabled
,
'process.env.__NEXT_CACHE_COMPONENTS'
:
isCacheComponentsEnabled
,
'process.env.__NEXT_CACHE_COMPONENTS'
:
isCacheComponentsEnabled
,
'process.env.__NEXT_USE_CACHE'
:
isUseCacheEnabled
,
'process.env.__NEXT_USE_CACHE'
:
isUseCacheEnabled
,
'process.env.__NEXT_SUSPENSE_PROFILING'
:
Boolean
(
config
.
experimental
.
suspenseProfiling
),
...(
isClient
...(
isClient
?
{
?
{
...
...
packages/next/src/build/swc/options.ts
View file @
17257e64
...
@@ -150,7 +150,7 @@ function getBaseSWCOptions({
...
@@ -150,7 +150,7 @@ function getBaseSWCOptions({
runtime
:
'automatic'
,
runtime
:
'automatic'
,
pragmaFrag
:
'React.Fragment'
,
pragmaFrag
:
'React.Fragment'
,
throwIfNamespace
:
true
,
throwIfNamespace
:
true
,
development
:
!!
development
,
development
:
!!
development
,
useBuiltins
:
true
,
useBuiltins
:
true
,
refresh
:
!!
hasReactRefresh
,
refresh
:
!!
hasReactRefresh
,
},
},
...
...
packages/next/src/build/templates/app-page.ts
View file @
17257e64
...
@@ -584,6 +584,9 @@ export async function handler(
...
@@ -584,6 +584,9 @@ export async function handler(
cacheComponents
:
Boolean
(
nextConfig
.
cacheComponents
),
cacheComponents
:
Boolean
(
nextConfig
.
cacheComponents
),
experimental
:
{
experimental
:
{
isRoutePPREnabled
,
isRoutePPREnabled
,
isSuspenseProfilingEnabled
:
Boolean
(
nextConfig
.
experimental
.
suspenseProfiling
),
expireTime
:
nextConfig
.
expireTime
,
expireTime
:
nextConfig
.
expireTime
,
staleTimes
:
nextConfig
.
experimental
.
staleTimes
,
staleTimes
:
nextConfig
.
experimental
.
staleTimes
,
dynamicOnHover
:
Boolean
(
nextConfig
.
experimental
.
dynamicOnHover
),
dynamicOnHover
:
Boolean
(
nextConfig
.
experimental
.
dynamicOnHover
),
...
...
packages/next/src/build/templates/edge-ssr-app.ts
View file @
17257e64
...
@@ -150,6 +150,9 @@ async function requestHandler(
...
@@ -150,6 +150,9 @@ async function requestHandler(
cacheComponents
:
Boolean
(
nextConfig
.
cacheComponents
),
cacheComponents
:
Boolean
(
nextConfig
.
cacheComponents
),
experimental
:
{
experimental
:
{
isRoutePPREnabled
:
false
,
isRoutePPREnabled
:
false
,
isSuspenseProfilingEnabled
:
Boolean
(
nextConfig
.
experimental
.
suspenseProfiling
),
expireTime
:
nextConfig
.
expireTime
,
expireTime
:
nextConfig
.
expireTime
,
staleTimes
:
nextConfig
.
experimental
.
staleTimes
,
staleTimes
:
nextConfig
.
experimental
.
staleTimes
,
dynamicOnHover
:
Boolean
(
nextConfig
.
experimental
.
dynamicOnHover
),
dynamicOnHover
:
Boolean
(
nextConfig
.
experimental
.
dynamicOnHover
),
...
...
packages/next/src/export/index.ts
View file @
17257e64
...
@@ -396,6 +396,8 @@ async function exportAppImpl(
...
@@ -396,6 +396,8 @@ async function exportAppImpl(
dynamicOnHover
:
nextConfig
.
experimental
.
dynamicOnHover
??
false
,
dynamicOnHover
:
nextConfig
.
experimental
.
dynamicOnHover
??
false
,
inlineCss
:
nextConfig
.
experimental
.
inlineCss
??
false
,
inlineCss
:
nextConfig
.
experimental
.
inlineCss
??
false
,
authInterrupts
:
!!
nextConfig
.
experimental
.
authInterrupts
,
authInterrupts
:
!!
nextConfig
.
experimental
.
authInterrupts
,
isSuspenseProfilingEnabled
:
!!
nextConfig
.
experimental
.
suspenseProfiling
,
},
},
reactMaxHeadersLength
:
nextConfig
.
reactMaxHeadersLength
,
reactMaxHeadersLength
:
nextConfig
.
reactMaxHeadersLength
,
hasReadableErrorStacks
:
hasReadableErrorStacks
:
...
...
packages/next/src/server/app-render/app-render.tsx
View file @
17257e64
...
@@ -92,6 +92,10 @@ import { createServerInsertedHTML } from './server-inserted-html'
...
@@ -92,6 +92,10 @@ import { createServerInsertedHTML } from './server-inserted-html'
import
{
getRequiredScripts
}
from
'./required-scripts'
import
{
getRequiredScripts
}
from
'./required-scripts'
import
{
addPathPrefix
}
from
'../../shared/lib/router/utils/add-path-prefix'
import
{
addPathPrefix
}
from
'../../shared/lib/router/utils/add-path-prefix'
import
{
makeGetServerInsertedHTML
}
from
'./make-get-server-inserted-html'
import
{
makeGetServerInsertedHTML
}
from
'./make-get-server-inserted-html'
import
{
suspenseBoundaryCollectorStorage
,
createSuspenseBoundaryCollector
,
}
from
'./suspense-boundary-collector'
import
{
walkTreeWithFlightRouterState
}
from
'./walk-tree-with-flight-router-state'
import
{
walkTreeWithFlightRouterState
}
from
'./walk-tree-with-flight-router-state'
import
{
createComponentTree
,
getRootParams
}
from
'./create-component-tree'
import
{
createComponentTree
,
getRootParams
}
from
'./create-component-tree'
import
{
getAssetQueryString
}
from
'./get-asset-query-string'
import
{
getAssetQueryString
}
from
'./get-asset-query-string'
...
@@ -2406,25 +2410,34 @@ export const renderToHTMLOrFlight: AppPageRender = (
...
@@ -2406,25 +2410,34 @@ export const renderToHTMLOrFlight: AppPageRender = (
nonce
,
nonce
,
})
})
return
workAsyncStorage
.
run
(
const
runRender
=
()
=>
workStore
,
workAsyncStorage
.
run
(
// The function to run
workStore
,
renderToHTMLOrFlightImpl
,
// The function to run
// all of it's args
renderToHTMLOrFlightImpl
,
req
,
// all of it's args
res
,
req
,
url
,
res
,
pagePath
,
url
,
query
,
pagePath
,
renderOpts
,
query
,
workStore
,
renderOpts
,
parsedRequestHeaders
,
workStore
,
postponedState
,
parsedRequestHeaders
,
serverComponentsHmrCache
,
postponedState
,
sharedContext
,
serverComponentsHmrCache
,
interpolatedParams
,
sharedContext
,
fallbackRouteParams
interpolatedParams
,
)
fallbackRouteParams
)
// When suspense profiling is enabled, wrap with the boundary collector context
if
(
renderOpts
.
experimental
.
isSuspenseProfilingEnabled
)
{
const
collector
=
createSuspenseBoundaryCollector
()
return
suspenseBoundaryCollectorStorage
.
run
(
collector
,
runRender
)
}
return
runRender
()
}
}
function
applyMetadataFromPrerenderResult
(
function
applyMetadataFromPrerenderResult
(
...
...
packages/next/src/server/app-render/make-get-server-inserted-html.tsx
View file @
17257e64
...
@@ -11,6 +11,8 @@ import { streamToString } from '../stream-utils/node-web-streams-helper'
...
@@ -11,6 +11,8 @@ import { streamToString } from '../stream-utils/node-web-streams-helper'
import
{
RedirectStatusCode
}
from
'../../client/components/redirect-status-code'
import
{
RedirectStatusCode
}
from
'../../client/components/redirect-status-code'
import
{
addPathPrefix
}
from
'../../shared/lib/router/utils/add-path-prefix'
import
{
addPathPrefix
}
from
'../../shared/lib/router/utils/add-path-prefix'
import
type
{
ClientTraceDataEntry
}
from
'../lib/trace/tracer'
import
type
{
ClientTraceDataEntry
}
from
'../lib/trace/tracer'
import
{
SuspenseBoundaryScript
}
from
'./suspense-boundary-injector'
import
{
getSuspenseBoundaries
}
from
'./suspense-boundary-collector'
export
function
makeGetServerInsertedHTML
({
export
function
makeGetServerInsertedHTML
({
polyfills
,
polyfills
,
...
@@ -74,6 +76,8 @@ export function makeGetServerInsertedHTML({
...
@@ -74,6 +76,8 @@ export function makeGetServerInsertedHTML({
const
serverInsertedHTML
=
renderServerInsertedHTML
()
const
serverInsertedHTML
=
renderServerInsertedHTML
()
// Skip React rendering if we know the content is empty.
// Skip React rendering if we know the content is empty.
// Note: We can't skip when suspense profiling might be active since
// SuspenseBoundaryScript may have data to inject.
if
(
if
(
polyfillTags
.
length
===
0
&&
polyfillTags
.
length
===
0
&&
traceMetaTags
.
length
===
0
&&
traceMetaTags
.
length
===
0
&&
...
@@ -81,7 +85,11 @@ export function makeGetServerInsertedHTML({
...
@@ -81,7 +85,11 @@ export function makeGetServerInsertedHTML({
Array
.
isArray
(
serverInsertedHTML
)
&&
Array
.
isArray
(
serverInsertedHTML
)
&&
serverInsertedHTML
.
length
===
0
serverInsertedHTML
.
length
===
0
)
{
)
{
return
''
// Check if there are any suspense boundaries to inject
const
boundaries
=
getSuspenseBoundaries
()
if
(
boundaries
.
length
===
0
)
{
return
''
}
}
}
const
stream
=
await
renderToReadableStream
(
const
stream
=
await
renderToReadableStream
(
...
@@ -90,6 +98,7 @@ export function makeGetServerInsertedHTML({
...
@@ -90,6 +98,7 @@ export function makeGetServerInsertedHTML({
{
serverInsertedHTML
}
{
serverInsertedHTML
}
{
traceMetaTags
}
{
traceMetaTags
}
{
errorMetaTags
}
{
errorMetaTags
}
<
SuspenseBoundaryScript
/>
</>,
</>,
{
{
// Larger chunk because this isn't sent over the network.
// Larger chunk because this isn't sent over the network.
...
...
packages/next/src/server/app-render/profiling-suspense.tsx
0 → 100644
View file @
17257e64
import
type
{
ComponentProps
,
ReactNode
,
ComponentType
}
from
'react'
import
{
registerSuspenseBoundary
,
pushSuspenseBoundary
,
popSuspenseBoundary
,
type
SuspenseBoundarySource
,
}
from
'./suspense-boundary-collector'
type
SuspenseProps
=
ComponentProps
<
typeof
import
(
'react'
).
Suspense
>
interface
ProfilingSuspenseProps
extends
SuspenseProps
{
__source
?:
SuspenseBoundarySource
}
// Wrapper component to track boundary nesting
function
SuspenseBoundaryTracker
({
boundaryId
,
children
,
}:
{
boundaryId
:
string
children
:
ReactNode
})
{
// Push this boundary onto the stack when rendering children
// This allows us to track parent-child relationships
pushSuspenseBoundary
(
boundaryId
)
// The pop happens after children render due to React's depth-first rendering
// We use a trick: wrap children in a component that pops on render
return
(
<>
{
children
}
<
SuspenseBoundaryTrackerEnd
boundaryId=
{
boundaryId
}
/>
</>
)
}
// This component renders at the end of the boundary's children
// and pops the boundary from the stack
function
SuspenseBoundaryTrackerEnd
({
boundaryId
:
_boundaryId
,
}:
{
boundaryId
:
string
})
{
popSuspenseBoundary
()
return
null
}
export
function
createProfilingSuspense
(
OriginalSuspense
:
ComponentType
<
SuspenseProps
>
):
ComponentType
<
ProfilingSuspenseProps
>
{
function
ProfilingSuspense
(
props
:
ProfilingSuspenseProps
):
ReactNode
{
const
{
children
,
fallback
,
__source
,
...
rest
}
=
props
// Register this boundary and get its unique ID
const
boundaryId
=
registerSuspenseBoundary
(
__source
||
null
)
// If no boundary ID (collector not active), render original
if
(
!
boundaryId
)
{
return
(
<
OriginalSuspense
fallback=
{
fallback
}
{
...
rest
}
>
{
children
}
</
OriginalSuspense
>
)
}
// Wrap children to track nesting
const
trackedChildren
=
(
<
SuspenseBoundaryTracker
boundaryId=
{
boundaryId
}
>
{
children
}
</
SuspenseBoundaryTracker
>
)
return
(
<
OriginalSuspense
fallback=
{
<>
{
/* Hidden marker for fallback state */
}
<
span
data
-
suspense
-
boundary=
{
boundaryId
}
data
-
suspense
-
state=
"fallback"
hidden
/>
{
fallback
}
</>
}
{
...
rest
}
>
{
/* Hidden marker for content state */
}
<
span
data
-
suspense
-
boundary=
{
boundaryId
}
data
-
suspense
-
state=
"content"
hidden
/>
{
trackedChildren
}
</
OriginalSuspense
>
)
}
// Preserve display name for debugging
ProfilingSuspense
.
displayName
=
'Suspense'
return
ProfilingSuspense
as
ComponentType
<
ProfilingSuspenseProps
>
}
packages/next/src/server/app-render/suspense-boundary-collector.ts
0 → 100644
View file @
17257e64
import
{
createAsyncLocalStorage
}
from
'./async-local-storage'
export
interface
SuspenseBoundarySource
{
fileName
:
string
lineNumber
:
number
columnNumber
:
number
}
export
interface
SuspenseBoundaryInfo
{
id
:
string
source
:
SuspenseBoundarySource
|
null
parentId
:
string
|
null
children
:
string
[]
}
export
interface
SuspenseBoundaryCollectorStore
{
boundaries
:
Map
<
string
,
SuspenseBoundaryInfo
>
idCounter
:
number
parentStack
:
string
[]
}
export
const
suspenseBoundaryCollectorStorage
=
createAsyncLocalStorage
<
SuspenseBoundaryCollectorStore
>
()
export
function
createSuspenseBoundaryCollector
():
SuspenseBoundaryCollectorStore
{
return
{
boundaries
:
new
Map
(),
idCounter
:
0
,
parentStack
:
[],
}
}
export
function
registerSuspenseBoundary
(
source
:
SuspenseBoundarySource
|
null
):
string
{
const
store
=
suspenseBoundaryCollectorStorage
.
getStore
()
if
(
!
store
)
{
return
''
}
const
id
=
`suspense-
${
store
.
idCounter
++
}
`
const
parentId
=
store
.
parentStack
[
store
.
parentStack
.
length
-
1
]
||
null
// Register this boundary
store
.
boundaries
.
set
(
id
,
{
id
,
source
,
parentId
,
children
:
[],
})
// Add to parent's children list
if
(
parentId
)
{
const
parent
=
store
.
boundaries
.
get
(
parentId
)
if
(
parent
)
{
parent
.
children
.
push
(
id
)
}
}
return
id
}
export
function
pushSuspenseBoundary
(
id
:
string
):
void
{
const
store
=
suspenseBoundaryCollectorStorage
.
getStore
()
if
(
store
&&
id
)
{
store
.
parentStack
.
push
(
id
)
}
}
export
function
popSuspenseBoundary
():
void
{
const
store
=
suspenseBoundaryCollectorStorage
.
getStore
()
if
(
store
)
{
store
.
parentStack
.
pop
()
}
}
export
function
getSuspenseBoundaries
():
SuspenseBoundaryInfo
[]
{
const
store
=
suspenseBoundaryCollectorStorage
.
getStore
()
if
(
!
store
)
{
return
[]
}
return
Array
.
from
(
store
.
boundaries
.
values
())
}
export
function
serializeSuspenseBoundaries
():
string
{
const
boundaries
=
getSuspenseBoundaries
()
return
JSON
.
stringify
({
boundaries
,
timestamp
:
Date
.
now
(),
})
}
packages/next/src/server/app-render/suspense-boundary-injector.tsx
0 → 100644
View file @
17257e64
import
React
from
'react'
import
{
getSuspenseBoundaries
,
type
SuspenseBoundaryInfo
,
}
from
'./suspense-boundary-collector'
export
interface
SuspenseBoundaryData
{
boundaries
:
SuspenseBoundaryInfo
[]
timestamp
:
number
}
export
function
getSuspenseBoundaryData
():
SuspenseBoundaryData
{
return
{
boundaries
:
getSuspenseBoundaries
(),
timestamp
:
Date
.
now
(),
}
}
export
function
SuspenseBoundaryScript
():
React
.
ReactNode
{
const
data
=
getSuspenseBoundaryData
()
// Only inject if there are boundaries to report
// (boundaries are only collected when suspense profiling is enabled)
if
(
data
.
boundaries
.
length
===
0
)
{
return
null
}
return
(
<
script
id=
"__NEXT_SUSPENSE_BOUNDARIES__"
type=
"application/json"
dangerouslySetInnerHTML=
{
{
__html
:
JSON
.
stringify
(
data
),
}
}
/>
)
}
export
function
serializeSuspenseBoundaryScript
():
string
{
const
data
=
getSuspenseBoundaryData
()
if
(
data
.
boundaries
.
length
===
0
)
{
return
''
}
return
`<script id="__NEXT_SUSPENSE_BOUNDARIES__" type="application/json">
${
JSON
.
stringify
(
data
)}
</script>`
}
packages/next/src/server/app-render/types.ts
View file @
17257e64
...
@@ -145,6 +145,7 @@ export interface RenderOptsPartial {
...
@@ -145,6 +145,7 @@ export interface RenderOptsPartial {
* prerendering.
* prerendering.
*/
*/
isRoutePPREnabled
?:
boolean
isRoutePPREnabled
?:
boolean
isSuspenseProfilingEnabled
?:
boolean
expireTime
:
number
|
undefined
expireTime
:
number
|
undefined
staleTimes
:
ExperimentalConfig
[
'staleTimes'
]
|
undefined
staleTimes
:
ExperimentalConfig
[
'staleTimes'
]
|
undefined
clientTraceMetadata
:
string
[]
|
undefined
clientTraceMetadata
:
string
[]
|
undefined
...
...
packages/next/src/server/async-storage/work-store.ts
View file @
17257e64
...
@@ -31,7 +31,7 @@ export type WorkStoreContext = {
...
@@ -31,7 +31,7 @@ export type WorkStoreContext = {
pendingWaitUntil
?:
Promise
<
any
>
pendingWaitUntil
?:
Promise
<
any
>
experimental
:
Pick
<
experimental
:
Pick
<
RenderOpts
[
'experimental'
],
RenderOpts
[
'experimental'
],
'isRoutePPREnabled'
|
'authInterrupts'
'isRoutePPREnabled'
|
'
isSuspenseProfilingEnabled'
|
'
authInterrupts'
>
>
/**
/**
...
...
packages/next/src/server/base-server.ts
View file @
17257e64
...
@@ -558,6 +558,8 @@ export default abstract class Server<
...
@@ -558,6 +558,8 @@ export default abstract class Server<
dynamicOnHover
:
this
.
nextConfig
.
experimental
.
dynamicOnHover
??
false
,
dynamicOnHover
:
this
.
nextConfig
.
experimental
.
dynamicOnHover
??
false
,
inlineCss
:
this
.
nextConfig
.
experimental
.
inlineCss
??
false
,
inlineCss
:
this
.
nextConfig
.
experimental
.
inlineCss
??
false
,
authInterrupts
:
!!
this
.
nextConfig
.
experimental
.
authInterrupts
,
authInterrupts
:
!!
this
.
nextConfig
.
experimental
.
authInterrupts
,
isSuspenseProfilingEnabled
:
!!
this
.
nextConfig
.
experimental
.
suspenseProfiling
,
},
},
onInstrumentationRequestError
:
onInstrumentationRequestError
:
this
.
instrumentationOnRequestError
.
bind
(
this
),
this
.
instrumentationOnRequestError
.
bind
(
this
),
...
...
packages/next/src/server/config-schema.ts
View file @
17257e64
...
@@ -329,6 +329,7 @@ export const experimentalSchema = {
...
@@ -329,6 +329,7 @@ export const experimentalSchema = {
serverComponentsHmrCache
:
z
.
boolean
().
optional
(),
serverComponentsHmrCache
:
z
.
boolean
().
optional
(),
authInterrupts
:
z
.
boolean
().
optional
(),
authInterrupts
:
z
.
boolean
().
optional
(),
useCache
:
z
.
boolean
().
optional
(),
useCache
:
z
.
boolean
().
optional
(),
suspenseProfiling
:
z
.
boolean
().
optional
(),
slowModuleDetection
:
z
slowModuleDetection
:
z
.
object
({
.
object
({
buildTimeThresholdMs
:
z
.
number
().
int
(),
buildTimeThresholdMs
:
z
.
number
().
int
(),
...
...
packages/next/src/server/config-shared.ts
View file @
17257e64
...
@@ -844,6 +844,17 @@ export interface ExperimentalConfig {
...
@@ -844,6 +844,17 @@ export interface ExperimentalConfig {
* @default false
* @default false
*/
*/
runtimeServerDeploymentId
?:
boolean
runtimeServerDeploymentId
?:
boolean
/**
* Enable Suspense boundary profiling for inspecting static shell composition.
* When enabled, Next.js will track all Suspense boundaries during rendering
* and inject inspection data into the HTML output.
*
* This is intended for profiling builds and will increase bundle size.
*
* @default false
*/
suspenseProfiling
?:
boolean
}
}
export
type
ExportPathMap
=
{
export
type
ExportPathMap
=
{
...
@@ -1670,6 +1681,7 @@ export interface NextConfigRuntime {
...
@@ -1670,6 +1681,7 @@ export interface NextConfigRuntime {
|
'proxyTimeout'
|
'proxyTimeout'
|
'testProxy'
|
'testProxy'
|
'runtimeServerDeploymentId'
|
'runtimeServerDeploymentId'
|
'suspenseProfiling'
>
&
{
>
&
{
// Pick on @internal fields generates invalid .d.ts files
// Pick on @internal fields generates invalid .d.ts files
/** @internal */
/** @internal */
...
@@ -1725,6 +1737,7 @@ export function getNextConfigRuntime(
...
@@ -1725,6 +1737,7 @@ export function getNextConfigRuntime(
proxyTimeout
:
ex
.
proxyTimeout
,
proxyTimeout
:
ex
.
proxyTimeout
,
testProxy
:
ex
.
testProxy
,
testProxy
:
ex
.
testProxy
,
runtimeServerDeploymentId
:
ex
.
runtimeServerDeploymentId
,
runtimeServerDeploymentId
:
ex
.
runtimeServerDeploymentId
,
suspenseProfiling
:
ex
.
suspenseProfiling
,
trustHostHeader
:
ex
.
trustHostHeader
,
trustHostHeader
:
ex
.
trustHostHeader
,
isExperimentalCompile
:
ex
.
isExperimentalCompile
,
isExperimentalCompile
:
ex
.
isExperimentalCompile
,
...
...
packages/next/src/server/route-modules/app-page/vendored/rsc/react.ts
View file @
17257e64
module
.
exports
=
(
import
{
createProfilingSuspense
}
from
'../../../../app-render/profiling-suspense'
const
ReactRSC
=
(
require
(
'../../module.compiled'
)
as
typeof
import
(
'../../module.compiled'
)
require
(
'../../module.compiled'
)
as
typeof
import
(
'../../module.compiled'
)
).
vendored
[
'react-rsc'
]
!
.
React
).
vendored
[
'react-rsc'
]
!
.
React
// Always wrap Suspense with profiling wrapper.
// The wrapper checks at render time if the collector is active.
// If not, it renders the original Suspense without markers.
module
.
exports
=
Object
.
assign
({},
ReactRSC
,
{
Suspense
:
createProfilingSuspense
(
ReactRSC
.
Suspense
),
})
packages/next/src/server/route-modules/app-page/vendored/ssr/react.ts
View file @
17257e64
module
.
exports
=
(
import
{
createProfilingSuspense
}
from
'../../../../app-render/profiling-suspense'
const
ReactSSR
=
(
require
(
'../../module.compiled'
)
as
typeof
import
(
'../../module.compiled'
)
require
(
'../../module.compiled'
)
as
typeof
import
(
'../../module.compiled'
)
).
vendored
[
'react-ssr'
]
!
.
React
).
vendored
[
'react-ssr'
]
!
.
React
// Always wrap Suspense with profiling wrapper.
// The wrapper checks at render time if the collector is active.
// If not, it renders the original Suspense without markers.
module
.
exports
=
Object
.
assign
({},
ReactSSR
,
{
Suspense
:
createProfilingSuspense
(
ReactSSR
.
Suspense
),
})
packages/next/src/server/web/adapter.ts
View file @
17257e64
...
@@ -298,6 +298,8 @@ export async function adapter(
...
@@ -298,6 +298,8 @@ export async function adapter(
cacheComponents
:
false
,
cacheComponents
:
false
,
experimental
:
{
experimental
:
{
isRoutePPREnabled
:
false
,
isRoutePPREnabled
:
false
,
isSuspenseProfilingEnabled
:
!!
params
.
request
.
nextConfig
?.
experimental
?.
suspenseProfiling
,
authInterrupts
:
authInterrupts
:
!!
params
.
request
.
nextConfig
?.
experimental
?.
authInterrupts
,
!!
params
.
request
.
nextConfig
?.
experimental
?.
authInterrupts
,
},
},
...
...
packages/next/src/server/web/types.ts
View file @
17257e64
...
@@ -22,7 +22,10 @@ export interface RequestData {
...
@@ -22,7 +22,10 @@ export interface RequestData {
trailingSlash
?:
boolean
trailingSlash
?:
boolean
experimental
?:
Pick
<
experimental
?:
Pick
<
ExperimentalConfig
,
ExperimentalConfig
,
'cacheLife'
|
'authInterrupts'
|
'clientParamParsingOrigins'
|
'cacheLife'
|
'authInterrupts'
|
'clientParamParsingOrigins'
|
'suspenseProfiling'
>
>
}
}
page
?:
{
page
?:
{
...
...
test/e2e/app-dir/suspense-profiling/app/layout.tsx
0 → 100644
View file @
17257e64
export
default
function
RootLayout
({
children
,
}:
{
children
:
React
.
ReactNode
})
{
return
(
<
html
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/e2e/app-dir/suspense-profiling/app/nested/page.tsx
0 → 100644
View file @
17257e64
import
{
Suspense
}
from
'react'
// Async component with configurable delay
async
function
AsyncContent
({
id
,
delay
,
children
,
}:
{
id
:
string
delay
:
number
children
?:
React
.
ReactNode
})
{
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
delay
))
return
(
<
div
data
-
testid=
{
`async-${id}`
}
>
<
span
>
Async content:
{
id
}
</
span
>
{
children
}
</
div
>
)
}
// Deeply nested Suspense structure to test parent-child tracking
function
Level1
()
{
return
(
<
Suspense
fallback=
{
<
div
data
-
testid=
"level1-loading"
>
Loading Level 1...
</
div
>
}
>
<
AsyncContent
id=
"level1"
delay=
{
50
}
>
<
Level2
/>
</
AsyncContent
>
</
Suspense
>
)
}
function
Level2
()
{
return
(
<
Suspense
fallback=
{
<
div
data
-
testid=
"level2-loading"
>
Loading Level 2...
</
div
>
}
>
<
AsyncContent
id=
"level2"
delay=
{
50
}
>
<
Level3
/>
</
AsyncContent
>
</
Suspense
>
)
}
function
Level3
()
{
return
(
<
Suspense
fallback=
{
<
div
data
-
testid=
"level3-loading"
>
Loading Level 3...
</
div
>
}
>
<
AsyncContent
id=
"level3"
delay=
{
50
}
>
<
div
data
-
testid=
"deepest-content"
>
Deepest content reached!
</
div
>
</
AsyncContent
>
</
Suspense
>
)
}
// Sibling Suspense boundaries at the same level
function
SiblingBoundaries
()
{
return
(
<
div
data
-
testid=
"siblings-container"
>
<
Suspense
fallback=
{
<
div
data
-
testid=
"sibling-a-loading"
>
Loading A...
</
div
>
}
>
<
AsyncContent
id=
"sibling-a"
delay=
{
30
}
/>
</
Suspense
>
<
Suspense
fallback=
{
<
div
data
-
testid=
"sibling-b-loading"
>
Loading B...
</
div
>
}
>
<
AsyncContent
id=
"sibling-b"
delay=
{
60
}
/>
</
Suspense
>
<
Suspense
fallback=
{
<
div
data
-
testid=
"sibling-c-loading"
>
Loading C...
</
div
>
}
>
<
AsyncContent
id=
"sibling-c"
delay=
{
90
}
/>
</
Suspense
>
</
div
>
)
}
export
default
function
NestedPage
()
{
return
(
<
div
data
-
testid=
"nested-page-root"
>
<
h1
>
Nested Suspense Test
</
h1
>
<
section
>
<
h2
>
Deeply Nested
</
h2
>
<
Level1
/>
</
section
>
<
section
>
<
h2
>
Sibling Boundaries
</
h2
>
<
SiblingBoundaries
/>
</
section
>
</
div
>
)
}
test/e2e/app-dir/suspense-profiling/app/page.tsx
0 → 100644
View file @
17257e64
import
{
Suspense
}
from
'react'
// Simulate async component
async
function
SlowComponent
({
id
,
delay
}:
{
id
:
string
;
delay
:
number
})
{
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
delay
))
return
<
div
data
-
testid=
{
`content-${id}`
}
>
Content
{
id
}
loaded
</
div
>
}
// Header with its own Suspense boundary
function
Header
()
{
return
(
<
header
>
<
Suspense
fallback=
{
<
div
data
-
testid=
"header-loading"
>
Loading header...
</
div
>
}
>
<
SlowComponent
id=
"header"
delay=
{
100
}
/>
</
Suspense
>
</
header
>
)
}
// Sidebar with nested Suspense boundaries
function
Sidebar
()
{
return
(
<
aside
>
<
Suspense
fallback=
{
<
div
data
-
testid=
"sidebar-loading"
>
Loading sidebar...
</
div
>
}
>
<
div
data
-
testid=
"sidebar-container"
>
<
SlowComponent
id=
"sidebar-top"
delay=
{
50
}
/>
<
Suspense
fallback=
{
<
div
data
-
testid=
"sidebar-nested-loading"
>
Loading nested...
</
div
>
}
>
<
SlowComponent
id=
"sidebar-nested"
delay=
{
150
}
/>
</
Suspense
>
</
div
>
</
Suspense
>
</
aside
>
)
}
// Main content area
function
MainContent
()
{
return
(
<
main
>
<
Suspense
fallback=
{
<
div
data
-
testid=
"main-loading"
>
Loading main...
</
div
>
}
>
<
SlowComponent
id=
"main"
delay=
{
200
}
/>
</
Suspense
>
</
main
>
)
}
// Footer
function
Footer
()
{
return
(
<
footer
>
<
Suspense
fallback=
{
<
div
data
-
testid=
"footer-loading"
>
Loading footer...
</
div
>
}
>
<
SlowComponent
id=
"footer"
delay=
{
75
}
/>
</
Suspense
>
</
footer
>
)
}
export
default
function
Page
()
{
return
(
<
div
data
-
testid=
"page-root"
>
<
h1
>
Suspense Profiling Test
</
h1
>
{
/* Multiple Suspense boundaries at different levels */
}
<
Suspense
fallback=
{
<
div
data
-
testid=
"outer-loading"
>
Loading page...
</
div
>
}
>
<
Header
/>
<
div
style=
{
{
display
:
'flex'
}
}
>
<
Sidebar
/>
<
MainContent
/>
</
div
>
<
Footer
/>
</
Suspense
>
</
div
>
)
}
test/e2e/app-dir/suspense-profiling/next.config.js
0 → 100644
View file @
17257e64
/** @type {import('next').NextConfig} */
module
.
exports
=
{
experimental
:
{
suspenseProfiling
:
true
,
},
}
test/e2e/app-dir/suspense-profiling/suspense-profiling.test.ts
0 → 100644
View file @
17257e64
import
{
nextTestSetup
}
from
'e2e-utils'
import
cheerio
from
'cheerio'
describe
(
'suspense-profiling'
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
files
:
__dirname
,
})
interface
SuspenseBoundarySource
{
fileName
:
string
lineNumber
:
number
columnNumber
:
number
}
interface
SuspenseBoundaryInfo
{
id
:
string
source
:
SuspenseBoundarySource
|
null
parentId
:
string
|
null
children
:
string
[]
}
interface
SuspenseBoundaryData
{
boundaries
:
SuspenseBoundaryInfo
[]
timestamp
:
number
}
function
parseBoundaryData
(
html
:
string
):
SuspenseBoundaryData
|
null
{
const
$
=
cheerio
.
load
(
html
)
const
script
=
$
(
'#__NEXT_SUSPENSE_BOUNDARIES__'
)
if
(
!
script
.
length
)
{
return
null
}
try
{
return
JSON
.
parse
(
script
.
html
()
||
'{}'
)
}
catch
{
return
null
}
}
function
getBoundaryMarkers
(
html
:
string
):
string
[]
{
const
$
=
cheerio
.
load
(
html
)
const
markers
:
string
[]
=
[]
$
(
'[data-suspense-boundary]'
).
each
((
_
,
el
)
=>
{
const
id
=
$
(
el
).
attr
(
'data-suspense-boundary'
)
if
(
id
)
{
markers
.
push
(
id
)
}
})
return
markers
}
describe
(
'basic functionality'
,
()
=>
{
it
(
'should inject suspense boundary data script'
,
async
()
=>
{
const
res
=
await
next
.
fetch
(
'/'
)
const
html
=
await
res
.
text
()
// Check that the script tag exists
expect
(
html
).
toContain
(
'__NEXT_SUSPENSE_BOUNDARIES__'
)
expect
(
html
).
toContain
(
'type="application/json"'
)
const
data
=
parseBoundaryData
(
html
)
expect
(
data
).
not
.
toBeNull
()
expect
(
data
?.
boundaries
).
toBeDefined
()
expect
(
Array
.
isArray
(
data
?.
boundaries
)).
toBe
(
true
)
expect
(
data
?.
timestamp
).
toBeGreaterThan
(
0
)
})
it
(
'should track multiple suspense boundaries'
,
async
()
=>
{
const
res
=
await
next
.
fetch
(
'/'
)
const
html
=
await
res
.
text
()
const
data
=
parseBoundaryData
(
html
)
expect
(
data
).
not
.
toBeNull
()
// The page has at least 6 Suspense boundaries:
// - outer (wrapping entire content)
// - header
// - sidebar
// - sidebar-nested
// - main
// - footer
expect
(
data
!
.
boundaries
.
length
).
toBeGreaterThanOrEqual
(
6
)
})
it
(
'should have boundaries with source field (null without __source props)'
,
async
()
=>
{
const
res
=
await
next
.
fetch
(
'/'
)
const
html
=
await
res
.
text
()
const
data
=
parseBoundaryData
(
html
)
expect
(
data
).
not
.
toBeNull
()
// All boundaries should have a source field (even if null)
// Source location requires __source props which aren't available in production
for
(
const
boundary
of
data
!
.
boundaries
)
{
expect
(
boundary
).
toHaveProperty
(
'source'
)
}
})
it
(
'should inject hidden marker elements in DOM'
,
async
()
=>
{
const
res
=
await
next
.
fetch
(
'/'
)
const
html
=
await
res
.
text
()
const
markers
=
getBoundaryMarkers
(
html
)
expect
(
markers
.
length
).
toBeGreaterThan
(
0
)
// Each marker should have a unique ID
const
uniqueMarkers
=
new
Set
(
markers
)
expect
(
uniqueMarkers
.
size
).
toBe
(
markers
.
length
)
// Check that markers have the content state
const
$
=
cheerio
.
load
(
html
)
$
(
'[data-suspense-boundary]'
).
each
((
_
,
el
)
=>
{
expect
(
$
(
el
).
attr
(
'data-suspense-state'
)).
toBe
(
'content'
)
expect
(
$
(
el
).
attr
(
'hidden'
)).
toBeDefined
()
})
})
})
describe
(
'nested boundaries'
,
()
=>
{
it
(
'should track parent-child relationships'
,
async
()
=>
{
const
res
=
await
next
.
fetch
(
'/nested'
)
const
html
=
await
res
.
text
()
const
data
=
parseBoundaryData
(
html
)
expect
(
data
).
not
.
toBeNull
()
// Find boundaries that have children
const
boundariesWithChildren
=
data
!
.
boundaries
.
filter
(
(
b
)
=>
b
.
children
.
length
>
0
)
expect
(
boundariesWithChildren
.
length
).
toBeGreaterThan
(
0
)
// Find boundaries that have parents
const
boundariesWithParents
=
data
!
.
boundaries
.
filter
(
(
b
)
=>
b
.
parentId
!==
null
)
expect
(
boundariesWithParents
.
length
).
toBeGreaterThan
(
0
)
})
it
(
'should track deeply nested boundaries (level1 -> level2 -> level3)'
,
async
()
=>
{
const
res
=
await
next
.
fetch
(
'/nested'
)
const
html
=
await
res
.
text
()
const
data
=
parseBoundaryData
(
html
)
expect
(
data
).
not
.
toBeNull
()
// We should have at least 3 levels of nesting plus siblings
// - level1 (contains level2)
// - level2 (contains level3)
// - level3
// - sibling-a, sibling-b, sibling-c
expect
(
data
!
.
boundaries
.
length
).
toBeGreaterThanOrEqual
(
6
)
// Build a map for easier lookup
const
boundaryMap
=
new
Map
(
data
!
.
boundaries
.
map
((
b
)
=>
[
b
.
id
,
b
]))
// Check that parent-child relationships form a valid tree
for
(
const
boundary
of
data
!
.
boundaries
)
{
if
(
boundary
.
parentId
!==
null
)
{
const
parent
=
boundaryMap
.
get
(
boundary
.
parentId
)
expect
(
parent
).
toBeDefined
()
expect
(
parent
!
.
children
).
toContain
(
boundary
.
id
)
}
}
})
it
(
'should track sibling boundaries at the same level'
,
async
()
=>
{
const
res
=
await
next
.
fetch
(
'/nested'
)
const
html
=
await
res
.
text
()
const
data
=
parseBoundaryData
(
html
)
expect
(
data
).
not
.
toBeNull
()
// Find boundaries that share the same parent
const
parentGroups
=
new
Map
<
string
|
null
,
SuspenseBoundaryInfo
[]
>
()
for
(
const
boundary
of
data
!
.
boundaries
)
{
const
parent
=
boundary
.
parentId
if
(
!
parentGroups
.
has
(
parent
))
{
parentGroups
.
set
(
parent
,
[])
}
parentGroups
.
get
(
parent
)
!
.
push
(
boundary
)
}
// At least one parent should have multiple children (the siblings)
const
parentsWithMultipleChildren
=
Array
.
from
(
parentGroups
.
values
()).
filter
(
(
children
)
=>
children
.
length
>
1
)
expect
(
parentsWithMultipleChildren
.
length
).
toBeGreaterThan
(
0
)
})
})
describe
(
'boundary markers match data'
,
()
=>
{
it
(
'should have matching markers for each boundary in data'
,
async
()
=>
{
const
res
=
await
next
.
fetch
(
'/'
)
const
html
=
await
res
.
text
()
const
data
=
parseBoundaryData
(
html
)
const
markers
=
getBoundaryMarkers
(
html
)
expect
(
data
).
not
.
toBeNull
()
// Each boundary ID in data should have at least one corresponding marker
for
(
const
boundary
of
data
!
.
boundaries
)
{
expect
(
markers
).
toContain
(
boundary
.
id
)
}
})
})
})
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