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 @@ ...@@ -1148,5 +1148,7 @@
"1147": "Turbopack database compaction is not supported on this platform", "1147": "Turbopack database compaction is not supported on this platform",
"1148": "webToReadable cannot be used in the edge runtime", "1148": "webToReadable cannot be used in the edge runtime",
"1149": "Node.js Readable cannot be teed 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 { ...@@ -80,7 +80,7 @@ import type {
} from '../server/route-modules/app-route/module' } from '../server/route-modules/app-route/module'
import type { FunctionsConfigManifest, ManifestRoute } from './index' import type { FunctionsConfigManifest, ManifestRoute } from './index'
import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex' 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 { fillMetadataSegment } from '../lib/metadata/get-metadata-route'
import { STATIC_METADATA_IMAGES } from '../lib/metadata/is-metadata-route' import { STATIC_METADATA_IMAGES } from '../lib/metadata/is-metadata-route'
...@@ -836,7 +836,7 @@ export async function isPageStatic({ ...@@ -836,7 +836,7 @@ export async function isPageStatic({
appConfig.revalidate = 0 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 // 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 // build the static paths. The edge runtime doesn't support static
......
...@@ -4,7 +4,7 @@ import { ...@@ -4,7 +4,7 @@ import {
} from '../shared/lib/router/utils/get-segment-param' } from '../shared/lib/router/utils/get-segment-param'
import { import {
isInterceptionAppRoute, isInterceptionAppRoute,
parseAppRoute, parseNormalizedAppRoute,
type NormalizedAppRoute, type NormalizedAppRoute,
type NormalizedAppRouteSegment, type NormalizedAppRouteSegment,
} from '../shared/lib/router/routes/app' } from '../shared/lib/router/routes/app'
...@@ -143,8 +143,8 @@ function validateAppRoute(route: NormalizedAppRoute): void { ...@@ -143,8 +143,8 @@ function validateAppRoute(route: NormalizedAppRoute): void {
*/ */
function parseAndValidateAppPath(path: string): NormalizedAppRoute { function parseAndValidateAppPath(path: string): NormalizedAppRoute {
// Fast parse the route information. We're expecting this to be a normalized // Fast parse the route information. We're expecting this to be a normalized
// route, so we pass `true` to the `parseAppRoute` function. // route.
const route = parseAppRoute(path, true) const route = parseNormalizedAppRoute(path)
// Slow walk the data from the route in order to validate it. // Slow walk the data from the route in order to validate it.
validateAppRoute(route) validateAppRoute(route)
......
...@@ -26,7 +26,7 @@ import { collectRootParamKeys } from '../../build/segment-config/app/collect-roo ...@@ -26,7 +26,7 @@ import { collectRootParamKeys } from '../../build/segment-config/app/collect-roo
import { buildAppStaticPaths } from '../../build/static-paths/app' import { buildAppStaticPaths } from '../../build/static-paths/app'
import { buildPagesStaticPaths } from '../../build/static-paths/pages' import { buildPagesStaticPaths } from '../../build/static-paths/pages'
import { createIncrementalCache } from '../../export/helpers/create-incremental-cache' 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 = { type RuntimeConfig = {
pprConfig: ExperimentalPPRConfig | undefined pprConfig: ExperimentalPPRConfig | undefined
...@@ -119,7 +119,7 @@ export async function loadStaticPaths({ ...@@ -119,7 +119,7 @@ export async function loadStaticPaths({
routeModule as AppPageRouteModule | AppRouteRouteModule routeModule as AppPageRouteModule | AppRouteRouteModule
) )
const route = parseAppRoute(pathname, true) const route = parseNormalizedAppRoute(pathname)
if (route.dynamicSegments.length === 0) { if (route.dynamicSegments.length === 0) {
throw new InvariantError( throw new InvariantError(
`Expected a dynamic route, but got a static route: ${pathname}` `Expected a dynamic route, but got a static route: ${pathname}`
......
...@@ -3,7 +3,7 @@ import type { FallbackRouteParam } from '../../build/static-paths/types' ...@@ -3,7 +3,7 @@ import type { FallbackRouteParam } from '../../build/static-paths/types'
import type { DynamicParamTypesShort } from '../../shared/lib/app-router-types' import type { DynamicParamTypesShort } from '../../shared/lib/app-router-types'
import { dynamicParamTypes } from '../app-render/get-short-dynamic-param-type' import { dynamicParamTypes } from '../app-render/get-short-dynamic-param-type'
import type AppPageRouteModule from '../route-modules/app-page/module' 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 { extractPathnameRouteParamSegmentsFromLoaderTree } from '../../build/static-paths/app/extract-pathname-route-param-segments-from-loader-tree'
import { getParamProperties } from '../../shared/lib/router/utils/get-segment-param' import { getParamProperties } from '../../shared/lib/router/utils/get-segment-param'
...@@ -119,7 +119,7 @@ export function getFallbackRouteParams( ...@@ -119,7 +119,7 @@ export function getFallbackRouteParams(
page: string, page: string,
routeModule: AppPageRouteModule routeModule: AppPageRouteModule
) { ) {
const route = parseAppRoute(page, true) const route = parseNormalizedAppRoute(page)
// Extract the pathname-contributing segments from the loader tree. This // Extract the pathname-contributing segments from the loader tree. This
// mirrors the logic in buildAppStaticPaths where we determine which segments // mirrors the logic in buildAppStaticPaths where we determine which segments
......
...@@ -173,14 +173,17 @@ export function isInterceptionAppRoute( ...@@ -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, pathname: string,
normalized: true allowedTypes: number
): NormalizedAppRoute
export function parseAppRoute(pathname: string, normalized: false): AppRoute
export function parseAppRoute(
pathname: string,
normalized: boolean
): AppRoute | NormalizedAppRoute { ): AppRoute | NormalizedAppRoute {
const pathnameSegments = pathname.split('/').filter(Boolean) const pathnameSegments = pathname.split('/').filter(Boolean)
...@@ -202,12 +205,20 @@ export function parseAppRoute( ...@@ -202,12 +205,20 @@ export function parseAppRoute(
} }
if ( if (
normalized && appSegment.type === 'route-group' &&
(appSegment.type === 'route-group' || !(allowedTypes & AllowGroupSegments)
appSegment.type === 'parallel-route') ) {
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( 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( ...@@ -219,12 +230,8 @@ export function parseAppRoute(
throw new Error(`Invalid interception route: ${pathname}`) throw new Error(`Invalid interception route: ${pathname}`)
} }
interceptingRoute = normalized interceptingRoute = parseAppRouteImpl(parts[0], allowedTypes)
? parseAppRoute(parts[0], true) interceptedRoute = parseAppRouteImpl(parts[1], allowedTypes)
: parseAppRoute(parts[0], false)
interceptedRoute = normalized
? parseAppRoute(parts[1], true)
: parseAppRoute(parts[1], false)
interceptionMarker = appSegment.interceptionMarker interceptionMarker = appSegment.interceptionMarker
} }
} }
...@@ -234,7 +241,7 @@ export function parseAppRoute( ...@@ -234,7 +241,7 @@ export function parseAppRoute(
) )
return { return {
normalized, normalized: allowedTypes === OnlyRoutableSegments,
pathname, pathname,
segments, segments,
dynamicSegments, dynamicSegments,
...@@ -243,3 +250,21 @@ export function parseAppRoute( ...@@ -243,3 +250,21 @@ export function parseAppRoute(
interceptedRoute, 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' ...@@ -5,7 +5,7 @@ import type { Params } from '../../../../server/request/params'
import type { DynamicParamTypesShort } from '../../app-router-types' import type { DynamicParamTypesShort } from '../../app-router-types'
import { InvariantError } from '../../invariant-error' import { InvariantError } from '../../invariant-error'
import { parseLoaderTree } from './parse-loader-tree' import { parseLoaderTree } from './parse-loader-tree'
import { parseAppRoute, parseAppRouteSegment } from '../routes/app' import { parseNormalizedAppRoute, parseAppRouteSegment } from '../routes/app'
import { resolveParamValue } from './resolve-param-value' import { resolveParamValue } from './resolve-param-value'
/** /**
...@@ -53,7 +53,7 @@ export function interpolateParallelRouteParams( ...@@ -53,7 +53,7 @@ export function interpolateParallelRouteParams(
] ]
// Parse the route from the provided page path. // Parse the route from the provided page path.
const route = parseAppRoute(pagePath, true) const route = parseNormalizedAppRoute(pagePath)
while (stack.length > 0) { while (stack.length > 0) {
const { tree, depth } = stack.pop()! 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