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
e83ab18c
Unverified
Commit
e83ab18c
authored
Dec 16, 2024
by
Zack Tanner
Committed by
GitHub
Dec 16, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
backport: refactor collectAppPageSegments (#73996)
Backports: -
https://github.com/vercel/next.js/pull/73908
parent
ada25fc2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
20 deletions
+37
-20
app-segments.ts
packages/next/src/build/segment-config/app/app-segments.ts
+37
-20
No files found.
packages/next/src/build/segment-config/app/app-segments.ts
View file @
e83ab18c
...
@@ -20,7 +20,10 @@ import {
...
@@ -20,7 +20,10 @@ import {
}
from
'../../../server/route-modules/checks'
}
from
'../../../server/route-modules/checks'
import
{
isClientReference
}
from
'../../../lib/client-reference'
import
{
isClientReference
}
from
'../../../lib/client-reference'
import
{
getSegmentParam
}
from
'../../../server/app-render/get-segment-param'
import
{
getSegmentParam
}
from
'../../../server/app-render/get-segment-param'
import
{
getLayoutOrPageModule
}
from
'../../../server/lib/app-dir-module'
import
{
getLayoutOrPageModule
,
type
LoaderTree
,
}
from
'../../../server/lib/app-dir-module'
import
{
PAGE_SEGMENT_KEY
}
from
'../../../shared/lib/segment'
import
{
PAGE_SEGMENT_KEY
}
from
'../../../shared/lib/segment'
type
GenerateStaticParams
=
(
options
:
{
params
?:
Params
})
=>
Promise
<
Params
[]
>
type
GenerateStaticParams
=
(
options
:
{
params
?:
Params
})
=>
Promise
<
Params
[]
>
...
@@ -74,17 +77,21 @@ export type AppSegment = {
...
@@ -74,17 +77,21 @@ export type AppSegment = {
* @returns the segments for the app page route module
* @returns the segments for the app page route module
*/
*/
async
function
collectAppPageSegments
(
routeModule
:
AppPageRouteModule
)
{
async
function
collectAppPageSegments
(
routeModule
:
AppPageRouteModule
)
{
const
segments
:
AppSegment
[]
=
[]
// We keep track of unique segments, since with parallel routes, it's possible
// to see the same segment multiple times.
const
uniqueSegments
=
new
Map
<
string
,
AppSegment
>
()
// Queue will store tuples of [loaderTree, currentSegments]
type
QueueItem
=
[
LoaderTree
,
AppSegment
[]]
const
queue
:
QueueItem
[]
=
[[
routeModule
.
userland
.
loaderTree
,
[]]]
// Helper function to process a loader tree path
while
(
queue
.
length
>
0
)
{
async
function
processLoaderTree
(
const
[
loaderTree
,
currentSegments
]
=
queue
.
shift
()
!
loaderTree
:
any
,
currentSegments
:
AppSegment
[]
=
[]
):
Promise
<
void
>
{
const
[
name
,
parallelRoutes
]
=
loaderTree
const
[
name
,
parallelRoutes
]
=
loaderTree
const
{
mod
:
userland
,
filePath
}
=
await
getLayoutOrPageModule
(
loaderTree
)
const
isClientComponent
:
boolean
=
userland
&&
isClientReference
(
userland
)
// Process current node
const
{
mod
:
userland
,
filePath
}
=
await
getLayoutOrPageModule
(
loaderTree
)
const
isClientComponent
=
userland
&&
isClientReference
(
userland
)
const
isDynamicSegment
=
/
\[
.*
\]
$/
.
test
(
name
)
const
isDynamicSegment
=
/
\[
.*
\]
$/
.
test
(
name
)
const
param
=
isDynamicSegment
?
getSegmentParam
(
name
)?.
param
:
undefined
const
param
=
isDynamicSegment
?
getSegmentParam
(
name
)?.
param
:
undefined
...
@@ -97,30 +104,40 @@ async function collectAppPageSegments(routeModule: AppPageRouteModule) {
...
@@ -97,30 +104,40 @@ async function collectAppPageSegments(routeModule: AppPageRouteModule) {
generateStaticParams
:
undefined
,
generateStaticParams
:
undefined
,
}
}
// Only server components can have app segment configurations. If this isn't
// Only server components can have app segment configurations
// an object, then we should skip it. This can happen when parsing the
// error components.
if
(
!
isClientComponent
)
{
if
(
!
isClientComponent
)
{
attach
(
segment
,
userland
,
routeModule
.
definition
.
pathname
)
attach
(
segment
,
userland
,
routeModule
.
definition
.
pathname
)
}
}
currentSegments
.
push
(
segment
)
// Create a unique key for the segment
const
segmentKey
=
getSegmentKey
(
segment
)
if
(
!
uniqueSegments
.
has
(
segmentKey
))
{
uniqueSegments
.
set
(
segmentKey
,
segment
)
}
const
updatedSegments
=
[...
currentSegments
,
segment
]
// If this is a page segment, we know we've reached a leaf node associated with the
// If this is a page segment, we've reached a leaf node
// page we're collecting segments for. We can add the collected segments to our final result.
if
(
name
===
PAGE_SEGMENT_KEY
)
{
if
(
name
===
PAGE_SEGMENT_KEY
)
{
segments
.
push
(...
currentSegments
)
// Add all segments in the current path
updatedSegments
.
forEach
((
seg
)
=>
{
const
key
=
getSegmentKey
(
seg
)
uniqueSegments
.
set
(
key
,
seg
)
})
}
}
//
Recursively process parallel routes
//
Add all parallel routes to the queue
for
(
const
parallelRouteKey
in
parallelRoutes
)
{
for
(
const
parallelRouteKey
in
parallelRoutes
)
{
const
parallelRoute
=
parallelRoutes
[
parallelRouteKey
]
const
parallelRoute
=
parallelRoutes
[
parallelRouteKey
]
await
processLoaderTree
(
parallelRoute
,
[...
current
Segments
])
queue
.
push
([
parallelRoute
,
updated
Segments
])
}
}
}
}
await
processLoaderTree
(
routeModule
.
userland
.
loaderTree
)
return
Array
.
from
(
uniqueSegments
.
values
())
return
segments
}
function
getSegmentKey
(
segment
:
AppSegment
)
{
return
`
${
segment
.
name
}
-
${
segment
.
filePath
??
''
}
-
$
{
segment
.
param
??
''
}
`
}
}
/**
/**
...
...
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