Skip to content

Commit

Permalink
share PageRuntime type
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Mar 17, 2022
1 parent d0e771e commit c0a7a2f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
13 changes: 5 additions & 8 deletions packages/next/build/entries.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { PageRuntime, NextConfigComplete } from '../server/config-shared'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import fs from 'fs'
import chalk from 'next/dist/compiled/chalk'
import { posix, join } from 'path'
Expand All @@ -12,11 +14,9 @@ import { MiddlewareLoaderOptions } from './webpack/loaders/next-middleware-loade
import { ClientPagesLoaderOptions } from './webpack/loaders/next-client-pages-loader'
import { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'
import { LoadedEnvFiles } from '@next/env'
import { NextConfigComplete } from '../server/config-shared'
import { parse } from '../build/swc'
import { isCustomErrorPage, isFlightPage, isReservedPage } from './utils'
import { ssrEntries } from './webpack/plugins/middleware-plugin'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import {
MIDDLEWARE_RUNTIME_WEBPACK,
MIDDLEWARE_SSR_RUNTIME_WEBPACK,
Expand Down Expand Up @@ -99,17 +99,14 @@ type Entrypoints = {
edgeServer: webpack5.EntryObject
}

const cachedPageRuntimeConfig = new Map<
string,
[number, 'nodejs' | 'edge' | undefined]
>()
const cachedPageRuntimeConfig = new Map<string, [number, PageRuntime]>()

// @TODO: We should limit the maximum concurrency of this function as there
// could be thousands of pages existing.
export async function getPageRuntime(
pageFilePath: string,
globalRuntimeFallback?: 'nodejs' | 'edge'
): Promise<'nodejs' | 'edge' | undefined> {
): Promise<PageRuntime> {
const cached = cachedPageRuntimeConfig.get(pageFilePath)
if (cached) {
return cached[1]
Expand All @@ -129,7 +126,7 @@ export async function getPageRuntime(
// discussion:
// https://github.com/vercel/next.js/discussions/34179
let isRuntimeRequired: boolean = false
let pageRuntime: 'nodejs' | 'edge' | undefined = undefined
let pageRuntime: PageRuntime = undefined

// Since these configurations should always be static analyzable, we can
// skip these cases that "runtime" and "gSP", "gSSP" are not included in the
Expand Down
5 changes: 0 additions & 5 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,11 +1084,6 @@ export default async function build(
totalSize: allSize,
static: isStatic,
isSsg,
isWebSsr:
hasConcurrentFeatures &&
!isMiddlewareRoute &&
!isReservedPage(page) &&
!isCustomErrorPage(page),
isHybridAmp,
ssgPageRoutes,
initialRevalidateSeconds: false,
Expand Down
6 changes: 3 additions & 3 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { NextConfigComplete, PageRuntime } from '../server/config-shared'

import '../server/node-polyfill-fetch'
import chalk from 'next/dist/compiled/chalk'
import getGzipSize from 'next/dist/compiled/gzip-size'
Expand Down Expand Up @@ -37,7 +39,6 @@ import * as Log from './output/log'
import { loadComponents } from '../server/load-components'
import { trace } from '../trace'
import { setHttpAgentOptions } from '../server/config'
import { NextConfigComplete } from '../server/config-shared'
import isError from '../lib/is-error'
import { recursiveDelete } from '../lib/recursive-delete'
import { Sema } from 'next/dist/compiled/async-sema'
Expand Down Expand Up @@ -77,12 +78,11 @@ export interface PageInfo {
totalSize: number
static: boolean
isSsg: boolean
isWebSsr: boolean
ssgPageRoutes: string[] | null
initialRevalidateSeconds: number | false
pageDuration: number | undefined
ssgPageDurations: number[] | undefined
runtime: 'nodejs' | 'edge' | undefined
runtime: PageRuntime
}

export async function printTreeView(
Expand Down
4 changes: 3 additions & 1 deletion packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
imageConfigDefault,
} from '../shared/lib/image-config'

export type PageRuntime = 'nodejs' | 'edge' | undefined

export type NextConfigComplete = Required<NextConfig> & {
images: Required<ImageConfigComplete>
typescript: Required<TypeScriptConfig>
Expand Down Expand Up @@ -101,7 +103,7 @@ export interface ExperimentalConfig {
craCompat?: boolean
esmExternals?: boolean | 'loose'
isrMemoryCacheSize?: number
runtime?: 'nodejs' | 'edge'
runtime?: Exclude<PageRuntime, undefined>
serverComponents?: boolean
fullySpecified?: boolean
urlImports?: NonNullable<webpack5.Configuration['experiments']>['buildHttp']
Expand Down

0 comments on commit c0a7a2f

Please sign in to comment.