Unverified Commit cd5d2210 authored by Josh Story's avatar Josh Story Committed by GitHub

Allow for parsing app route strings with and without slot names. (#92327)

The old impl has a boolean arg that was always true. To support some
fixes to routing I need to make the parser sometimes allow for slots but
still not things like route groups. I am generalizing the function
implementation to have a filter type that decides whether the input
string is allowed to have certain types such as route groups and slot
names. All the existing uses still disallow any non-url path segments.
parent e296bcb8
......@@ -1148,5 +1148,7 @@
"1147": "Turbopack database compaction is not supported on this platform",
"1148": "webToReadable cannot be used in the edge runtime",
"1149": "Node.js Readable cannot be teed in the edge runtime",
"1150": "Node.js Readable cannot be converted to a web stream in the edge runtime"
"1150": "Node.js Readable cannot be converted to a web stream in the edge runtime",
"1151": "%s is being parsed as a normalized route, but it has a route group segment.",
"1152": "%s is being parsed as a normalized route, but it has a parallel route segment."
}
......@@ -80,7 +80,7 @@ import type {
} from '../server/route-modules/app-route/module'
import type { FunctionsConfigManifest, ManifestRoute } from './index'
import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex'
import { parseAppRoute } from '../shared/lib/router/routes/app'
import { parseNormalizedAppRoute } from '../shared/lib/router/routes/app'
import { fillMetadataSegment } from '../lib/metadata/get-metadata-route'
import { STATIC_METADATA_IMAGES } from '../lib/metadata/is-metadata-route'
......@@ -836,7 +836,7 @@ export async function isPageStatic({
appConfig.revalidate = 0
}
const route = parseAppRoute(page, true)
const route = parseNormalizedAppRoute(page)
// If the page is dynamic and we're not in edge runtime, then we need to
// build the static paths. The edge runtime doesn't support static
......
......@@ -4,7 +4,7 @@ import {
} from '../shared/lib/router/utils/get-segment-param'
import {
isInterceptionAppRoute,
parseAppRoute,
parseNormalizedAppRoute,
type NormalizedAppRoute,
type NormalizedAppRouteSegment,
} from '../shared/lib/router/routes/app'
......@@ -143,8 +143,8 @@ function validateAppRoute(route: NormalizedAppRoute): void {
*/
function parseAndValidateAppPath(path: string): NormalizedAppRoute {
// Fast parse the route information. We're expecting this to be a normalized
// route, so we pass `true` to the `parseAppRoute` function.
const route = parseAppRoute(path, true)
// route.
const route = parseNormalizedAppRoute(path)
// Slow walk the data from the route in order to validate it.
validateAppRoute(route)
......
......@@ -26,7 +26,7 @@ import { collectRootParamKeys } from '../../build/segment-config/app/collect-roo
import { buildAppStaticPaths } from '../../build/static-paths/app'
import { buildPagesStaticPaths } from '../../build/static-paths/pages'
import { createIncrementalCache } from '../../export/helpers/create-incremental-cache'
import { parseAppRoute } from '../../shared/lib/router/routes/app'
import { parseNormalizedAppRoute } from '../../shared/lib/router/routes/app'
type RuntimeConfig = {
pprConfig: ExperimentalPPRConfig | undefined
......@@ -119,7 +119,7 @@ export async function loadStaticPaths({
routeModule as AppPageRouteModule | AppRouteRouteModule
)
const route = parseAppRoute(pathname, true)
const route = parseNormalizedAppRoute(pathname)
if (route.dynamicSegments.length === 0) {
throw new InvariantError(
`Expected a dynamic route, but got a static route: ${pathname}`
......
......@@ -3,7 +3,7 @@ import type { FallbackRouteParam } from '../../build/static-paths/types'
import type { DynamicParamTypesShort } from '../../shared/lib/app-router-types'
import { dynamicParamTypes } from '../app-render/get-short-dynamic-param-type'
import type AppPageRouteModule from '../route-modules/app-page/module'
import { parseAppRoute } from '../../shared/lib/router/routes/app'
import { parseNormalizedAppRoute } from '../../shared/lib/router/routes/app'
import { extractPathnameRouteParamSegmentsFromLoaderTree } from '../../build/static-paths/app/extract-pathname-route-param-segments-from-loader-tree'
import { getParamProperties } from '../../shared/lib/router/utils/get-segment-param'
......@@ -119,7 +119,7 @@ export function getFallbackRouteParams(
page: string,
routeModule: AppPageRouteModule
) {
const route = parseAppRoute(page, true)
const route = parseNormalizedAppRoute(page)
// Extract the pathname-contributing segments from the loader tree. This
// mirrors the logic in buildAppStaticPaths where we determine which segments
......
......@@ -173,14 +173,17 @@ export function isInterceptionAppRoute(
)
}
export function parseAppRoute(
// Bitmask for which non-URL segment types to allow during parsing.
// By default, route groups and parallel routes are rejected because
// they should have been stripped by normalizeAppPath. These flags
// let callers opt in to allowing specific types.
const OnlyRoutableSegments = /* */ 0b00
const AllowParallelSegments = /* */ 0b01
const AllowGroupSegments = /* */ 0b10
function parseAppRouteImpl(
pathname: string,
normalized: true
): NormalizedAppRoute
export function parseAppRoute(pathname: string, normalized: false): AppRoute
export function parseAppRoute(
pathname: string,
normalized: boolean
allowedTypes: number
): AppRoute | NormalizedAppRoute {
const pathnameSegments = pathname.split('/').filter(Boolean)
......@@ -202,12 +205,20 @@ export function parseAppRoute(
}
if (
normalized &&
(appSegment.type === 'route-group' ||
appSegment.type === 'parallel-route')
appSegment.type === 'route-group' &&
!(allowedTypes & AllowGroupSegments)
) {
throw new InvariantError(
`${pathname} is being parsed as a normalized route, but it has a route group segment.`
)
}
if (
appSegment.type === 'parallel-route' &&
!(allowedTypes & AllowParallelSegments)
) {
throw new InvariantError(
`${pathname} is being parsed as a normalized route, but it has a route group or parallel route segment.`
`${pathname} is being parsed as a normalized route, but it has a parallel route segment.`
)
}
......@@ -219,12 +230,8 @@ export function parseAppRoute(
throw new Error(`Invalid interception route: ${pathname}`)
}
interceptingRoute = normalized
? parseAppRoute(parts[0], true)
: parseAppRoute(parts[0], false)
interceptedRoute = normalized
? parseAppRoute(parts[1], true)
: parseAppRoute(parts[1], false)
interceptingRoute = parseAppRouteImpl(parts[0], allowedTypes)
interceptedRoute = parseAppRouteImpl(parts[1], allowedTypes)
interceptionMarker = appSegment.interceptionMarker
}
}
......@@ -234,7 +241,7 @@ export function parseAppRoute(
)
return {
normalized,
normalized: allowedTypes === OnlyRoutableSegments,
pathname,
segments,
dynamicSegments,
......@@ -243,3 +250,21 @@ export function parseAppRoute(
interceptedRoute,
}
}
/**
* Parse an app route that has been fully normalized (no @slot or ()
* group segments). Throws if either is present.
*/
export function parseNormalizedAppRoute(pathname: string): NormalizedAppRoute {
return parseAppRouteImpl(pathname, OnlyRoutableSegments) as NormalizedAppRoute
}
/**
* Parse an app route that may contain @slot segments but not ()
* group segments. Slot segments are preserved as parallel-route
* type segments so callers can distinguish routes in different
* parallel slots.
*/
export function parseAppRouteWithSlots(pathname: string): AppRoute {
return parseAppRouteImpl(pathname, AllowParallelSegments) as AppRoute
}
......@@ -5,7 +5,7 @@ import type { Params } from '../../../../server/request/params'
import type { DynamicParamTypesShort } from '../../app-router-types'
import { InvariantError } from '../../invariant-error'
import { parseLoaderTree } from './parse-loader-tree'
import { parseAppRoute, parseAppRouteSegment } from '../routes/app'
import { parseNormalizedAppRoute, parseAppRouteSegment } from '../routes/app'
import { resolveParamValue } from './resolve-param-value'
/**
......@@ -53,7 +53,7 @@ export function interpolateParallelRouteParams(
]
// Parse the route from the provided page path.
const route = parseAppRoute(pagePath, true)
const route = parseNormalizedAppRoute(pagePath)
while (stack.length > 0) {
const { tree, depth } = stack.pop()!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment