Skip to content

Commit ea1d029

Browse files
committed
use helpers
1 parent d5abc77 commit ea1d029

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

packages/next/src/next-devtools/dev-overlay/components/overview/segment-explorer.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import {
1010
} from './segment-boundary-trigger'
1111
import { Tooltip } from '../../../components/tooltip'
1212
import { useRef, useState } from 'react'
13-
14-
const BUILTIN_PREFIX = '__next_builtin__'
13+
import {
14+
BOUNDARY_PREFIX,
15+
BUILTIN_PREFIX,
16+
isBoundaryFile,
17+
normalizeBoundaryFilename,
18+
} from '../../../../server/app-render/segment-explorer-path'
1519

1620
const isFileNode = (node: SegmentTrieNode) => {
1721
return !!node.value?.type && !!node.value?.pagePath
@@ -142,12 +146,13 @@ function PageSegmentTreeLayerPresentation({
142146
filesChildrenKeys.forEach((childKey) => {
143147
const childNode = node.children[childKey]
144148
if (!childNode || !childNode.value) return
145-
if (childNode.value.type.startsWith('boundary:')) {
146-
const boundaryType = childNode.value.type.split(':')[1] as
147-
| 'not-found'
148-
| 'loading'
149-
| 'error'
150-
boundaries[boundaryType] = childNode.value.pagePath || null
149+
if (isBoundaryFile(childNode.value.type)) {
150+
const boundaryType = childNode.value.type.replace(BOUNDARY_PREFIX, '')
151+
152+
if (boundaryType in boundaries) {
153+
boundaries[boundaryType as keyof typeof boundaries] =
154+
childNode.value.pagePath || null
155+
}
151156
}
152157
})
153158

@@ -184,13 +189,13 @@ function PageSegmentTreeLayerPresentation({
184189
}
185190
// If it's boundary node, which marks the existence of the boundary not the rendered status,
186191
// we don't need to present in the rendered files.
187-
if (childNode.value.type.startsWith('boundary:')) {
192+
if (isBoundaryFile(childNode.value.type)) {
188193
return null
189194
}
190195
const filePath = childNode.value.pagePath
191196
const lastSegment = filePath.split('/').pop() || ''
192197
const isBuiltin = filePath.startsWith(BUILTIN_PREFIX)
193-
const fileName = lastSegment.replace(BUILTIN_PREFIX, '')
198+
const fileName = normalizeBoundaryFilename(lastSegment)
194199

195200
return (
196201
<span

packages/next/src/server/app-render/segment-explorer-path.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { LoaderTree } from '../lib/app-dir-module'
22

3+
export const BUILTIN_PREFIX = '__next_builtin__'
4+
35
export function normalizeConventionFilePath(
46
projectDir: string,
57
conventionPath: string | undefined
@@ -22,12 +24,24 @@ export function normalizeConventionFilePath(
2224
if (nextInternalPrefixRegex.test(relativePath)) {
2325
relativePath = relativePath.replace(nextInternalPrefixRegex, '')
2426
// Add a special prefix to let segment explorer know it's a built-in component
25-
relativePath = `__next_builtin__${relativePath}`
27+
relativePath = `${BUILTIN_PREFIX}${relativePath}`
2628
}
2729

2830
return relativePath
2931
}
3032

33+
export const BOUNDARY_SUFFIX = '@boundary'
34+
export function normalizeBoundaryFilename(filename: string) {
35+
return filename
36+
.replace(new RegExp(`^${BUILTIN_PREFIX}`), '')
37+
.replace(new RegExp(`${BOUNDARY_SUFFIX}$`), '')
38+
}
39+
40+
export const BOUNDARY_PREFIX = 'boundary:'
41+
export function isBoundaryFile(fileType: string) {
42+
return fileType.startsWith(BOUNDARY_PREFIX)
43+
}
44+
3145
export function getConventionPathByType(
3246
tree: LoaderTree,
3347
dir: string,

0 commit comments

Comments
 (0)