Unverified Commit d95d2d35 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Only resolve page runtime for react 18 and error on read failure (#35705)

* Read the proper page file from either pages directory or from node_modules (inernal pages like _app, _document)
* Only reading page runtime when `reactRoot` is enabled, reduce time for react 17 apps
parent 7eee27f9
import type { PageRuntime, NextConfigComplete } from '../server/config-shared' import type {
PageRuntime,
NextConfigComplete,
NextConfig,
} from '../server/config-shared'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack' import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import fs from 'fs' import fs from 'fs'
import chalk from 'next/dist/compiled/chalk' import chalk from 'next/dist/compiled/chalk'
...@@ -105,8 +109,11 @@ const cachedPageRuntimeConfig = new Map<string, [number, PageRuntime]>() ...@@ -105,8 +109,11 @@ const cachedPageRuntimeConfig = new Map<string, [number, PageRuntime]>()
// could be thousands of pages existing. // could be thousands of pages existing.
export async function getPageRuntime( export async function getPageRuntime(
pageFilePath: string, pageFilePath: string,
globalRuntimeFallback?: 'nodejs' | 'edge' nextConfig: Partial<NextConfig>
): Promise<PageRuntime> { ): Promise<PageRuntime> {
if (!nextConfig.experimental?.reactRoot) return undefined
const globalRuntime = nextConfig.experimental?.runtime
const cached = cachedPageRuntimeConfig.get(pageFilePath) const cached = cachedPageRuntimeConfig.get(pageFilePath)
if (cached) { if (cached) {
return cached[1] return cached[1]
...@@ -118,6 +125,7 @@ export async function getPageRuntime( ...@@ -118,6 +125,7 @@ export async function getPageRuntime(
encoding: 'utf8', encoding: 'utf8',
}) })
} catch (err) { } catch (err) {
if (process.env.NODE_ENV === 'production') throw err
return undefined return undefined
} }
...@@ -183,7 +191,7 @@ export async function getPageRuntime( ...@@ -183,7 +191,7 @@ export async function getPageRuntime(
if (!pageRuntime) { if (!pageRuntime) {
if (isRuntimeRequired) { if (isRuntimeRequired) {
pageRuntime = globalRuntimeFallback pageRuntime = globalRuntime
} }
} }
...@@ -217,6 +225,7 @@ export async function createEntrypoints( ...@@ -217,6 +225,7 @@ export async function createEntrypoints(
const hasRuntimeConfig = const hasRuntimeConfig =
Object.keys(config.publicRuntimeConfig).length > 0 || Object.keys(config.publicRuntimeConfig).length > 0 ||
Object.keys(config.serverRuntimeConfig).length > 0 Object.keys(config.serverRuntimeConfig).length > 0
const hasReactRoot = !!config.experimental.reactRoot
const defaultServerlessOptions = { const defaultServerlessOptions = {
absoluteAppPath: pages['/_app'], absoluteAppPath: pages['/_app'],
...@@ -242,11 +251,9 @@ export async function createEntrypoints( ...@@ -242,11 +251,9 @@ export async function createEntrypoints(
'base64' 'base64'
), ),
i18n: config.i18n ? JSON.stringify(config.i18n) : '', i18n: config.i18n ? JSON.stringify(config.i18n) : '',
reactRoot: config.experimental.reactRoot ? 'true' : '', reactRoot: hasReactRoot ? 'true' : '',
} }
const globalRuntime = config.experimental.runtime
await Promise.all( await Promise.all(
Object.keys(pages).map(async (page) => { Object.keys(pages).map(async (page) => {
const absolutePagePath = pages[page] const absolutePagePath = pages[page]
...@@ -260,11 +267,12 @@ export async function createEntrypoints( ...@@ -260,11 +267,12 @@ export async function createEntrypoints(
const isReserved = isReservedPage(page) const isReserved = isReservedPage(page)
const isCustomError = isCustomErrorPage(page) const isCustomError = isCustomErrorPage(page)
const isFlight = isFlightPage(config, absolutePagePath) const isFlight = isFlightPage(config, absolutePagePath)
const isEdgeRuntime = const isInternalPages = !absolutePagePath.startsWith(PAGES_DIR_ALIAS)
(await getPageRuntime( const pageFilePath = isInternalPages
join(pagesDir, absolutePagePath.slice(PAGES_DIR_ALIAS.length + 1)), ? require.resolve(absolutePagePath)
globalRuntime : join(pagesDir, absolutePagePath.replace(PAGES_DIR_ALIAS, ''))
)) === 'edge' const pageRuntime = await getPageRuntime(pageFilePath, config)
const isEdgeRuntime = pageRuntime === 'edge'
if (page.match(MIDDLEWARE_ROUTE)) { if (page.match(MIDDLEWARE_ROUTE)) {
const loaderOpts: MiddlewareLoaderOptions = { const loaderOpts: MiddlewareLoaderOptions = {
......
...@@ -971,12 +971,8 @@ export default async function build( ...@@ -971,12 +971,8 @@ export default async function build(
p.startsWith(actualPage + '.') || p.startsWith(actualPage + '.') ||
p.startsWith(actualPage + '/index.') p.startsWith(actualPage + '/index.')
) )
const pageRuntime = const pageRuntime = pagePath
hasConcurrentFeatures && pagePath ? await getPageRuntime(join(pagesDir, pagePath), config)
? await getPageRuntime(
join(pagesDir, pagePath),
config.experimental.runtime
)
: undefined : undefined
if ( if (
......
...@@ -509,7 +509,7 @@ export default class HotReloader { ...@@ -509,7 +509,7 @@ export default class HotReloader {
const pageRuntimeConfig = await getPageRuntime( const pageRuntimeConfig = await getPageRuntime(
absolutePagePath, absolutePagePath,
this.runtime this.config
) )
const isEdgeSSRPage = pageRuntimeConfig === 'edge' && !isApiRoute const isEdgeSSRPage = pageRuntimeConfig === 'edge' && !isApiRoute
......
...@@ -285,7 +285,7 @@ export default class DevServer extends Server { ...@@ -285,7 +285,7 @@ export default class DevServer extends Server {
invalidatePageRuntimeCache(fileName, safeTime) invalidatePageRuntimeCache(fileName, safeTime)
const pageRuntimeConfig = await getPageRuntime( const pageRuntimeConfig = await getPageRuntime(
fileName, fileName,
this.nextConfig.experimental.runtime this.nextConfig
) )
const isEdgeRuntime = pageRuntimeConfig === 'edge' const isEdgeRuntime = pageRuntimeConfig === 'edge'
......
...@@ -207,7 +207,7 @@ export default function onDemandEntryHandler( ...@@ -207,7 +207,7 @@ export default function onDemandEntryHandler(
const isApiRoute = normalizedPage.match(API_ROUTE) && !isMiddleware const isApiRoute = normalizedPage.match(API_ROUTE) && !isMiddleware
const pageRuntimeConfig = await getPageRuntime( const pageRuntimeConfig = await getPageRuntime(
absolutePagePath, absolutePagePath,
nextConfig.experimental.runtime nextConfig
) )
const isEdgeServer = pageRuntimeConfig === 'edge' const isEdgeServer = pageRuntimeConfig === 'edge'
......
import { getPageRuntime } from 'next/dist/build/entries' import { getPageRuntime } from 'next/dist/build/entries'
import type { PageRuntime } from 'next/dist/server/config-shared'
import { join } from 'path' import { join } from 'path'
const fixtureDir = join(__dirname, 'fixtures') const fixtureDir = join(__dirname, 'fixtures')
function createNextConfig(runtime?: PageRuntime) {
return {
experimental: { reactRoot: true, runtime },
}
}
describe('parse page runtime config', () => { describe('parse page runtime config', () => {
it('should parse nodejs runtime correctly', async () => { it('should parse nodejs runtime correctly', async () => {
const runtime = await getPageRuntime( const runtime = await getPageRuntime(
join(fixtureDir, 'page-runtime/nodejs.js') join(fixtureDir, 'page-runtime/nodejs.js'),
createNextConfig()
) )
expect(runtime).toBe('nodejs') expect(runtime).toBe('nodejs')
}) })
it('should parse edge runtime correctly', async () => { it('should parse edge runtime correctly', async () => {
const runtime = await getPageRuntime( const runtime = await getPageRuntime(
join(fixtureDir, 'page-runtime/edge.js') join(fixtureDir, 'page-runtime/edge.js'),
createNextConfig()
) )
expect(runtime).toBe('edge') expect(runtime).toBe('edge')
}) })
it('should return undefined if no runtime is specified', async () => { it('should return undefined if no runtime is specified', async () => {
const runtime = await getPageRuntime( const runtime = await getPageRuntime(
join(fixtureDir, 'page-runtime/static.js') join(fixtureDir, 'page-runtime/static.js'),
createNextConfig()
) )
expect(runtime).toBe(undefined) expect(runtime).toBe(undefined)
}) })
...@@ -30,7 +40,7 @@ describe('fallback to the global runtime configuration', () => { ...@@ -30,7 +40,7 @@ describe('fallback to the global runtime configuration', () => {
it('should fallback when gSP is defined and exported', async () => { it('should fallback when gSP is defined and exported', async () => {
const runtime = await getPageRuntime( const runtime = await getPageRuntime(
join(fixtureDir, 'page-runtime/fallback-with-gsp.js'), join(fixtureDir, 'page-runtime/fallback-with-gsp.js'),
'edge' createNextConfig('edge')
) )
expect(runtime).toBe('edge') expect(runtime).toBe('edge')
}) })
...@@ -38,7 +48,7 @@ describe('fallback to the global runtime configuration', () => { ...@@ -38,7 +48,7 @@ describe('fallback to the global runtime configuration', () => {
it('should fallback when gSP is re-exported from other module', async () => { it('should fallback when gSP is re-exported from other module', async () => {
const runtime = await getPageRuntime( const runtime = await getPageRuntime(
join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'), join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'),
'edge' createNextConfig('edge')
) )
expect(runtime).toBe('edge') expect(runtime).toBe('edge')
}) })
......
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