Unverified Commit a56066c5 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Pages Route Module (#50070)

This exports the named exports from the user's page route file and
exports a new `routeModule` stub. This stub will be the new rendering
mechanism used for pages within `pages/` and will serve to allow us to
deprecate large chunks of the server code to instead live within the
pages entrypoints.

TLDR;

- Route Modules now accept a `definition` so that they have their full
route definition
- Exports are now hoisted from the userland module by name to prevent
name collisions
- A new `PagesRouteModule` stub was added that will in the future
contain all the pages rendering logic

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 2aa68ae2
......@@ -2,12 +2,19 @@ import { EdgeRouteModuleWrapper } from 'next/dist/server/web/edge-route-module-w
import RouteModule from 'ROUTE_MODULE'
import * as userland from 'ENTRY'
import { PAGE, PATHNAME } from 'BOOTSTRAP_CONFIG'
import { PAGE, PATHNAME, KIND } from 'BOOTSTRAP_CONFIG'
// TODO: (wyattjoh) - perform the option construction in Rust to allow other modules to accept different options
const routeModule = new RouteModule({
userland,
pathname: PATHNAME,
definition: {
page: PAGE,
kind: KIND,
pathname: PATHNAME,
// The following aren't used in production.
filename: '',
bundlePath: '',
},
resolvedPagePath: `app/${PAGE}`,
nextConfigOutput: undefined,
})
......
......@@ -18,11 +18,18 @@ import { attachRequestMeta } from '../../internal/next-request-helpers'
import RouteModule from 'ROUTE_MODULE'
import * as userland from 'ENTRY'
import { PAGE, PATHNAME } from 'BOOTSTRAP_CONFIG'
import { PAGE, PATHNAME, KIND } from 'BOOTSTRAP_CONFIG'
const routeModule = new RouteModule({
userland,
pathname: PATHNAME,
definition: {
page: PAGE,
kind: KIND,
pathname: PATHNAME,
// The following aren't used in production.
filename: '',
bundlePath: '',
},
resolvedPagePath: `app/${PAGE}`,
nextConfigOutput: undefined,
})
......
......@@ -72,9 +72,12 @@ declare module 'ROUTE_MODULE' {
}
declare module 'BOOTSTRAP_CONFIG' {
import type { RouteKind } from 'next/dist/server/future/route-kind'
export const NAME: string
export const PAGE: string
export const PATHNAME: string
export const KIND: RouteKind
}
declare module 'CLIENT_MODULE' {
......
......@@ -104,6 +104,7 @@ pub async fn bootstrap(
let mut config = config.await?.clone_value();
config.insert("PAGE".to_string(), path.to_string());
config.insert("PATHNAME".to_string(), pathname);
config.insert("KIND".to_string(), "APP_ROUTE".to_string());
let config_asset = as_es_module_asset(
VirtualAssetVc::new(
......
......@@ -1193,16 +1193,20 @@ export default async function build(
? [
'hasCustomGetInitialProps',
'isPageStatic',
'getNamedExports',
'getDefinedNamedExports',
'exportPage',
]
: ['hasCustomGetInitialProps', 'isPageStatic', 'getNamedExports'],
: [
'hasCustomGetInitialProps',
'isPageStatic',
'getDefinedNamedExports',
],
}) as Worker &
Pick<
typeof import('./worker'),
| 'hasCustomGetInitialProps'
| 'isPageStatic'
| 'getNamedExports'
| 'getDefinedNamedExports'
| 'exportPage'
>
}
......@@ -1275,7 +1279,7 @@ export default async function build(
true
)
const namedExportsPromise = pagesStaticWorkers.getNamedExports(
const namedExportsPromise = pagesStaticWorkers.getDefinedNamedExports(
appPageToCheck,
distDir,
runtimeEnvConfig
......
......@@ -1677,11 +1677,11 @@ export async function hasCustomGetInitialProps(
return mod.getInitialProps !== mod.origGetInitialProps
}
export async function getNamedExports(
export async function getDefinedNamedExports(
page: string,
distDir: string,
runtimeEnvConfig: any
): Promise<Array<string>> {
): Promise<ReadonlyArray<string>> {
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
const components = await loadComponents({
distDir,
......@@ -1689,9 +1689,10 @@ export async function getNamedExports(
hasServerComponents: false,
isAppPath: false,
})
let mod = components.ComponentMod
return Object.keys(mod)
return Object.keys(components.ComponentMod).filter((key) => {
return typeof components.ComponentMod[key] !== 'undefined'
})
}
export function detectConflictingPaths(
......
......@@ -19,6 +19,9 @@ import { isAppRouteRoute } from '../../../lib/is-app-route-route'
import { isMetadataRoute } from '../../../lib/metadata/is-metadata-route'
import { NextConfig } from '../../../server/config-shared'
import { AppPathnameNormalizer } from '../../../server/future/normalizers/built/app/app-pathname-normalizer'
import { RouteKind } from '../../../server/future/route-kind'
import { AppRouteRouteModuleOptions } from '../../../server/future/route-modules/app-route/module'
import { AppBundlePathNormalizer } from '../../../server/future/normalizers/built/app/app-bundle-path-normalizer'
export type AppLoaderOptions = {
name: string
......@@ -107,21 +110,24 @@ async function createAppRouteCode({
// References the route handler file to load found in `./routes/${kind}.ts`.
// TODO: allow switching to the different kinds of routes
const kind = 'app-route'
const normalizer = new AppPathnameNormalizer()
const pathname = normalizer.normalize(page)
const pathname = new AppPathnameNormalizer().normalize(page)
const bundlePath = new AppBundlePathNormalizer().normalize(page)
// This is providing the options defined by the route options type found at
// ./routes/${kind}.ts. This is stringified here so that the literal for
// `userland` can reference the variable for `userland` that's in scope for
// the loader code.
const options = `{
userland,
pathname: ${JSON.stringify(pathname)},
resolvedPagePath: ${JSON.stringify(resolvedPagePath)},
nextConfigOutput: ${
nextConfigOutput ? JSON.stringify(nextConfigOutput) : 'undefined'
const options: Omit<AppRouteRouteModuleOptions, 'userland'> = {
definition: {
kind: RouteKind.APP_ROUTE,
page,
pathname,
filename,
bundlePath,
},
}`
resolvedPagePath,
nextConfigOutput,
}
return `
import 'next/dist/server/node-polyfill-headers'
......@@ -130,7 +136,11 @@ async function createAppRouteCode({
import * as userland from ${JSON.stringify(resolvedPagePath)}
const routeModule = new RouteModule(${options})
const options = ${JSON.stringify(options)}
const routeModule = new RouteModule({
...options,
userland,
})
// Pull out the exports that we need to expose from the module. This should
// be eliminated when we've moved the other routes to the new format. These
......
/**
* Hoists a name from a module or promised module.
*
* @param module the module to hoist the name from
* @param name the name to hoist
* @returns the value on the module (or promised module)
*/
export function hoist(module: any, name: string) {
// If the name is available in the module, return it.
if (name in module) {
return module[name]
}
// If a property called `then` exists, assume it's a promise and
// return a promise that resolves to the name.
if ('then' in module && typeof module.then === 'function') {
return module.then((mod: any) => hoist(mod, name))
}
// If we're trying to hoise the default export, and the module is a function,
// return the module itself.
if (typeof module === 'function' && name === 'default') {
return module
}
// Otherwise, return undefined.
return undefined
}
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { stringify } from 'querystring'
import { getModuleBuildInfo } from './get-module-build-info'
import { getModuleBuildInfo } from '../get-module-build-info'
import { PagesRouteModuleOptions } from '../../../../server/future/route-modules/pages/module'
import { RouteKind } from '../../../../server/future/route-kind'
import { normalizePagePath } from '../../../../shared/lib/page-path/normalize-page-path'
/**
* The options for the route loader.
......@@ -54,10 +57,47 @@ const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
preferredRegion,
}
const options: Omit<PagesRouteModuleOptions, 'userland'> = {
definition: {
kind: RouteKind.PAGES,
page: normalizePagePath(page),
pathname: page,
// The following aren't used in production.
bundlePath: '',
filename: '',
},
}
return `
// Next.js Route Loader
export * from ${JSON.stringify(absolutePagePath)}
export { default } from ${JSON.stringify(absolutePagePath)}
import RouteModule from "next/dist/server/future/route-modules/pages/module"
import { hoist } from "next/dist/build/webpack/loaders/next-route-loader/helpers"
// Import the userland code.
import * as userland from ${JSON.stringify(absolutePagePath)}
// Re-export the component (should be the default export).
export default hoist(userland, "default")
// Re-export methods.
export const getStaticProps = hoist(userland, "getStaticProps")
export const getStaticPaths = hoist(userland, "getStaticPaths")
export const getServerSideProps = hoist(userland, "getServerSideProps")
export const config = hoist(userland, "config")
export const reportWebVitals = hoist(userland, "reportWebVitals")
// Re-export legacy methods.
export const unstable_getStaticProps = hoist(userland, "unstable_getStaticProps")
export const unstable_getStaticPaths = hoist(userland, "unstable_getStaticPaths")
export const unstable_getStaticParams = hoist(userland, "unstable_getStaticParams")
export const unstable_getServerProps = hoist(userland, "unstable_getServerProps")
export const unstable_getServerSideProps = hoist(userland, "unstable_getServerSideProps")
// Create and export the route module that will be consumed.
const options = ${JSON.stringify(options)}
const routeModule = new RouteModule({ ...options, userland })
export { routeModule }
`
}
......
......@@ -139,6 +139,7 @@ export async function getNotFoundError(
!/next-(app|middleware|client-pages|route|flight-(client|server|client-entry))-loader\.js/.test(
name
) &&
!/next-route-loader\/index\.js/.test(name) &&
!/css-loader.+\.js/.test(name)
)
if (moduleTrace.length === 0) return ''
......
......@@ -44,6 +44,7 @@ import getRouteFromEntrypoint from '../get-route-from-entrypoint'
import { fileExists } from '../../lib/file-exists'
import {
difference,
isInstrumentationHookFile,
isMiddlewareFile,
isMiddlewareFilename,
} from '../../build/utils'
......@@ -855,7 +856,8 @@ export default class HotReloader {
} else if (
!isAPIRoute(page) &&
!isMiddlewareFile(page) &&
!isInternalPathname(relativeRequest)
!isInternalPathname(relativeRequest) &&
!isInstrumentationHookFile(page)
) {
value = getRouteLoaderEntry({
page,
......
......@@ -2,6 +2,7 @@ import type { NextConfig } from '../../../config-shared'
import type { AppRouteRouteDefinition } from '../../route-definitions/app-route-route-definition'
import type { AppConfig } from '../../../../build/utils'
import type { NextRequest } from '../../../web/spec-extension/request'
import type { PrerenderManifest } from '../../../../build'
import {
RouteModule,
......@@ -27,13 +28,28 @@ import { AppRouteRouteHandlersSpan } from '../../../lib/trace/constants'
import { getPathnameFromAbsolutePath } from './helpers/get-pathname-from-absolute-path'
import { proxyRequest } from './helpers/proxy-request'
import { resolveHandlerError } from './helpers/resolve-handler-error'
import { RouteKind } from '../../route-kind'
import * as Log from '../../../../build/output/log'
import { autoImplementMethods } from './helpers/auto-implement-methods'
import { getNonStaticMethods } from './helpers/get-non-static-methods'
import { PrerenderManifest } from '../../../../build'
import { appendMutableCookies } from '../../../web/spec-extension/adapters/request-cookies'
// These are imported weirdly like this because of the way that the bundling
// works. We need to import the built files from the dist directory, but we
// can't do that directly because we need types from the source files. So we
// import the types from the source files and then import the built files.
const { requestAsyncStorage } =
require('next/dist/client/components/request-async-storage') as typeof import('../../../../client/components/request-async-storage')
const { staticGenerationAsyncStorage } =
require('next/dist/client/components/static-generation-async-storage') as typeof import('../../../../client/components/static-generation-async-storage')
const serverHooks =
require('next/dist/client/components/hooks-server-context') as typeof import('../../../../client/components/hooks-server-context')
const headerHooks =
require('next/dist/client/components/headers') as typeof import('../../../../client/components/headers')
const { staticGenerationBailout } =
require('next/dist/client/components/static-generation-bailout') as typeof import('../../../../client/components/static-generation-bailout')
const { actionAsyncStorage } =
require('next/dist/client/components/action-async-storage') as typeof import('../../../../client/components/action-async-storage')
/**
* AppRouteRouteHandlerContext is the context that is passed to the route
* handler for app routes.
......@@ -89,8 +105,7 @@ export type AppRouteUserlandModule = AppRouteHandlers &
* module from the bundled code.
*/
export interface AppRouteRouteModuleOptions
extends RouteModuleOptions<AppRouteUserlandModule> {
readonly pathname: string
extends RouteModuleOptions<AppRouteRouteDefinition, AppRouteUserlandModule> {
readonly resolvedPagePath: string
readonly nextConfigOutput: NextConfig['output']
}
......@@ -102,8 +117,40 @@ export class AppRouteRouteModule extends RouteModule<
AppRouteRouteDefinition,
AppRouteUserlandModule
> {
public readonly definition: AppRouteRouteDefinition
public readonly pathname: string
/**
* A reference to the request async storage.
*/
public readonly requestAsyncStorage = requestAsyncStorage
/**
* A reference to the static generation async storage.
*/
public readonly staticGenerationAsyncStorage = staticGenerationAsyncStorage
/**
* An interface to call server hooks which interact with the underlying
* storage.
*/
public readonly serverHooks = serverHooks
/**
* An interface to call header hooks which interact with the underlying
* request storage.
*/
public readonly headerHooks = headerHooks
/**
* An interface to call static generation bailout hooks which interact with
* the underlying static generation storage.
*/
public readonly staticGenerationBailout = staticGenerationBailout
/**
* A reference to the mutation related async storage, such as mutations of
* cookies.
*/
public readonly actionAsyncStorage = actionAsyncStorage
public readonly resolvedPagePath: string
public readonly nextConfigOutput: NextConfig['output'] | undefined
......@@ -113,22 +160,12 @@ export class AppRouteRouteModule extends RouteModule<
constructor({
userland,
pathname,
definition,
resolvedPagePath,
nextConfigOutput,
}: AppRouteRouteModuleOptions) {
super({ userland })
this.definition = {
kind: RouteKind.APP_ROUTE,
pathname,
// The following aren't needed for the route handler.
page: '',
bundlePath: '',
filename: '',
}
super({ userland, definition })
this.pathname = pathname
this.resolvedPagePath = resolvedPagePath
this.nextConfigOutput = nextConfigOutput
......@@ -146,7 +183,7 @@ export class AppRouteRouteModule extends RouteModule<
this.dynamic = 'error'
} else if (this.dynamic === 'force-dynamic') {
throw new Error(
`export const dynamic = "force-dynamic" on page "${pathname}" cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export`
`export const dynamic = "force-dynamic" on page "${definition.pathname}" cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export`
)
}
}
......
import type {
GetServerSideProps,
GetStaticPaths,
GetStaticProps,
NextComponentType,
PageConfig,
} from '../../../../../types'
import type { PagesRouteDefinition } from '../../route-definitions/pages-route-definition'
import { RouteModule, type RouteModuleOptions } from '../route-module'
/**
* The userland module for a page. This is the module that is exported from the
* page file that contains the page component, page config, and any page data
* fetching functions.
*/
export type PagesUserlandModule = {
/**
* The exported page component.
*/
readonly default: NextComponentType
/**
* The exported page config.
*/
readonly config?: PageConfig
/**
* The exported `getStaticProps` function.
*/
readonly getStaticProps?: GetStaticProps
/**
* The exported `getStaticPaths` function.
*/
readonly getStaticPaths?: GetStaticPaths
/**
* The exported `getServerSideProps` function.
*/
readonly getServerSideProps?: GetServerSideProps
}
export type PagesRouteModuleOptions = RouteModuleOptions<
PagesRouteDefinition,
PagesUserlandModule
>
class PagesRouteModule extends RouteModule<
PagesRouteDefinition,
PagesUserlandModule
> {
public setup(): Promise<void> {
throw new Error('Method not implemented.')
}
public handle(): Promise<Response> {
throw new Error('Method not implemented.')
}
}
export default PagesRouteModule
import type { RouteDefinition } from '../route-definitions/route-definition'
import type { NextRequest } from '../../web/spec-extension/request'
// These are imported weirdly like this because of the way that the bundling
// works. We need to import the built files from the dist directory, but we
// can't do that directly because we need types from the source files. So we
// import the types from the source files and then import the built files.
const { requestAsyncStorage } =
require('next/dist/client/components/request-async-storage') as typeof import('../../../client/components/request-async-storage')
const { staticGenerationAsyncStorage } =
require('next/dist/client/components/static-generation-async-storage') as typeof import('../../../client/components/static-generation-async-storage')
const serverHooks =
require('next/dist/client/components/hooks-server-context') as typeof import('../../../client/components/hooks-server-context')
const headerHooks =
require('next/dist/client/components/headers') as typeof import('../../../client/components/headers')
const { staticGenerationBailout } =
require('next/dist/client/components/static-generation-bailout') as typeof import('../../../client/components/static-generation-bailout')
const { actionAsyncStorage } =
require('next/dist/client/components/action-async-storage') as typeof import('../../../client/components/action-async-storage')
/**
* RouteModuleOptions is the options that are passed to the route module, other
* route modules should extend this class to add specific options for their
* route.
*/
export interface RouteModuleOptions<U = unknown> {
export interface RouteModuleOptions<
D extends RouteDefinition = RouteDefinition,
U = unknown
> {
readonly definition: Readonly<D>
readonly userland: Readonly<U>
}
......@@ -46,40 +33,6 @@ export abstract class RouteModule<
D extends RouteDefinition = RouteDefinition,
U = unknown
> {
/**
* A reference to the request async storage.
*/
public readonly requestAsyncStorage = requestAsyncStorage
/**
* A reference to the static generation async storage.
*/
public readonly staticGenerationAsyncStorage = staticGenerationAsyncStorage
/**
* An interface to call server hooks which interact with the underlying
* storage.
*/
public readonly serverHooks = serverHooks
/**
* An interface to call header hooks which interact with the underlying
* request storage.
*/
public readonly headerHooks = headerHooks
/**
* An interface to call static generation bailout hooks which interact with
* the underlying static generation storage.
*/
public readonly staticGenerationBailout = staticGenerationBailout
/**
* A reference to the mutation related async storage, such as mutations of
* cookies.
*/
public readonly actionAsyncStorage = actionAsyncStorage
/**
* The userland module. This is the module that is exported from the user's
* code. This is marked as readonly to ensure that the module is not mutated
......@@ -90,7 +43,7 @@ export abstract class RouteModule<
/**
* The definition of the route.
*/
public abstract readonly definition: D
public readonly definition: Readonly<D>
/**
* Setup will setup the route handler. This could patch any globals or perform
......@@ -107,7 +60,8 @@ export abstract class RouteModule<
context: RouteModuleHandleContext
): Promise<Response>
constructor({ userland }: RouteModuleOptions<U>) {
constructor({ userland, definition }: RouteModuleOptions<D, U>) {
this.userland = userland
this.definition = definition
}
}
......@@ -96,5 +96,6 @@ Caused by:
Syntax Error
Import trace for requested module:
./index.js"
./index.js
./pages/index.js"
`;
......@@ -28,7 +28,8 @@ Caused by:
Syntax Error
Import trace for requested module:
./index.js"
./index.js
./pages/index.js"
`;
exports[`ReactRefreshLogBox default syntax > runtime error 3`] = `
......@@ -47,5 +48,6 @@ Caused by:
Syntax Error
Import trace for requested module:
./index.js"
./index.js
./pages/index.js"
`;
......@@ -595,13 +595,13 @@ describe.each([[''], ['/docs']])(
expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxHeader(browser)).toMatchInlineSnapshot(`
" 1 of 1 unhandled error
Server Error
" 1 of 1 unhandled error
Server Error
Error: The default export is not a React Component in page: \\"/hmr/about5\\"
Error: The default export is not a React Component in page: \\"/hmr/about5\\"
This error happened while generating the page. Any console logs will be displayed in the terminal window."
`)
This error happened while generating the page. Any console logs will be displayed in the terminal window."
`)
await next.patchFile(aboutPage, aboutContent)
......@@ -698,13 +698,13 @@ describe.each([[''], ['/docs']])(
expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxHeader(browser)).toMatchInlineSnapshot(`
" 1 of 1 unhandled error
Server Error
" 1 of 1 unhandled error
Server Error
Error: The default export is not a React Component in page: \\"/hmr/about7\\"
Error: The default export is not a React Component in page: \\"/hmr/about7\\"
This error happened while generating the page. Any console logs will be displayed in the terminal window."
`)
This error happened while generating the page. Any console logs will be displayed in the terminal window."
`)
await next.patchFile(aboutPage, aboutContent)
......@@ -756,18 +756,19 @@ describe.each([[''], ['/docs']])(
`"Failed to compile"`
)
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"./components/parse-error.xyz
Module parse failed: Unexpected token (3:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| This
| is
> }}}
| invalid
| js
Import trace for requested module:
./components/parse-error.xyz"
`)
"./components/parse-error.xyz
Module parse failed: Unexpected token (3:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| This
| is
> }}}
| invalid
| js
Import trace for requested module:
./components/parse-error.xyz
./pages/hmr/about8.js"
`)
await next.patchFile(aboutPage, aboutContent)
......@@ -863,11 +864,11 @@ describe.each([[''], ['/docs']])(
expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxHeader(browser)).toMatchInlineSnapshot(`
" 1 of 1 unhandled error
Unhandled Runtime Error
" 1 of 1 unhandled error
Unhandled Runtime Error
Error: an-expected-error-in-gip"
`)
Error: an-expected-error-in-gip"
`)
await next.patchFile(
erroredPage,
......@@ -907,13 +908,13 @@ describe.each([[''], ['/docs']])(
expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxHeader(browser)).toMatchInlineSnapshot(`
" 1 of 1 unhandled error
Server Error
" 1 of 1 unhandled error
Server Error
Error: an-expected-error-in-gip
Error: an-expected-error-in-gip
This error happened while generating the page. Any console logs will be displayed in the terminal window."
`)
This error happened while generating the page. Any console logs will be displayed in the terminal window."
`)
const erroredPage = join('pages', 'hmr', 'error-in-gip.js')
......
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