Unverified Commit 2989c8ae authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add app-path-routes manifest (#38420)

This outputs a separate manifest for leveraging during deploy to handle the new app outputs. Also ensures dynamic routes from `app` our output in the `routes-manifest` correctly along with fixing the `react-dom` import. 

x-ref: https://github.com/vercel/next.js/pull/37551
parent aa0ba3c3
...@@ -54,6 +54,8 @@ import { ...@@ -54,6 +54,8 @@ import {
SERVER_FILES_MANIFEST, SERVER_FILES_MANIFEST,
STATIC_STATUS_PAGES, STATIC_STATUS_PAGES,
MIDDLEWARE_MANIFEST, MIDDLEWARE_MANIFEST,
APP_PATHS_MANIFEST,
APP_PATH_ROUTES_MANIFEST,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { getSortedRoutes, isDynamicRoute } from '../shared/lib/router/utils' import { getSortedRoutes, isDynamicRoute } from '../shared/lib/router/utils'
import { __ApiPreviewProps } from '../server/api-utils' import { __ApiPreviewProps } from '../server/api-utils'
...@@ -114,6 +116,7 @@ import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex' ...@@ -114,6 +116,7 @@ import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex'
import { flatReaddir } from '../lib/flat-readdir' import { flatReaddir } from '../lib/flat-readdir'
import { RemotePattern } from '../shared/lib/image-config' import { RemotePattern } from '../shared/lib/image-config'
import { eventSwcPlugins } from '../telemetry/events/swc-plugins' import { eventSwcPlugins } from '../telemetry/events/swc-plugins'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
export type SsgRoute = { export type SsgRoute = {
initialRevalidateSeconds: number | false initialRevalidateSeconds: number | false
...@@ -385,10 +388,10 @@ export default async function build( ...@@ -385,10 +388,10 @@ export default async function build(
}) })
) )
let mappedappPaths: { [page: string]: string } | undefined let mappedAppPaths: { [page: string]: string } | undefined
if (appPaths && appDir) { if (appPaths && appDir) {
mappedappPaths = nextBuildSpan mappedAppPaths = nextBuildSpan
.traceChild('create-app-mapping') .traceChild('create-app-mapping')
.traceFn(() => .traceFn(() =>
createPagesMapping({ createPagesMapping({
...@@ -427,7 +430,7 @@ export default async function build( ...@@ -427,7 +430,7 @@ export default async function build(
rootDir: dir, rootDir: dir,
rootPaths: mappedRootPaths, rootPaths: mappedRootPaths,
appDir, appDir,
appPaths: mappedappPaths, appPaths: mappedAppPaths,
pageExtensions: config.pageExtensions, pageExtensions: config.pageExtensions,
}) })
) )
...@@ -574,21 +577,36 @@ export default async function build( ...@@ -574,21 +577,36 @@ export default async function build(
defaultLocale: string defaultLocale: string
localeDetection?: false localeDetection?: false
} }
} = nextBuildSpan.traceChild('generate-routes-manifest').traceFn(() => ({ } = nextBuildSpan.traceChild('generate-routes-manifest').traceFn(() => {
version: 3, const sortedRoutes = getSortedRoutes([
pages404: true, ...pageKeys,
basePath: config.basePath, ...Object.keys(mappedAppPaths || {}).map((key) =>
redirects: redirects.map((r: any) => buildCustomRoute(r, 'redirect')), normalizeAppPath(key)
headers: headers.map((r: any) => buildCustomRoute(r, 'header')), ),
dynamicRoutes: getSortedRoutes(pageKeys) ])
.filter(isDynamicRoute) const dynamicRoutes: Array<ReturnType<typeof pageToRoute>> = []
.map(pageToRoute), const staticRoutes: typeof dynamicRoutes = []
staticRoutes: getSortedRoutes(pageKeys)
.filter((page) => !isDynamicRoute(page) && !isReservedPage(page)) for (const route of sortedRoutes) {
.map(pageToRoute), if (isDynamicRoute(route)) {
dataRoutes: [], dynamicRoutes.push(pageToRoute(route))
i18n: config.i18n || undefined, } else if (!isReservedPage(route)) {
})) staticRoutes.push(pageToRoute(route))
}
}
return {
version: 3,
pages404: true,
basePath: config.basePath,
redirects: redirects.map((r: any) => buildCustomRoute(r, 'redirect')),
headers: headers.map((r: any) => buildCustomRoute(r, 'header')),
dynamicRoutes,
staticRoutes,
dataRoutes: [],
i18n: config.i18n || undefined,
}
})
if (rewrites.beforeFiles.length === 0 && rewrites.fallback.length === 0) { if (rewrites.beforeFiles.length === 0 && rewrites.fallback.length === 0) {
routesManifest.rewrites = rewrites.afterFiles.map((r: any) => routesManifest.rewrites = rewrites.afterFiles.map((r: any) =>
...@@ -689,6 +707,7 @@ export default async function build( ...@@ -689,6 +707,7 @@ export default async function build(
REACT_LOADABLE_MANIFEST, REACT_LOADABLE_MANIFEST,
config.optimizeFonts ? path.join(serverDir, FONT_MANIFEST) : null, config.optimizeFonts ? path.join(serverDir, FONT_MANIFEST) : null,
BUILD_ID_FILE, BUILD_ID_FILE,
appDir ? path.join(serverDir, APP_PATHS_MANIFEST) : null,
] ]
.filter(nonNullable) .filter(nonNullable)
.map((file) => path.join(config.distDir, file)), .map((file) => path.join(config.distDir, file)),
...@@ -1618,6 +1637,24 @@ export default async function build( ...@@ -1618,6 +1637,24 @@ export default async function build(
'utf8' 'utf8'
) )
if (appDir) {
const appPathsManifest = JSON.parse(
await promises.readFile(
path.join(distDir, serverDir, APP_PATHS_MANIFEST),
'utf8'
)
)
const appPathRoutes: Record<string, string> = {}
Object.keys(appPathsManifest).forEach((entry) => {
appPathRoutes[entry] = normalizeAppPath(entry) || '/'
})
await promises.writeFile(
path.join(distDir, APP_PATH_ROUTES_MANIFEST),
JSON.stringify(appPathRoutes, null, 2)
)
}
const middlewareManifest: MiddlewareManifest = JSON.parse( const middlewareManifest: MiddlewareManifest = JSON.parse(
await promises.readFile( await promises.readFile(
path.join(distDir, serverDir, MIDDLEWARE_MANIFEST), path.join(distDir, serverDir, MIDDLEWARE_MANIFEST),
......
...@@ -20,11 +20,13 @@ import { ...@@ -20,11 +20,13 @@ import {
import { isDynamicRoute } from '../shared/lib/router/utils' import { isDynamicRoute } from '../shared/lib/router/utils'
import { tryGetPreviewData } from './api-utils/node' import { tryGetPreviewData } from './api-utils/node'
import { htmlEscapeJsonString } from './htmlescape' import { htmlEscapeJsonString } from './htmlescape'
import { stripInternalQueries } from './utils' import { shouldUseReactRoot, stripInternalQueries } from './utils'
import { NextApiRequestCookies } from './api-utils' import { NextApiRequestCookies } from './api-utils'
import { matchSegment } from '../client/components/match-segments' import { matchSegment } from '../client/components/match-segments'
const ReactDOMServer = process.env.__NEXT_REACT_ROOT // this needs to be required lazily so that `next-server` can set
// the env before we require
const ReactDOMServer = shouldUseReactRoot
? require('react-dom/server.browser') ? require('react-dom/server.browser')
: require('react-dom/server') : require('react-dom/server')
......
...@@ -913,7 +913,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -913,7 +913,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const { useFileSystemPublicRoutes } = this.nextConfig const { useFileSystemPublicRoutes } = this.nextConfig
if (useFileSystemPublicRoutes) { if (useFileSystemPublicRoutes) {
this.appPathRoutes = this.getappPathRoutes() this.appPathRoutes = this.getAppPathRoutes()
this.dynamicRoutes = this.getDynamicRoutes() this.dynamicRoutes = this.getDynamicRoutes()
} }
...@@ -1022,7 +1022,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1022,7 +1022,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
.filter((item): item is RoutingItem => Boolean(item)) .filter((item): item is RoutingItem => Boolean(item))
} }
protected getappPathRoutes(): Record<string, string> { protected getAppPathRoutes(): Record<string, string> {
const appPathRoutes: Record<string, string> = {} const appPathRoutes: Record<string, string> = {}
Object.keys(this.appPathsManifest || {}).forEach((entry) => { Object.keys(this.appPathsManifest || {}).forEach((entry) => {
...@@ -1031,7 +1031,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1031,7 +1031,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return appPathRoutes return appPathRoutes
} }
protected getappPathLayouts(pathname: string): string[] { protected getAppPathLayouts(pathname: string): string[] {
const layoutPaths: string[] = [] const layoutPaths: string[] = []
if (this.appPathRoutes) { if (this.appPathRoutes) {
......
...@@ -5,6 +5,7 @@ export const PHASE_DEVELOPMENT_SERVER = 'phase-development-server' ...@@ -5,6 +5,7 @@ export const PHASE_DEVELOPMENT_SERVER = 'phase-development-server'
export const PHASE_TEST = 'phase-test' export const PHASE_TEST = 'phase-test'
export const PAGES_MANIFEST = 'pages-manifest.json' export const PAGES_MANIFEST = 'pages-manifest.json'
export const APP_PATHS_MANIFEST = 'app-paths-manifest.json' export const APP_PATHS_MANIFEST = 'app-paths-manifest.json'
export const APP_PATH_ROUTES_MANIFEST = 'app-path-routes-manifest.json'
export const BUILD_MANIFEST = 'build-manifest.json' export const BUILD_MANIFEST = 'build-manifest.json'
export const EXPORT_MARKER = 'export-marker.json' export const EXPORT_MARKER = 'export-marker.json'
export const EXPORT_DETAIL = 'export-detail.json' export const EXPORT_DETAIL = 'export-detail.json'
......
...@@ -80,8 +80,9 @@ describe('Switchable runtime', () => { ...@@ -80,8 +80,9 @@ describe('Switchable runtime', () => {
.click() .click()
.waitForElementByCss('.node-rsc-ssr') .waitForElementByCss('.node-rsc-ssr')
expect(await browser.elementByCss('body').text()).toContain( await check(
'This is a SSR RSC page.' () => browser.eval('document.documentElement.innerHTML'),
/This is a SSR RSC page/
) )
expect(flightRequest).toContain('/node-rsc-ssr?__flight__=1') expect(flightRequest).toContain('/node-rsc-ssr?__flight__=1')
}) })
...@@ -94,8 +95,9 @@ describe('Switchable runtime', () => { ...@@ -94,8 +95,9 @@ describe('Switchable runtime', () => {
.click() .click()
.waitForElementByCss('.node-rsc-ssg') .waitForElementByCss('.node-rsc-ssg')
expect(await browser.elementByCss('body').text()).toContain( await check(
'This is a SSG RSC page.' () => browser.eval('document.documentElement.innerHTML'),
/This is a SSG RSC page/
) )
}) })
...@@ -107,8 +109,9 @@ describe('Switchable runtime', () => { ...@@ -107,8 +109,9 @@ describe('Switchable runtime', () => {
.click() .click()
.waitForElementByCss('.node-rsc') .waitForElementByCss('.node-rsc')
expect(await browser.elementByCss('body').text()).toContain( await check(
'This is a static RSC page.' () => browser.eval('document.documentElement.innerHTML'),
/This is a static RSC page/
) )
}) })
......
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