reafactor: renamed pathname -> page (#55282)

This commit is contained in:
Wyatt Johnson 2023-09-12 10:02:43 -06:00 committed by GitHub
parent 9f852403f0
commit d3656cc42b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 97 additions and 102 deletions

View file

@ -71,7 +71,7 @@ async function runOperation(renderData: RenderData) {
const query = parse(renderData.rawQuery)
const renderOpt: Omit<
RenderOpts,
'App' | 'Document' | 'Component' | 'pathname'
'App' | 'Document' | 'Component' | 'page'
> & {
params: ParsedUrlQuery
} = {

View file

@ -22,7 +22,7 @@ const MIME_TEXT_HTML_UTF8 = 'text/html; charset=utf-8'
async function render(request: NextRequest, event: NextFetchEvent) {
const renderOpt: Omit<
RenderOpts,
'App' | 'Document' | 'Component' | 'pathname'
'App' | 'Document' | 'Component' | 'page'
> & { params: ParsedUrlQuery } = {
// TODO(WEB-1195) params
params: {},

View file

@ -168,7 +168,7 @@ export default function startHandler({
default: comp,
...namespace,
},
pathname: renderData.path,
page: renderData.path,
buildId: 'development',
/* RenderOptsPartial */

View file

@ -1456,7 +1456,7 @@ export async function isPageStatic({
} else {
componentsResult = await loadComponents({
distDir,
pathname: originalAppPath || page,
page: originalAppPath || page,
isAppPath: pageType === 'app',
})
}
@ -1701,7 +1701,7 @@ export async function hasCustomGetInitialProps(
const components = await loadComponents({
distDir,
pathname: page,
page: page,
isAppPath: false,
})
let mod = components.ComponentMod
@ -1725,7 +1725,7 @@ export async function getDefinedNamedExports(
)
const components = await loadComponents({
distDir,
pathname: page,
page: page,
isAppPath: false,
})

View file

@ -77,7 +77,7 @@ export function getRender({
minimalMode: true,
webServerConfig: {
page,
normalizedPage: isAppPath ? normalizeAppPath(page) : page,
pathname: isAppPath ? normalizeAppPath(page) : page,
pagesType,
prerenderManifest,
extendRenderOpts: {
@ -91,8 +91,8 @@ export function getRender({
},
renderToHTML,
incrementalCacheHandler,
loadComponent: async (pathname) => {
if (pathname === page) {
loadComponent: async (inputPage) => {
if (inputPage === page) {
return {
...baseLoadComponentResult,
Component: pageMod.default,
@ -102,13 +102,13 @@ export function getRender({
getStaticPaths: pageMod.getStaticPaths,
ComponentMod: pageMod,
isAppPath: !!pageMod.__next_app__,
pathname,
page: inputPage,
routeModule: pageMod.routeModule,
}
}
// If there is a custom 500 page, we need to handle it separately.
if (pathname === '/500' && error500Mod) {
if (inputPage === '/500' && error500Mod) {
return {
...baseLoadComponentResult,
Component: error500Mod.default,
@ -117,12 +117,12 @@ export function getRender({
getServerSideProps: error500Mod.getServerSideProps,
getStaticPaths: error500Mod.getStaticPaths,
ComponentMod: error500Mod,
pathname,
page: inputPage,
routeModule: error500Mod.routeModule,
}
}
if (pathname === '/_error') {
if (inputPage === '/_error') {
return {
...baseLoadComponentResult,
Component: errorMod.default,
@ -131,7 +131,7 @@ export function getRender({
getServerSideProps: errorMod.getServerSideProps,
getStaticPaths: errorMod.getStaticPaths,
ComponentMod: errorMod,
pathname,
page: inputPage,
routeModule: errorMod.routeModule,
}
}

View file

@ -324,7 +324,7 @@ export default async function exportPage({
if (!isRouteHandler) {
components = await loadComponents({
distDir,
pathname: page,
page: page,
isAppPath: isAppDir,
})
curRenderOpts = {

View file

@ -240,7 +240,7 @@ export async function handleAction({
req,
res,
ComponentMod,
pathname,
page,
serverActionsManifest,
generateFlight,
staticGenerationStore,
@ -250,7 +250,7 @@ export async function handleAction({
req: IncomingMessage
res: ServerResponse
ComponentMod: any
pathname: string
page: string
serverActionsManifest: any
generateFlight: (options: {
actionResult: ActionResult
@ -281,7 +281,7 @@ export async function handleAction({
)
let bound = []
const workerName = 'app' + pathname
const workerName = 'app' + page
const serverModuleMap = new Proxy(
{},
{
@ -406,7 +406,7 @@ export async function handleAction({
// actions.js
// app/page.js
// action woker1
// action worker1
// appRender1
// app/foo/page.js

View file

@ -1345,7 +1345,7 @@ export async function renderToHTMLOrFlight(
const [MetadataTree, MetadataOutlet] = createMetadataComponents({
tree: loaderTreeToRender,
errorType: props.asNotFound ? 'not-found' : undefined,
pathname: pathname,
pathname,
searchParams: providedSearchParams,
getDynamicParamFromSegment: getDynamicParamFromSegment,
appUsingSizeAdjust: appUsingSizeAdjust,
@ -1717,7 +1717,7 @@ export async function renderToHTMLOrFlight(
req,
res,
ComponentMod,
pathname: renderOpts.pathname,
page: renderOpts.page,
serverActionsManifest,
generateFlight,
staticGenerationStore,

View file

@ -304,7 +304,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
protected abstract getBuildId(): string
protected abstract findPageComponents(params: {
pathname: string
page: string
query: NextParsedUrlQuery
params: Params
isAppPath: boolean
@ -1519,21 +1519,20 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}: {
pathname: string
requestHeaders: import('./lib/incremental-cache').IncrementalCache['requestHeaders']
originalAppPath?: string
page: string
isAppPath: boolean
}): Promise<{
staticPaths?: string[]
fallbackMode?: 'static' | 'blocking' | false
}> {
// `staticPaths` is intentionally set to `undefined` as it should've
// been caught when checking disk data.
const staticPaths = undefined
// Read whether or not fallback should exist from the manifest.
const fallbackField =
this.getPrerenderManifest().dynamicRoutes[pathname]?.fallback
return {
staticPaths,
// `staticPaths` is intentionally set to `undefined` as it should've
// been caught when checking disk data.
staticPaths: undefined,
fallbackMode:
typeof fallbackField === 'string'
? 'static'
@ -1592,7 +1591,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
this.stripInternalHeaders(req)
const is500Page = pathname === '/500'
const isAppPath = components.isAppPath
const isAppPath = components.isAppPath === true
const hasServerProps = !!components.getServerSideProps
let hasStaticPaths = !!components.getStaticPaths
@ -1611,12 +1610,13 @@ export default abstract class Server<ServerOptions extends Options = Options> {
let fallbackMode: FallbackMode
let hasFallback = false
const isDynamic = isDynamicRoute(components.pathname)
const isDynamic = isDynamicRoute(components.page)
if (isAppPath && isDynamic) {
const pathsResult = await this.getStaticPaths({
pathname,
originalAppPath: components.pathname,
page: components.page,
isAppPath,
requestHeaders: req.headers,
})
@ -1625,7 +1625,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
hasFallback = typeof fallbackMode !== 'undefined'
if (this.nextConfig.output === 'export') {
const page = components.pathname
const page = components.page
if (fallbackMode !== 'static') {
throw new Error(
@ -2242,7 +2242,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
ssgCacheKey,
async (hasResolved, hadCache): Promise<ResponseCacheEntry | null> => {
const isProduction = !this.renderOpts.dev
const isDynamicPathname = isDynamicRoute(pathname)
const didRespond = hasResolved || res.sent
if (!staticPaths) {
@ -2250,6 +2249,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
? await this.getStaticPaths({
pathname,
requestHeaders: req.headers,
isAppPath,
page: components.page,
})
: { staticPaths: undefined, fallbackMode: false })
}
@ -2315,7 +2316,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
staticPathKey &&
!didRespond &&
!isPreviewMode &&
isDynamicPathname &&
isDynamic &&
(isProduction || !staticPaths || !isPageIncludedInStaticPaths)
) {
if (
@ -2551,7 +2552,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}
const result = await this.findPageComponents({
pathname: page,
page,
query,
params: ctx.renderOpts.params || {},
isAppPath,
@ -2834,7 +2835,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (this.hasAppDir) {
// Use the not-found entry in app directory
result = await this.findPageComponents({
pathname: this.renderOpts.dev ? '/not-found' : '/_not-found',
page: this.renderOpts.dev ? '/not-found' : '/_not-found',
query,
params: {},
isAppPath: true,
@ -2845,7 +2846,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (!result && (await this.hasPage('/404'))) {
result = await this.findPageComponents({
pathname: '/404',
page: '/404',
query,
params: {},
isAppPath: false,
@ -2866,7 +2867,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// dev overlay is used instead
if (statusPage !== '/500' || !this.renderOpts.dev) {
result = await this.findPageComponents({
pathname: statusPage,
page: statusPage,
query,
params: {},
isAppPath: false,
@ -2879,7 +2880,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (!result) {
result = await this.findPageComponents({
pathname: '/_error',
page: '/_error',
query,
params: {},
isAppPath: false,

View file

@ -622,17 +622,18 @@ export default class DevServer extends Server {
protected async getStaticPaths({
pathname,
originalAppPath,
requestHeaders,
page,
isAppPath,
}: {
pathname: string
originalAppPath?: string
requestHeaders: IncrementalCache['requestHeaders']
page: string
isAppPath: boolean
}): Promise<{
staticPaths?: string[]
fallbackMode?: false | 'static' | 'blocking'
}> {
const isAppPath = Boolean(originalAppPath)
// we lazy load the staticPaths to prevent the user
// from waiting on them for the page to load in dev mode
@ -658,7 +659,7 @@ export default class DevServer extends Server {
httpAgentOptions,
locales,
defaultLocale,
originalAppPath,
page,
isAppPath,
requestHeaders,
incrementalCacheHandlerPath:
@ -741,14 +742,14 @@ export default class DevServer extends Server {
}
protected async findPageComponents({
pathname,
page,
query,
params,
isAppPath,
appPaths = null,
shouldEnsure,
}: {
pathname: string
page: string
query: NextParsedUrlQuery
params: Params
isAppPath: boolean
@ -757,7 +758,7 @@ export default class DevServer extends Server {
shouldEnsure: boolean
}): Promise<FindComponentsResult | null> {
await this.devReady
const compilationErr = await this.getCompilationError(pathname)
const compilationErr = await this.getCompilationError(page)
if (compilationErr) {
// Wrap build errors so that they don't get logged again
throw new WrappedBuildError(compilationErr)
@ -765,7 +766,7 @@ export default class DevServer extends Server {
try {
if (shouldEnsure || this.renderOpts.customServer) {
await this.ensurePage({
page: pathname,
page,
appPaths,
clientOnly: false,
})
@ -779,7 +780,7 @@ export default class DevServer extends Server {
this.restorePatchedGlobals()
return await super.findPageComponents({
pathname,
page,
query,
params,
isAppPath,

View file

@ -32,7 +32,7 @@ export async function loadStaticPaths({
locales,
defaultLocale,
isAppPath,
originalAppPath,
page,
isrFlushToDisk,
fetchCacheKeyPrefix,
maxMemoryCacheSize,
@ -45,8 +45,8 @@ export async function loadStaticPaths({
httpAgentOptions: NextConfigComplete['httpAgentOptions']
locales?: string[]
defaultLocale?: string
isAppPath?: boolean
originalAppPath?: string
isAppPath: boolean
page: string
isrFlushToDisk?: boolean
fetchCacheKeyPrefix?: string
maxMemoryCacheSize?: number
@ -65,8 +65,9 @@ export async function loadStaticPaths({
const components = await loadComponents({
distDir,
pathname: originalAppPath || pathname,
isAppPath: !!isAppPath,
// In `pages/`, the page is the same as the pathname.
page: page || pathname,
isAppPath,
})
if (!components.getStaticPaths && !isAppPath) {

View file

@ -57,6 +57,7 @@ export class PagesAPIRouteMatcherProvider extends ManifestRouteMatcherProvider<P
matchers.push(
new PagesAPIRouteMatcher({
kind: RouteKind.PAGES_API,
// In `pages/`, the page is the same as the pathname.
pathname: page,
page,
bundlePath: this.normalizers.bundlePath.normalize(page),

View file

@ -67,6 +67,7 @@ export class PagesRouteMatcherProvider extends ManifestRouteMatcherProvider<Page
matchers.push(
new PagesRouteMatcher({
kind: RouteKind.PAGES,
// In `pages/`, the page is the same as the pathname.
pathname: page,
page,
bundlePath: this.normalizers.bundlePath.normalize(page),

View file

@ -49,7 +49,7 @@ export type LoadComponentsReturnType = {
ComponentMod: any
routeModule?: RouteModule
isAppPath?: boolean
pathname: string
page: string
}
/**
@ -89,11 +89,11 @@ async function loadJSManifest<T>(
async function loadComponentsImpl({
distDir,
pathname,
page,
isAppPath,
}: {
distDir: string
pathname: string
page: string
isAppPath: boolean
}): Promise<LoadComponentsReturnType> {
let DocumentMod = {}
@ -105,15 +105,13 @@ async function loadComponentsImpl({
])
}
const ComponentMod = await Promise.resolve().then(() =>
requirePage(pathname, distDir, isAppPath)
requirePage(page, distDir, isAppPath)
)
// Make sure to avoid loading the manifest for Route Handlers
const hasClientManifest =
isAppPath &&
(pathname.endsWith('/page') ||
pathname === '/not-found' ||
pathname === '/_not-found')
(page.endsWith('/page') || page === '/not-found' || page === '/_not-found')
const [
buildManifest,
@ -131,13 +129,10 @@ async function loadComponentsImpl({
distDir,
'server',
'app',
pathname.replace(/%5F/g, '_') +
'_' +
CLIENT_REFERENCE_MANIFEST +
'.js'
page.replace(/%5F/g, '_') + '_' + CLIENT_REFERENCE_MANIFEST + '.js'
),
'__RSC_MANIFEST',
pathname.replace(/%5F/g, '_')
page.replace(/%5F/g, '_')
)
: undefined,
isAppPath
@ -168,7 +163,7 @@ async function loadComponentsImpl({
clientReferenceManifest,
serverActionsManifest,
isAppPath,
pathname,
page,
routeModule,
}
}

View file

@ -42,7 +42,7 @@ export type LoadComponentsReturnType = {
ComponentMod: any
routeModule?: RouteModule
isAppPath?: boolean
pathname: string
page: string
}
async function loadDefaultErrorComponentsImpl(
@ -68,7 +68,7 @@ async function loadDefaultErrorComponentsImpl(
),
reactLoadableManifest: {},
ComponentMod,
pathname: '/_error',
page: '/_error',
routeModule: ComponentMod.routeModule,
}
}

View file

@ -197,12 +197,12 @@ export default class NextNodeServer extends BaseServer {
// needed for most requests
loadComponents({
distDir: this.distDir,
pathname: '/_document',
page: '/_document',
isAppPath: false,
}).catch(() => {})
loadComponents({
distDir: this.distDir,
pathname: '/_app',
page: '/_app',
isAppPath: false,
}).catch(() => {})
}
@ -600,12 +600,12 @@ export default class NextNodeServer extends BaseServer {
}
protected async findPageComponents({
pathname,
page,
query,
params,
isAppPath,
}: {
pathname: string
page: string
query: NextParsedUrlQuery
params: Params
isAppPath: boolean
@ -620,12 +620,12 @@ export default class NextNodeServer extends BaseServer {
{
spanName: `resolving page into components`,
attributes: {
'next.route': isAppPath ? normalizeAppPath(pathname) : pathname,
'next.route': isAppPath ? normalizeAppPath(page) : page,
},
},
() =>
this.findPageComponentsImpl({
pathname,
page,
query,
params,
isAppPath,
@ -634,38 +634,37 @@ export default class NextNodeServer extends BaseServer {
}
private async findPageComponentsImpl({
pathname,
page,
query,
params,
isAppPath,
}: {
pathname: string
page: string
query: NextParsedUrlQuery
params: Params
isAppPath: boolean
}): Promise<FindComponentsResult | null> {
const paths: string[] = [pathname]
const pagePaths: string[] = [page]
if (query.amp) {
// try serving a static AMP version first
paths.unshift(
(isAppPath ? normalizeAppPath(pathname) : normalizePagePath(pathname)) +
'.amp'
pagePaths.unshift(
(isAppPath ? normalizeAppPath(page) : normalizePagePath(page)) + '.amp'
)
}
if (query.__nextLocale) {
paths.unshift(
...paths.map(
pagePaths.unshift(
...pagePaths.map(
(path) => `/${query.__nextLocale}${path === '/' ? '' : path}`
)
)
}
for (const pagePath of paths) {
for (const pagePath of pagePaths) {
try {
const components = await loadComponents({
distDir: this.distDir,
pathname: pagePath,
page: pagePath,
isAppPath,
})
@ -1532,7 +1531,7 @@ export default class NextNodeServer extends BaseServer {
trailingSlash: this.nextConfig.trailingSlash,
},
url: url,
page: page,
page,
body: getRequestMeta(params.request, '__NEXT_CLONABLE_BODY'),
signal: signalFromNodeResponse(
(params.response as NodeNextResponse).originalResponse

View file

@ -1349,13 +1349,13 @@ export async function renderToHTMLImpl(
}
}
getTracer().getRootSpanAttributes()?.set('next.route', renderOpts.pathname)
getTracer().getRootSpanAttributes()?.set('next.route', renderOpts.page)
const documentResult = await getTracer().trace(
RenderSpan.renderDocument,
{
spanName: `render route (pages) ${renderOpts.pathname}`,
spanName: `render route (pages) ${renderOpts.page}`,
attributes: {
'next.route': renderOpts.pathname,
'next.route': renderOpts.page,
},
},
async () => renderDocument()

View file

@ -1,7 +1,7 @@
import { BLOCKED_PAGES } from '../shared/lib/constants'
export function isBlockedPage(pathname: string): boolean {
return BLOCKED_PAGES.includes(pathname)
export function isBlockedPage(page: string): boolean {
return BLOCKED_PAGES.includes(page)
}
export function cleanAmpPath(pathname: string): string {

View file

@ -28,11 +28,9 @@ import { IncrementalCache } from './lib/incremental-cache'
interface WebServerOptions extends Options {
webServerConfig: {
page: string
normalizedPage: string
pathname: string
pagesType: 'app' | 'pages' | 'root'
loadComponent: (
pathname: string
) => Promise<LoadComponentsReturnType | null>
loadComponent: (page: string) => Promise<LoadComponentsReturnType | null>
extendRenderOpts: Partial<BaseServer['renderOpts']> &
Pick<BaseServer['renderOpts'], 'buildId'>
renderToHTML:
@ -97,7 +95,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
return {
// keep same theme but server path doesn't need to be accurate
[this.serverOptions.webServerConfig
.normalizedPage]: `server${this.serverOptions.webServerConfig.page}.js`,
.pathname]: `server${this.serverOptions.webServerConfig.page}.js`,
}
}
@ -147,7 +145,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
// interpolate query information into page for dynamic route
// so that rewritten paths are handled properly
const normalizedPage = this.serverOptions.webServerConfig.normalizedPage
const normalizedPage = this.serverOptions.webServerConfig.pathname
if (pathname !== normalizedPage) {
pathname = normalizedPage
@ -294,18 +292,16 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
}
protected async findPageComponents({
pathname,
page,
query,
params,
}: {
pathname: string
page: string
query: NextParsedUrlQuery
params: Params | null
isAppPath: boolean
}) {
const result = await this.serverOptions.webServerConfig.loadComponent(
pathname
)
const result = await this.serverOptions.webServerConfig.loadComponent(page)
if (!result) return null
return {

View file

@ -56,7 +56,7 @@ export function getNextPathnameInfo(
): NextPathnameInfo {
const { basePath, i18n, trailingSlash } = options.nextConfig ?? {}
const info: NextPathnameInfo = {
pathname: pathname,
pathname,
trailingSlash: pathname !== '/' ? pathname.endsWith('/') : trailingSlash,
}