Unverified Commit b0143432 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Handle dynamic routes / catchall routes in on-demand-entries (#38512)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent d2eb7861
...@@ -248,7 +248,30 @@ function createServerComponentRenderer( ...@@ -248,7 +248,30 @@ function createServerComponentRenderer(
return ServerComponentWrapper return ServerComponentWrapper
} }
export type Segment = string | [param: string, value: string] type DynamicParamTypes = 'catchall' | 'optional-catchall' | 'dynamic'
// c = catchall
// oc = optional catchall
// d = dynamic
export type DynamicParamTypesShort = 'c' | 'oc' | 'd'
function getShortDynamicParamType(
type: DynamicParamTypes
): DynamicParamTypesShort {
switch (type) {
case 'catchall':
return 'c'
case 'optional-catchall':
return 'oc'
case 'dynamic':
return 'd'
default:
throw new Error('Unknown dynamic param type')
}
}
export type Segment =
| string
| [param: string, value: string, type: DynamicParamTypesShort]
type LoaderTree = [ type LoaderTree = [
segment: string, segment: string,
...@@ -301,7 +324,7 @@ export type ChildProp = { ...@@ -301,7 +324,7 @@ export type ChildProp = {
function getSegmentParam(segment: string): { function getSegmentParam(segment: string): {
param: string param: string
type: 'catchall' | 'optional-catchall' | 'dynamic' type: DynamicParamTypes
} | null { } | null {
if (segment.startsWith('[[...') && segment.endsWith(']]')) { if (segment.startsWith('[[...') && segment.endsWith(']]')) {
return { return {
...@@ -416,6 +439,7 @@ export async function renderToHTML( ...@@ -416,6 +439,7 @@ export async function renderToHTML(
param: string param: string
value: string | string[] | null value: string | string[] | null
treeValue: string treeValue: string
type: DynamicParamTypesShort
} | null => { } | null => {
// TODO: use correct matching for dynamic routes to get segment param // TODO: use correct matching for dynamic routes to get segment param
const segmentParam = getSegmentParam(segment) const segmentParam = getSegmentParam(segment)
...@@ -431,6 +455,7 @@ export async function renderToHTML( ...@@ -431,6 +455,7 @@ export async function renderToHTML(
return { return {
param: key, param: key,
value: null, value: null,
type: getShortDynamicParamType(segmentParam.type),
treeValue: '', treeValue: '',
} }
} }
...@@ -441,6 +466,7 @@ export async function renderToHTML( ...@@ -441,6 +466,7 @@ export async function renderToHTML(
param: key, param: key,
value: value, value: value,
treeValue: Array.isArray(value) ? value.join('/') : value, treeValue: Array.isArray(value) ? value.join('/') : value,
type: getShortDynamicParamType(segmentParam.type),
} }
} }
...@@ -451,7 +477,9 @@ export async function renderToHTML( ...@@ -451,7 +477,9 @@ export async function renderToHTML(
const dynamicParam = getDynamicParamFromSegment(segment) const dynamicParam = getDynamicParamFromSegment(segment)
const segmentTree: FlightRouterState = [ const segmentTree: FlightRouterState = [
dynamicParam ? [dynamicParam.param, dynamicParam.treeValue] : segment, dynamicParam
? [dynamicParam.param, dynamicParam.treeValue, dynamicParam.type]
: segment,
{}, {},
] ]
...@@ -539,7 +567,11 @@ export async function renderToHTML( ...@@ -539,7 +567,11 @@ export async function renderToHTML(
const childProp: ChildProp = { const childProp: ChildProp = {
current: <ChildComponent />, current: <ChildComponent />,
segment: childSegmentParam segment: childSegmentParam
? [childSegmentParam.param, childSegmentParam.treeValue] ? [
childSegmentParam.param,
childSegmentParam.treeValue,
childSegmentParam.type,
]
: parallelRoutes[currentValue][0], : parallelRoutes[currentValue][0],
} }
...@@ -686,7 +718,7 @@ export async function renderToHTML( ...@@ -686,7 +718,7 @@ export async function renderToHTML(
} }
: parentParams : parentParams
const actualSegment: Segment = segmentParam const actualSegment: Segment = segmentParam
? [segmentParam.param, segmentParam.treeValue] ? [segmentParam.param, segmentParam.treeValue, segmentParam.type]
: segment : segment
const renderComponentsOnThisLevel = const renderComponentsOnThisLevel =
......
...@@ -15,7 +15,7 @@ import { serverComponentRegex } from '../../build/webpack/loaders/utils' ...@@ -15,7 +15,7 @@ import { serverComponentRegex } from '../../build/webpack/loaders/utils'
import { getPageStaticInfo } from '../../build/analysis/get-page-static-info' import { getPageStaticInfo } from '../../build/analysis/get-page-static-info'
import { isMiddlewareFile, isMiddlewareFilename } from '../../build/utils' import { isMiddlewareFile, isMiddlewareFilename } from '../../build/utils'
import { PageNotFoundError } from '../../shared/lib/utils' import { PageNotFoundError } from '../../shared/lib/utils'
import { FlightRouterState } from '../app-render' import { DynamicParamTypesShort, FlightRouterState } from '../app-render'
function treePathToEntrypoint( function treePathToEntrypoint(
segmentPath: string[], segmentPath: string[],
...@@ -38,6 +38,22 @@ function treePathToEntrypoint( ...@@ -38,6 +38,22 @@ function treePathToEntrypoint(
return treePathToEntrypoint(childSegmentPath, path) return treePathToEntrypoint(childSegmentPath, path)
} }
function convertDynamicParamTypeToSyntax(
dynamicParamTypeShort: DynamicParamTypesShort,
param: string
) {
switch (dynamicParamTypeShort) {
case 'c':
return `[...${param}]`
case 'oc':
return `[[...${param}]]`
case 'd':
return `[${param}]`
default:
throw new Error('Unknown dynamic param type')
}
}
function getEntrypointsFromTree( function getEntrypointsFromTree(
tree: FlightRouterState, tree: FlightRouterState,
isFirst: boolean, isFirst: boolean,
...@@ -45,7 +61,9 @@ function getEntrypointsFromTree( ...@@ -45,7 +61,9 @@ function getEntrypointsFromTree(
) { ) {
const [segment, parallelRoutes] = tree const [segment, parallelRoutes] = tree
const currentSegment = Array.isArray(segment) ? segment[0] : segment const currentSegment = Array.isArray(segment)
? convertDynamicParamTypeToSyntax(segment[2], segment[0])
: segment
const currentPath = [...parentPath, currentSegment] const currentPath = [...parentPath, currentSegment]
......
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