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
0ca32b2b
Unverified
Commit
0ca32b2b
authored
May 16, 2025
by
Jiachi Liu
Committed by
GitHub
May 16, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[devtool] initial support for segment explorer (#78858)
parent
3a7ea695
Hide whitespace changes
Inline
Side-by-side
Showing
28 changed files
with
587 additions
and
22 deletions
+587
-22
define-env.ts
packages/next/src/build/define-env.ts
+2
-0
dev-tools-indicator.tsx
...onents/errors/dev-tools-indicator/dev-tools-indicator.tsx
+22
-0
dev-tools-info.tsx
...ors/dev-tools-indicator/dev-tools-info/dev-tools-info.tsx
+27
-20
segment-explorer.tsx
...t-dev-overlay/ui/components/overview/segment-explorer.tsx
+204
-0
component-styles.tsx
...mponents/react-dev-overlay/ui/styles/component-styles.tsx
+2
-0
create-component-tree.tsx
...ages/next/src/server/app-render/create-component-tree.tsx
+48
-2
entry-base.ts
packages/next/src/server/app-render/entry-base.ts
+9
-0
types.ts
packages/next/src/server/app-render/types.ts
+6
-0
base-server.ts
packages/next/src/server/base-server.ts
+3
-0
config-schema.ts
packages/next/src/server/config-schema.ts
+1
-0
config-shared.ts
packages/next/src/server/config-shared.ts
+6
-0
app-segment-tree.tsx
packages/next/src/shared/lib/devtool/app-segment-tree.tsx
+101
-0
trie.ts
packages/next/src/shared/lib/devtool/trie.ts
+63
-0
layout.tsx
test/development/app-dir/segment-explorer/app/layout.tsx
+9
-0
default.tsx
...dir/segment-explorer/app/parallel-routes/@bar/default.tsx
+3
-0
layout.tsx
...-dir/segment-explorer/app/parallel-routes/@bar/layout.tsx
+8
-0
page.tsx
...pp-dir/segment-explorer/app/parallel-routes/@bar/page.tsx
+3
-0
page.tsx
...ment-explorer/app/parallel-routes/@bar/test-page/page.tsx
+3
-0
default.tsx
...dir/segment-explorer/app/parallel-routes/@foo/default.tsx
+3
-0
layout.tsx
...-dir/segment-explorer/app/parallel-routes/@foo/layout.tsx
+8
-0
page.tsx
...segment-explorer/app/parallel-routes/@foo/no-bar/page.tsx
+3
-0
page.tsx
...pp-dir/segment-explorer/app/parallel-routes/@foo/page.tsx
+3
-0
page.tsx
...ment-explorer/app/parallel-routes/@foo/test-page/page.tsx
+3
-0
layout.tsx
...t/app-dir/segment-explorer/app/parallel-routes/layout.tsx
+10
-0
page.tsx
...-dir/segment-explorer/app/parallel-routes/no-bar/page.tsx
+10
-0
page.tsx
...ent/app-dir/segment-explorer/app/parallel-routes/page.tsx
+14
-0
page.tsx
...r/segment-explorer/app/parallel-routes/test-page/page.tsx
+3
-0
next.config.js
test/development/app-dir/segment-explorer/next.config.js
+10
-0
No files found.
packages/next/src/build/define-env.ts
View file @
0ca32b2b
...
...
@@ -279,6 +279,8 @@ export function getDefineEnv({
'process.env.__NEXT_RELATIVE_DIST_DIR'
:
config
.
distDir
,
}
:
{}),
'process.env.__NEXT_DEVTOOL_SEGMENT_EXPLORER'
:
config
.
experimental
.
devtoolSegmentExplorer
??
false
,
}
const
userDefines
=
config
.
compiler
?.
define
??
{}
...
...
packages/next/src/client/components/react-dev-overlay/ui/components/errors/dev-tools-indicator/dev-tools-indicator.tsx
View file @
0ca32b2b
...
...
@@ -11,6 +11,7 @@ import { TurbopackInfo } from './dev-tools-info/turbopack-info'
import
{
RouteInfo
}
from
'./dev-tools-info/route-info'
import
GearIcon
from
'../../../icons/gear-icon'
import
{
UserPreferences
}
from
'./dev-tools-info/user-preferences'
import
{
SegmentsExplorer
}
from
'../../overview/segment-explorer'
import
{
MENU_CURVE
,
MENU_DURATION_MS
,
...
...
@@ -80,6 +81,7 @@ const OVERLAYS = {
Turbo
:
'turbo'
,
Route
:
'route'
,
Preferences
:
'preferences'
,
SegmentExplorer
:
'segment-explorer'
,
}
as
const
export
type
Overlays
=
(
typeof
OVERLAYS
)[
keyof
typeof
OVERLAYS
]
...
...
@@ -123,6 +125,7 @@ function DevToolsPopover({
const
isTurbopackInfoOpen
=
open
===
OVERLAYS
.
Turbo
const
isRouteInfoOpen
=
open
===
OVERLAYS
.
Route
const
isPreferencesOpen
=
open
===
OVERLAYS
.
Preferences
const
isSegmentExplorerOpen
=
open
===
OVERLAYS
.
SegmentExplorer
const
{
mounted
:
menuMounted
,
rendered
:
menuRendered
}
=
useDelayedRender
(
isMenuOpen
,
...
...
@@ -325,6 +328,16 @@ function DevToolsPopover({
setScale=
{
setScale
}
/>
{
/* Page Segment Explorer */
}
{
process
.
env
.
__NEXT_DEVTOOL_SEGMENT_EXPLORER
?
(
<
SegmentsExplorer
isOpen=
{
isSegmentExplorerOpen
}
close=
{
openRootMenu
}
triggerRef=
{
triggerRef
}
style=
{
popover
}
/>
)
:
null
}
{
/* Dropdown Menu */
}
{
menuMounted
&&
(
<
div
...
...
@@ -390,6 +403,15 @@ function DevToolsPopover({
onClick=
{
()
=>
setOpen
(
OVERLAYS
.
Preferences
)
}
index=
{
isTurbopack
?
2
:
3
}
/>
{
process
.
env
.
__NEXT_DEVTOOL_SEGMENT_EXPLORER
?
(
<
MenuItem
data
-
rendered
-
files
label=
"Segment Explorer"
value=
{
<
ChevronRight
/>
}
onClick=
{
()
=>
setOpen
(
OVERLAYS
.
SegmentExplorer
)
}
index=
{
isTurbopack
?
3
:
4
}
/>
)
:
null
}
</
div
>
</
Context
.
Provider
>
</
div
>
...
...
packages/next/src/client/components/react-dev-overlay/ui/components/errors/dev-tools-indicator/dev-tools-info/dev-tools-info.tsx
View file @
0ca32b2b
...
...
@@ -9,9 +9,10 @@ export interface DevToolsInfoPropsCore {
}
export
interface
DevToolsInfoProps
extends
DevToolsInfoPropsCore
{
title
:
string
title
:
React
.
ReactNode
children
:
React
.
ReactNode
learnMoreLink
?:
string
closeButton
?:
boolean
}
export
function
DevToolsInfo
({
...
...
@@ -20,6 +21,7 @@ export function DevToolsInfo({
learnMoreLink
,
isOpen
,
triggerRef
,
closeButton
=
true
,
close
,
...
props
}:
DevToolsInfoProps
)
{
...
...
@@ -55,25 +57,29 @@ export function DevToolsInfo({
<
div
className=
"dev-tools-info-container"
>
<
h1
className=
"dev-tools-info-title"
>
{
title
}
</
h1
>
{
children
}
<
div
className=
"dev-tools-info-button-container"
>
<
button
ref=
{
closeButtonRef
}
className=
"dev-tools-info-close-button"
onClick=
{
close
}
>
Close
</
button
>
{
learnMoreLink
&&
(
<
a
className=
"dev-tools-info-learn-more-button"
href=
{
learnMoreLink
}
target=
"_blank"
rel=
"noreferrer noopener"
>
Learn More
</
a
>
)
}
</
div
>
{
(
closeButton
||
learnMoreLink
)
&&
(
<
div
className=
"dev-tools-info-button-container"
>
{
closeButton
?
(
<
button
ref=
{
closeButtonRef
}
className=
"dev-tools-info-close-button"
onClick=
{
close
}
>
Close
</
button
>
)
:
null
}
{
learnMoreLink
&&
(
<
a
className=
"dev-tools-info-learn-more-button"
href=
{
learnMoreLink
}
target=
"_blank"
rel=
"noreferrer noopener"
>
Learn More
</
a
>
)
}
</
div
>
)
}
</
div
>
</
div
>
)
...
...
@@ -112,6 +118,7 @@ export const DEV_TOOLS_INFO_STYLES = `
.dev-tools-info-container {
padding: 12px;
width: 100%;
}
.dev-tools-info-title {
...
...
packages/next/src/client/components/react-dev-overlay/ui/components/overview/segment-explorer.tsx
0 → 100644
View file @
0ca32b2b
import
type
{
HTMLProps
}
from
'react'
import
{
css
}
from
'../../../utils/css'
import
type
{
DevToolsInfoPropsCore
}
from
'../errors/dev-tools-indicator/dev-tools-info/dev-tools-info'
import
{
DevToolsInfo
}
from
'../errors/dev-tools-indicator/dev-tools-info/dev-tools-info'
import
{
cx
}
from
'../../utils/cx'
import
{
LeftArrow
}
from
'../../icons/left-arrow'
import
{
useSegmentTreeClientState
,
type
SegmentNode
,
}
from
'../../../../../../shared/lib/devtool/app-segment-tree'
import
type
{
Trie
,
TrieNode
}
from
'../../../../../../shared/lib/devtool/trie'
const
IconLayout
=
(
props
:
React
.
SVGProps
<
SVGSVGElement
>
)
=>
{
return
(
<
svg
{
...
props
}
viewBox=
"0 0 16 16"
fill=
"none"
xmlns=
"http://www.w3.org/2000/svg"
>
<
path
d=
"M16 12.5L15.9873 12.7559C15.8677 13.9323 14.9323 14.8677 13.7559 14.9873L13.5 15H2.5L2.24414 14.9873C1.06772 14.8677 0.132274 13.9323 0.0126953 12.7559L0 12.5V1H16V12.5ZM1.5 6.25488V12.5C1.5 13.0523 1.94772 13.5 2.5 13.5H4.99512V6.25488H1.5ZM6.24512 6.25488V13.5H13.5C14.0523 13.5 14.5 13.0523 14.5 12.5V6.25488H6.24512ZM1.5 5.00488H14.5V2.5H1.5V5.00488Z"
fill=
"currentColor"
/>
</
svg
>
)
}
const
IconPage
=
(
props
:
React
.
SVGProps
<
SVGSVGElement
>
)
=>
{
return
(
<
svg
{
...
props
}
viewBox=
"0 0 16 16"
fill=
"none"
strokeLinejoin=
"round"
xmlns=
"http://www.w3.org/2000/svg"
>
<
path
fillRule=
"evenodd"
clipRule=
"evenodd"
d=
"M14.5 6.5V13.5C14.5 14.8807 13.3807 16 12 16H4C2.61929 16 1.5 14.8807 1.5 13.5V1.5V0H3H8H9.08579C9.351 0 9.60536 0.105357 9.79289 0.292893L14.2071 4.70711C14.3946 4.89464 14.5 5.149 14.5 5.41421V6.5ZM13 6.5V13.5C13 14.0523 12.5523 14.5 12 14.5H4C3.44772 14.5 3 14.0523 3 13.5V1.5H8V5V6.5H9.5H13ZM9.5 2.12132V5H12.3787L9.5 2.12132Z"
fill=
"currentColor"
/>
</
svg
>
)
}
const
ICONS
=
{
layout
:
<
IconLayout
width=
{
16
}
/>,
page
:
<
IconPage
width=
{
16
}
/>,
}
function
PageSegmentTree
({
tree
}:
{
tree
:
Trie
<
SegmentNode
>
|
undefined
})
{
if
(
!
tree
)
{
return
null
}
return
(
<
div
className=
"segment-explorer-content"
>
<
PageSegmentTreeLayerPresentation
tree=
{
tree
}
node=
{
tree
.
getRoot
()
}
level=
{
0
}
/>
</
div
>
)
}
function
PageSegmentTreeLayerPresentation
({
tree
,
node
,
level
,
}:
{
tree
:
Trie
<
SegmentNode
>
node
:
TrieNode
<
SegmentNode
>
level
:
number
})
{
const
segments
=
node
.
value
?.
pagePath
?.
split
(
'/'
)
||
[]
const
fileName
=
segments
[
segments
.
length
-
1
]
||
''
const
nodeName
=
node
.
value
?.
type
const
pagePathPrefix
=
segments
.
slice
(
0
,
-
1
).
join
(
'/'
)
return
(
<
div
className=
"segment-explorer-item"
>
{
!
fileName
||
level
===
0
?
null
:
(
<
div
className=
"segment-explorer-item-row"
>
<
div
className=
"segment-explorer-line"
>
<
div
className=
{
`segment-explorer-line-text-${nodeName}`
}
>
<
span
className=
{
cx
(
'segment-explorer-line-icon'
,
`segment-explorer-line-icon-${nodeName}`
)
}
>
{
nodeName
===
'layout'
?
ICONS
.
layout
:
ICONS
.
page
}
</
span
>
{
pagePathPrefix
===
''
?
''
:
`${pagePathPrefix}/`
}
<
span
className=
"segment-explorer-filename-path"
>
{
fileName
}
</
span
>
</
div
>
</
div
>
</
div
>
)
}
<
div
className=
"tree-node-expanded-rendered-children"
>
{
Object
.
entries
(
node
.
children
).
map
(
([
key
,
child
])
=>
child
&&
(
<
PageSegmentTreeLayerPresentation
key=
{
key
}
tree=
{
tree
}
node=
{
child
}
level=
{
level
+
1
}
/>
)
)
}
</
div
>
</
div
>
)
}
export
function
SegmentsExplorer
(
props
:
DevToolsInfoPropsCore
&
HTMLProps
<
HTMLDivElement
>
)
{
const
ctx
=
useSegmentTreeClientState
()
if
(
!
ctx
)
{
return
null
}
return
(
<
DevToolsInfo
title=
{
<>
<
button
className=
"segment-explorer-back-button"
onClick=
{
props
.
close
}
>
<
LeftArrow
/>
</
button
>
{
'Segment Explorer'
}
</>
}
closeButton=
{
false
}
{
...
props
}
>
<
PageSegmentTree
tree=
{
ctx
.
tree
}
/>
</
DevToolsInfo
>
)
}
export
const
DEV_TOOLS_INFO_RENDER_FILES_STYLES
=
css
`
.segment-explorer-back-button {
margin-right: 12px;
color: var(--color-gray-1000);
}
.segment-explorer-back-button svg {
width: 20px;
height: 20px;
}
.segment-explorer-content {
overflow-y: auto;
padding: 0 12px;
font-size: var(--size-14);
}
.segment-explorer-item-row {
display: flex;
align-items: center;
gap: 8px;
padding: 2px 0;
}
.segment-explorer-filename-path {
display: inline-block;
&:hover {
color: var(--color-gray-1000);
text-decoration: none;
}
}
.segment-explorer-filename-path a {
color: inherit;
text-decoration: inherit;
}
.segment-explorer-line {
white-space: pre;
}
.segment-explorer-line-icon {
margin-right: 4px;
}
.segment-explorer-line-icon-page {
color: inherit;
}
.segment-explorer-line-icon-layout {
color: var(--color-gray-1-00);
}
.segment-explorer-line-text-page {
color: var(--color-blue-900);
font-weight: 500;
}
`
packages/next/src/client/components/react-dev-overlay/ui/styles/component-styles.tsx
View file @
0ca32b2b
...
...
@@ -21,6 +21,7 @@ import { DEV_TOOLS_INFO_STYLES } from '../components/errors/dev-tools-indicator/
import
{
DEV_TOOLS_INFO_TURBOPACK_INFO_STYLES
}
from
'../components/errors/dev-tools-indicator/dev-tools-info/turbopack-info'
import
{
DEV_TOOLS_INFO_ROUTE_INFO_STYLES
}
from
'../components/errors/dev-tools-indicator/dev-tools-info/route-info'
import
{
DEV_TOOLS_INFO_USER_PREFERENCES_STYLES
}
from
'../components/errors/dev-tools-indicator/dev-tools-info/user-preferences'
import
{
DEV_TOOLS_INFO_RENDER_FILES_STYLES
}
from
'../components/overview/segment-explorer'
import
{
FADER_STYLES
}
from
'../components/fader'
export
function
ComponentStyles
()
{
...
...
@@ -49,6 +50,7 @@ export function ComponentStyles() {
${DEV_TOOLS_INFO_TURBOPACK_INFO_STYLES}
${DEV_TOOLS_INFO_ROUTE_INFO_STYLES}
${DEV_TOOLS_INFO_USER_PREFERENCES_STYLES}
${DEV_TOOLS_INFO_RENDER_FILES_STYLES}
${FADER_STYLES}
`
}
</
style
>
...
...
packages/next/src/server/app-render/create-component-tree.tsx
View file @
0ca32b2b
...
...
@@ -95,6 +95,7 @@ async function createComponentTreeInternal({
renderOpts
:
{
nextConfigOutput
,
experimental
},
workStore
,
componentMod
:
{
SegmentViewNode
,
HTTPAccessFallbackBoundary
,
LayoutRouter
,
RenderFromTemplateContext
,
...
...
@@ -621,8 +622,26 @@ async function createComponentTreeInternal({
)
}
const
dir
=
ctx
.
renderOpts
.
dir
||
process
.
cwd
()
const
isSegmentViewEnabled
=
process
.
env
.
NODE_ENV
===
'development'
&&
ctx
.
renderOpts
.
devtoolSegmentExplorer
const
nodeName
=
modType
??
'page'
if
(
isPage
)
{
const
PageComponent
=
Component
const
PageComponent
=
isSegmentViewEnabled
?
(
pageProps
:
any
)
=>
{
return
(
<
SegmentViewNode
type=
{
nodeName
}
pagePath=
{
normalizePageOrLayoutFilePath
(
dir
,
layoutOrPagePath
)
}
>
<
Component
{
...
pageProps
}
/>
</
SegmentViewNode
>
)
}
:
Component
// Assign searchParams to props if this is a page
let
pageElement
:
React
.
ReactNode
if
(
isClientComponent
)
{
...
...
@@ -708,7 +727,18 @@ async function createComponentTreeInternal({
isPossiblyPartialResponse
,
]
}
else
{
const
SegmentComponent
=
Component
const
SegmentComponent
=
isSegmentViewEnabled
?
(
segmentProps
:
any
)
=>
{
return
(
<
SegmentViewNode
type=
{
nodeName
}
pagePath=
{
normalizePageOrLayoutFilePath
(
dir
,
layoutOrPagePath
)
}
>
<
Component
{
...
segmentProps
}
/>
</
SegmentViewNode
>
)
}
:
Component
const
isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot
=
rootLayoutAtThisLevel
&&
...
...
@@ -964,3 +994,19 @@ function getRootParamsImpl(
)
}
}
function
normalizePageOrLayoutFilePath
(
projectDir
:
string
,
layoutOrPagePath
:
string
|
undefined
)
{
const
dir
=
projectDir
/*ctx.renderOpts.dir*/
||
process
.
cwd
()
const
relativePath
=
(
layoutOrPagePath
||
''
)
// remove turbopack [project] prefix
.
replace
(
/^
\[
project
\][\\/]
/
,
''
)
// remove the project root from the path
.
replace
(
dir
,
''
)
// remove /(src/)?app/ dir prefix
.
replace
(
/^
[\\/](
src
[\\/])?
app
[\\/]
/
,
''
)
return
relativePath
}
packages/next/src/server/app-render/entry-base.ts
View file @
0ca32b2b
...
...
@@ -45,6 +45,13 @@ import { Postpone } from './rsc/postpone'
import
{
taintObjectReference
}
from
'./rsc/taint'
export
{
collectSegmentData
}
from
'./collect-segment-data'
let
SegmentViewNode
:
typeof
import
(
'../../shared/lib/devtool/app-segment-tree'
).
SegmentViewNode
=
()
=>
null
if
(
process
.
env
.
NODE_ENV
===
'development'
)
{
const
appSegmentTree
:
typeof
import
(
'../../shared/lib/devtool/app-segment-tree'
)
=
require
(
'../../shared/lib/devtool/app-segment-tree'
)
SegmentViewNode
=
appSegmentTree
.
SegmentViewNode
}
// patchFetch makes use of APIs such as `React.unstable_postpone` which are only available
// in the experimental channel of React, so export it from here so that it comes from the bundled runtime
function
patchFetch
()
{
...
...
@@ -78,4 +85,6 @@ export {
HTTPAccessFallbackBoundary
,
patchFetch
,
createMetadataComponents
,
// Development only
SegmentViewNode
,
}
packages/next/src/server/app-render/types.ts
View file @
0ca32b2b
...
...
@@ -179,6 +179,7 @@ export type ServerOnInstrumentationRequestError = (
)
=>
void
|
Promise
<
void
>
export
interface
RenderOptsPartial
{
dir
?:
string
previewProps
:
__ApiPreviewProps
|
undefined
err
?:
Error
|
null
dev
?:
boolean
...
...
@@ -269,6 +270,11 @@ export interface RenderOptsPartial {
* statically generated.
*/
doNotThrowOnEmptyStaticShell
?:
boolean
/**
* next config experimental.devtoolSegmentExplorer
*/
devtoolSegmentExplorer
?:
boolean
}
export
type
RenderOpts
=
LoadComponentsReturnType
<
AppPageModule
>
&
...
...
packages/next/src/server/base-server.ts
View file @
0ca32b2b
...
...
@@ -563,6 +563,7 @@ export default abstract class Server<
}
this
.
renderOpts
=
{
dir
:
this
.
dir
,
supportsDynamicResponse
:
true
,
trailingSlash
:
this
.
nextConfig
.
trailingSlash
,
deploymentId
:
this
.
nextConfig
.
deploymentId
,
...
...
@@ -614,6 +615,8 @@ export default abstract class Server<
onInstrumentationRequestError
:
this
.
instrumentationOnRequestError
.
bind
(
this
),
reactMaxHeadersLength
:
this
.
nextConfig
.
reactMaxHeadersLength
,
devtoolSegmentExplorer
:
this
.
nextConfig
.
experimental
.
devtoolSegmentExplorer
,
}
// Initialize next/config with the environment configuration
...
...
packages/next/src/server/config-schema.ts
View file @
0ca32b2b
...
...
@@ -497,6 +497,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
})
.
optional
(),
globalNotFound
:
z
.
boolean
().
optional
(),
devtoolSegmentExplorer
:
z
.
boolean
().
optional
(),
})
.
optional
(),
exportPathMap
:
z
...
...
packages/next/src/server/config-shared.ts
View file @
0ca32b2b
...
...
@@ -685,6 +685,11 @@ export interface ExperimentalConfig {
*
*/
globalNotFound
?:
boolean
/**
* Enable segment viewer for the app directory in dev tool.
*/
devtoolSegmentExplorer
?:
boolean
}
export
type
ExportPathMap
=
{
...
...
@@ -1382,6 +1387,7 @@ export const defaultConfig: NextConfig = {
useCache
:
undefined
,
slowModuleDetection
:
undefined
,
globalNotFound
:
false
,
devtoolSegmentExplorer
:
false
,
},
htmlLimitedBots
:
undefined
,
bundlePagesRouterDependencies
:
false
,
...
...
packages/next/src/shared/lib/devtool/app-segment-tree.tsx
0 → 100644
View file @
0ca32b2b
'use client'
import
{
type
ReactNode
,
useEffect
,
useSyncExternalStore
}
from
'react'
import
{
createTrie
,
type
Trie
}
from
'./trie'
export
type
SegmentNode
=
{
type
:
string
pagePath
:
string
}
type
DevtoolClientState
=
{
tree
?:
Trie
<
SegmentNode
>
}
const
DEFAULT_CLIENT_STATE
=
typeof
window
===
'undefined'
?
undefined
:
createTrie
<
SegmentNode
>
({
getKey
:
(
item
)
=>
item
.
pagePath
,
})
declare
global
{
interface
Window
{
__NEXT_DEVTOOLS_CLIENT_STATE
?:
DevtoolClientState
}
}
function
getSegmentTreeClientState
():
DevtoolClientState
{
if
(
typeof
window
===
'undefined'
)
{
return
{}
}
if
(
!
window
.
__NEXT_DEVTOOLS_CLIENT_STATE
)
{
window
.
__NEXT_DEVTOOLS_CLIENT_STATE
=
{
// Initial state
tree
:
DEFAULT_CLIENT_STATE
,
}
}
return
window
.
__NEXT_DEVTOOLS_CLIENT_STATE
}
const
listeners
=
typeof
window
===
'undefined'
?
null
:
new
Set
<
()
=>
void
>
()
const
createSegmentTreeStore
=
():
{
subscribe
:
(
callback
:
()
=>
void
)
=>
()
=>
void
getSnapshot
:
()
=>
DevtoolClientState
getServerSnapshot
:
()
=>
undefined
}
=>
{
if
(
typeof
window
===
'undefined'
)
{
return
{
subscribe
:
()
=>
()
=>
void
0
,
getSnapshot
:
()
=>
({}),
getServerSnapshot
:
()
=>
undefined
,
}
}
// return a store that can be used by useSyncExternalStore
return
{
subscribe
:
(
callback
)
=>
{
listeners
?.
add
(
callback
)
return
()
=>
listeners
?.
delete
(
callback
)
},
getSnapshot
:
()
=>
{
return
getSegmentTreeClientState
()
},
getServerSnapshot
:
()
=>
{
return
undefined
},
}
}
const
{
subscribe
,
getSnapshot
,
getServerSnapshot
}
=
createSegmentTreeStore
()
export
function
SegmentViewNode
({
type
,
pagePath
,
children
,
}:
{
type
:
string
pagePath
:
string
children
:
ReactNode
})
{
const
clientState
=
getSegmentTreeClientState
()
const
tree
=
clientState
.
tree
useEffect
(()
=>
{
if
(
!
tree
)
{
return
}
tree
.
insert
({
type
,
pagePath
,
})
},
[
type
,
pagePath
,
tree
])
return
children
}
export
function
useSegmentTreeClientState
()
{
const
state
=
useSyncExternalStore
(
subscribe
,
getSnapshot
,
getServerSnapshot
)
return
state
}
packages/next/src/shared/lib/devtool/trie.ts
0 → 100644
View file @
0ca32b2b
/**
* Trie data structure for storing and searching paths
*
* This can be used to store app router paths and search for them efficiently.
* e.g.
*
* [trie root]
* ├── layout.js
* ├── page.js
* ├── blog
* ├── layout.js
* ├── page.js
* ├── [slug]
* ├── layout.js
* ├── page.js
**/
export
type
TrieNode
<
Value
=
string
>
=
{
value
?:
Value
children
:
{
[
key
:
string
]:
TrieNode
<
Value
>
|
undefined
}
}
export
type
Trie
<
Value
=
string
>
=
{
insert
:
(
value
:
Value
)
=>
void
getRoot
:
()
=>
TrieNode
<
Value
>
}
export
function
createTrie
<
Value
=
string
>
({
getKey
=
(
k
)
=>
k
as
unknown
as
string
,
}:
{
getKey
:
(
k
:
Value
)
=>
string
}):
Trie
<
Value
>
{
const
root
:
TrieNode
<
Value
>
=
{
value
:
undefined
,
children
:
{},
}
function
insert
(
value
:
Value
)
{
let
currentNode
=
root
const
key
=
getKey
(
value
)
const
segments
=
key
.
split
(
'/'
)
for
(
const
segment
of
segments
)
{
if
(
!
currentNode
.
children
[
segment
])
{
currentNode
.
children
[
segment
]
=
{
// Skip value for intermediate nodes
children
:
{},
}
}
currentNode
=
currentNode
.
children
[
segment
]
}
currentNode
.
value
=
value
}
function
getRoot
():
TrieNode
<
Value
>
{
return
root
}
return
{
insert
,
getRoot
}
}
test/development/app-dir/segment-explorer/app/layout.tsx
0 → 100644
View file @
0ca32b2b
import
{
ReactNode
}
from
'react'
export
default
function
Root
({
children
}:
{
children
:
ReactNode
})
{
return
(
<
html
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/development/app-dir/segment-explorer/app/parallel-routes/@bar/default.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Page
()
{
return
'default @bar'
}
test/development/app-dir/segment-explorer/app/parallel-routes/@bar/layout.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Layout
({
children
})
{
return
(
<
div
>
<
h2
>
@bar Layout
</
h2
>
<
div
id=
"bar-children"
>
{
children
}
</
div
>
</
div
>
)
}
test/development/app-dir/segment-explorer/app/parallel-routes/@bar/page.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Page
()
{
return
'page @bar'
}
test/development/app-dir/segment-explorer/app/parallel-routes/@bar/test-page/page.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Page
()
{
return
'test-page @bar'
}
test/development/app-dir/segment-explorer/app/parallel-routes/@foo/default.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Page
()
{
return
'default @foo'
}
test/development/app-dir/segment-explorer/app/parallel-routes/@foo/layout.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Layout
({
children
})
{
return
(
<
div
>
<
h2
>
@foo Layout
</
h2
>
<
div
id=
"foo-children"
>
{
children
}
</
div
>
</
div
>
)
}
test/development/app-dir/segment-explorer/app/parallel-routes/@foo/no-bar/page.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Page
()
{
return
'no-bar @foo'
}
test/development/app-dir/segment-explorer/app/parallel-routes/@foo/page.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Page
()
{
return
'page @foo'
}
test/development/app-dir/segment-explorer/app/parallel-routes/@foo/test-page/page.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Page
()
{
return
'test-page @foo'
}
test/development/app-dir/segment-explorer/app/parallel-routes/layout.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
Layout
({
children
,
bar
,
foo
})
{
return
(
<
div
>
<
h1
>
Parallel Routes Layout
</
h1
>
<
div
id=
"nested-children"
>
{
children
}
</
div
>
<
div
id=
"foo-slot"
>
{
foo
}
</
div
>
<
div
id=
"bar-slot"
>
{
bar
}
</
div
>
</
div
>
)
}
test/development/app-dir/segment-explorer/app/parallel-routes/no-bar/page.tsx
0 → 100644
View file @
0ca32b2b
import
Link
from
'next/link'
export
default
function
Page
()
{
return
(
<
div
>
<
p
>
no bar
</
p
>
<
Link
href=
"/parallel-routes"
>
Back to /parallel-routes
</
Link
>
</
div
>
)
}
test/development/app-dir/segment-explorer/app/parallel-routes/page.tsx
0 → 100644
View file @
0ca32b2b
import
Link
from
'next/link'
export
default
function
Page
()
{
return
(
<
div
>
Hello from Nested
<
br
/>
<
Link
href=
"/parallel-routes/test-page"
>
To /parallel-routes/test-page
</
Link
>
<
br
/>
<
Link
href=
"/parallel-routes/no-bar"
>
To /parallel-routes/no-bar
</
Link
>
</
div
>
)
}
test/development/app-dir/segment-explorer/app/parallel-routes/test-page/page.tsx
0 → 100644
View file @
0ca32b2b
export
default
function
TestPage
()
{
return
'test page'
}
test/development/app-dir/segment-explorer/next.config.js
0 → 100644
View file @
0ca32b2b
/**
* @type {import('next').NextConfig}
*/
const
nextConfig
=
{
experimental
:
{
devtoolSegmentExplorer
:
true
,
},
}
module
.
exports
=
nextConfig
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