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

Add support for optional catchall with new router (#38444)

Follow-up of #38439.

Found a small issue with booting `next start` that is now resolved.
Also added optional catchall routes support.



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 3da3df23
...@@ -10,8 +10,8 @@ export type PagesManifest = { [page: string]: string } ...@@ -10,8 +10,8 @@ export type PagesManifest = { [page: string]: string }
let edgeServerPages = {} let edgeServerPages = {}
let nodeServerPages = {} let nodeServerPages = {}
let edgeServerRootPaths = {} let edgeServerAppPaths = {}
let nodeServerRootPaths = {} let nodeServerAppPaths = {}
// This plugin creates a pages-manifest.json from page entrypoints. // This plugin creates a pages-manifest.json from page entrypoints.
// This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR // This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR
...@@ -42,7 +42,7 @@ export default class PagesManifestPlugin implements webpack.Plugin { ...@@ -42,7 +42,7 @@ export default class PagesManifestPlugin implements webpack.Plugin {
createAssets(compilation: any, assets: any) { createAssets(compilation: any, assets: any) {
const entrypoints = compilation.entrypoints const entrypoints = compilation.entrypoints
const pages: PagesManifest = {} const pages: PagesManifest = {}
const rootPaths: PagesManifest = {} const appPaths: PagesManifest = {}
for (const entrypoint of entrypoints.values()) { for (const entrypoint of entrypoints.values()) {
const pagePath = getRouteFromEntrypoint( const pagePath = getRouteFromEntrypoint(
...@@ -78,7 +78,7 @@ export default class PagesManifestPlugin implements webpack.Plugin { ...@@ -78,7 +78,7 @@ export default class PagesManifestPlugin implements webpack.Plugin {
file = normalizePathSep(file) file = normalizePathSep(file)
if (entrypoint.name.startsWith('app/')) { if (entrypoint.name.startsWith('app/')) {
rootPaths[pagePath] = file appPaths[pagePath] = file
} else { } else {
pages[pagePath] = file pages[pagePath] = file
} }
...@@ -88,10 +88,10 @@ export default class PagesManifestPlugin implements webpack.Plugin { ...@@ -88,10 +88,10 @@ export default class PagesManifestPlugin implements webpack.Plugin {
// we need to merge both pages to generate the full manifest. // we need to merge both pages to generate the full manifest.
if (this.isEdgeRuntime) { if (this.isEdgeRuntime) {
edgeServerPages = pages edgeServerPages = pages
edgeServerRootPaths = rootPaths edgeServerAppPaths = appPaths
} else { } else {
nodeServerPages = pages nodeServerPages = pages
nodeServerRootPaths = rootPaths nodeServerAppPaths = appPaths
} }
assets[ assets[
...@@ -113,8 +113,8 @@ export default class PagesManifestPlugin implements webpack.Plugin { ...@@ -113,8 +113,8 @@ export default class PagesManifestPlugin implements webpack.Plugin {
] = new sources.RawSource( ] = new sources.RawSource(
JSON.stringify( JSON.stringify(
{ {
...edgeServerRootPaths, ...edgeServerAppPaths,
...nodeServerRootPaths, ...nodeServerAppPaths,
}, },
null, null,
2 2
......
...@@ -299,6 +299,34 @@ export type ChildProp = { ...@@ -299,6 +299,34 @@ export type ChildProp = {
segment: Segment segment: Segment
} }
function getSegmentParam(segment: string): {
param: string
type: 'catchall' | 'optional-catchall' | 'dynamic'
} | null {
if (segment.startsWith('[[...') && segment.endsWith(']]')) {
return {
type: 'optional-catchall',
param: segment.slice(5, -2),
}
}
if (segment.startsWith('[...') && segment.endsWith(']')) {
return {
type: 'catchall',
param: segment.slice(4, -1),
}
}
if (segment.startsWith('[') && segment.endsWith(']')) {
return {
type: 'dynamic',
param: segment.slice(1, -1),
}
}
return null
}
export async function renderToHTML( export async function renderToHTML(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
...@@ -383,25 +411,35 @@ export async function renderToHTML( ...@@ -383,25 +411,35 @@ export async function renderToHTML(
segment: string segment: string
): { param: string; value: string } | null => { ): { param: string; value: string } | 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 = const segmentParam = getSegmentParam(segment)
segment.startsWith('[') && segment.endsWith(']') if (!segmentParam) {
? segment.slice(segment.startsWith('[...') ? 4 : 1, -1) return null
: null }
const key = segmentParam.param
if (!segmentParam || (!pathParams[segmentParam] && !query[segmentParam])) { if (!pathParams[key] && !query[key]) {
if (segmentParam.type === 'optional-catchall') {
return {
param: key,
value: '',
}
}
return null return null
} }
return { return {
param: segmentParam, param: key,
// @ts-expect-error TODO: handle case where value is an array
value: value:
// TODO: this should only read from `pathParams`. There's an inconsistency where `query` holds params currently which has to be fixed. // TODO: this should only read from `pathParams`. There's an inconsistency where `query` holds params currently which has to be fixed.
pathParams[segmentParam] ?? (Array.isArray(pathParams[key])
(Array.isArray(query[segmentParam]) ? // @ts-expect-error TODO: handle case where value is an array
pathParams[key].join('/')
: pathParams[key]) ??
(Array.isArray(query[key])
? // @ts-expect-error TODO: handle case where value is an array ? // @ts-expect-error TODO: handle case where value is an array
query[segmentParam].join('/') query[key].join('/')
: query[segmentParam]), : query[key]),
} }
} }
......
...@@ -36,6 +36,7 @@ import { format as formatUrl, parse as parseUrl } from 'url' ...@@ -36,6 +36,7 @@ import { format as formatUrl, parse as parseUrl } from 'url'
import { getRedirectStatus } from '../lib/load-custom-routes' import { getRedirectStatus } from '../lib/load-custom-routes'
import { import {
NEXT_BUILTIN_DOCUMENT, NEXT_BUILTIN_DOCUMENT,
NEXT_CLIENT_SSR_ENTRY_SUFFIX,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
SERVER_DIRECTORY, SERVER_DIRECTORY,
STATIC_STATUS_PAGES, STATIC_STATUS_PAGES,
...@@ -1026,33 +1027,14 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1026,33 +1027,14 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const appPathRoutes: Record<string, string> = {} const appPathRoutes: Record<string, string> = {}
Object.keys(this.appPathsManifest || {}).forEach((entry) => { Object.keys(this.appPathsManifest || {}).forEach((entry) => {
if (entry.endsWith(NEXT_CLIENT_SSR_ENTRY_SUFFIX)) {
return
}
appPathRoutes[normalizeAppPath(entry) || '/'] = entry appPathRoutes[normalizeAppPath(entry) || '/'] = entry
}) })
return appPathRoutes return appPathRoutes
} }
protected getAppPathLayouts(pathname: string): string[] {
const layoutPaths: string[] = []
if (this.appPathRoutes) {
const paths = Object.values(this.appPathRoutes)
const parts = pathname.split('/').filter(Boolean)
for (let i = 1; i < parts.length; i++) {
const layoutPath = `/${parts.slice(0, i).join('/')}/layout`
if (paths.includes(layoutPath)) {
layoutPaths.push(layoutPath)
}
}
if (this.appPathRoutes['/layout']) {
layoutPaths.unshift('/layout')
}
}
return layoutPaths
}
protected async run( protected async run(
req: BaseNextRequest, req: BaseNextRequest,
res: BaseNextResponse, res: BaseNextResponse,
......
...@@ -28,9 +28,6 @@ export function getPagePath( ...@@ -28,9 +28,6 @@ export function getPagePath(
let rootPathsManifest: undefined | PagesManifest let rootPathsManifest: undefined | PagesManifest
if (appDirEnabled) { if (appDirEnabled) {
if (page === '/_root') {
return join(serverBuildPath, 'root.js')
}
rootPathsManifest = require(join(serverBuildPath, APP_PATHS_MANIFEST)) rootPathsManifest = require(join(serverBuildPath, APP_PATHS_MANIFEST))
} }
const pagesManifest = require(join( const pagesManifest = require(join(
......
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