Skip to content

Commit

Permalink
Rename page runtime edge to experimental-edge (#38041)
Browse files Browse the repository at this point in the history
* Rename page runtime edge to experimental-edge

* fix ut

* fix lint

* PageRuntime -> ServerRuntime

* rename constant
  • Loading branch information
huozhi committed Jun 27, 2022
1 parent 252d3b7 commit a5f8382
Show file tree
Hide file tree
Showing 44 changed files with 100 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docs/advanced-features/react-18/server-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
}
```

Using `runtime` also enables [Streaming SSR](/docs/advanced-features/react-18/streaming). When setting `runtime` to `'edge'`, the server will be running entirely in the [Edge Runtime](https://nextjs.org/docs/api-reference/edge-runtime).
Using `runtime` also enables [Streaming SSR](/docs/advanced-features/react-18/streaming). When setting `runtime` to `'experimental-edge'`, the server will be running entirely in the [Edge Runtime](https://nextjs.org/docs/api-reference/edge-runtime).

Now, you can start using React Server Components in Next.js. [See our example](https://github.com/vercel/next-rsc-demo) for more information.

Expand Down
4 changes: 2 additions & 2 deletions docs/advanced-features/react-18/switchable-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ If you have [React 18](/docs/advanced-features/react-18/overview) installed, the

## Global Runtime Option

You can set the experimental option `runtime` to either `'nodejs'` or `'edge'` in your `next.config.js` file:
You can set the experimental option `runtime` to either `'nodejs'` or `'experimental-edge'` in your `next.config.js` file:

```jsx
// next.config.js
Expand All @@ -23,7 +23,7 @@ This option determines which runtime should be used as the default rendering run

## Page Runtime Option

On each page, you can optionally export a `runtime` config set to either `'nodejs'` or `'edge'`:
On each page, you can optionally export a `runtime` config set to either `'nodejs'` or `'experimental-edge'`:

```jsx
export const config = {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/analysis/extract-const-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {

/**
* Extracts the value of an exported const variable named `exportedName`
* (e.g. "export const config = { runtime: 'edge' }") from swc's AST.
* (e.g. "export const config = { runtime: 'experimental-edge' }") from swc's AST.
* The value must be one of (or throws UnsupportedValueError):
* - string
* - boolean
Expand Down
19 changes: 10 additions & 9 deletions packages/next/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { PageRuntime } from '../../server/config-shared'
import type { ServerRuntime } from '../../server/config-shared'
import type { NextConfig } from '../../server/config-shared'
import { tryToExtractExportedConstValue } from './extract-const-value'
import { escapeStringRegexp } from '../../shared/lib/escape-regexp'
import { parseModule } from './parse-module'
import { promises as fs } from 'fs'
import { tryToParsePath } from '../../lib/try-to-parse-path'
import * as Log from '../output/log'
import { SERVER_RUNTIME } from '../../lib/constants'

interface MiddlewareConfig {
pathMatcher: RegExp
}

export interface PageStaticInfo {
runtime?: PageRuntime
runtime?: ServerRuntime
ssg?: boolean
ssr?: boolean
middleware?: Partial<MiddlewareConfig>
Expand All @@ -39,15 +40,15 @@ export async function getPageStaticInfo(params: {
const { ssg, ssr } = checkExports(swcAST)
const config = tryToExtractExportedConstValue(swcAST, 'config') || {}

let runtime = ['experimental-edge', 'edge'].includes(config?.runtime)
? 'edge'
: ssr || ssg
? config?.runtime || nextConfig.experimental?.runtime
: undefined
let runtime =
SERVER_RUNTIME.edge === config?.runtime
? SERVER_RUNTIME.edge
: ssr || ssg
? config?.runtime || nextConfig.experimental?.runtime
: undefined

if (runtime === 'experimental-edge' || runtime === 'edge') {
if (runtime === SERVER_RUNTIME.edge) {
warnAboutExperimentalEdgeApiFunctions()
runtime = 'edge'
}

const middlewareConfig = getMiddlewareConfig(config, nextConfig)
Expand Down
9 changes: 5 additions & 4 deletions packages/next/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ClientPagesLoaderOptions } from './webpack/loaders/next-client-pag
import type { MiddlewareLoaderOptions } from './webpack/loaders/next-middleware-loader'
import type { EdgeSSRLoaderQuery } from './webpack/loaders/next-edge-ssr-loader'
import type { NextConfigComplete } from '../server/config-shared'
import type { PageRuntime } from '../server/config-shared'
import type { ServerRuntime } from '../server/config-shared'
import type { ServerlessLoaderQuery } from './webpack/loaders/next-serverless-loader'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import type { LoadedEnvFiles } from '@next/env'
Expand All @@ -15,6 +15,7 @@ import {
PAGES_DIR_ALIAS,
ROOT_DIR_ALIAS,
APP_DIR_ALIAS,
SERVER_RUNTIME,
} from '../lib/constants'
import {
CLIENT_STATIC_FILES_RUNTIME_AMP,
Expand Down Expand Up @@ -439,12 +440,12 @@ export function runDependingOnPageType<T>(params: {
onEdgeServer: () => T
onServer: () => T
page: string
pageRuntime: PageRuntime
pageRuntime: ServerRuntime
}) {
if (isMiddlewareFile(params.page)) {
return { edgeServer: params.onEdgeServer() }
} else if (params.page.match(API_ROUTE)) {
return params.pageRuntime === 'edge'
return params.pageRuntime === SERVER_RUNTIME.edge
? { edgeServer: params.onEdgeServer() }
: { server: params.onServer() }
} else if (params.page === '/_document') {
Expand All @@ -457,7 +458,7 @@ export function runDependingOnPageType<T>(params: {
) {
return { client: params.onClient(), server: params.onServer() }
} else {
return params.pageRuntime === 'edge'
return params.pageRuntime === SERVER_RUNTIME.edge
? { client: params.onClient(), edgeServer: params.onEdgeServer() }
: { client: params.onClient(), server: params.onServer() }
}
Expand Down
3 changes: 2 additions & 1 deletion packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
PUBLIC_DIR_MIDDLEWARE_CONFLICT,
MIDDLEWARE_FILENAME,
PAGES_DIR_ALIAS,
SERVER_RUNTIME,
} from '../lib/constants'
import { fileExists } from '../lib/file-exists'
import { findPagesDir } from '../lib/find-pages-dir'
Expand Down Expand Up @@ -1117,7 +1118,7 @@ export default async function build(
if (
!isReservedPage(page) &&
// We currently don't support static optimization in the Edge runtime.
pageRuntime !== 'edge'
pageRuntime !== SERVER_RUNTIME.edge
) {
try {
let isPageStaticSpan =
Expand Down
9 changes: 5 additions & 4 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
NextConfig,
NextConfigComplete,
PageRuntime,
ServerRuntime,
} from '../server/config-shared'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'

Expand All @@ -24,6 +24,7 @@ import {
SERVER_PROPS_GET_INIT_PROPS_CONFLICT,
SERVER_PROPS_SSG_CONFLICT,
MIDDLEWARE_FILENAME,
SERVER_RUNTIME,
} from '../lib/constants'
import { EDGE_RUNTIME_WEBPACK } from '../shared/lib/constants'
import prettyBytes from '../lib/pretty-bytes'
Expand Down Expand Up @@ -76,7 +77,7 @@ export interface PageInfo {
initialRevalidateSeconds: number | false
pageDuration: number | undefined
ssgPageDurations: number[] | undefined
runtime: PageRuntime
runtime: ServerRuntime
}

export async function printTreeView(
Expand Down Expand Up @@ -193,7 +194,7 @@ export async function printTreeView(
? '○'
: pageInfo?.isSsg
? '●'
: pageInfo?.runtime === 'edge'
: pageInfo?.runtime === SERVER_RUNTIME.edge
? 'ℇ'
: 'λ'

Expand Down Expand Up @@ -1295,7 +1296,7 @@ export async function isEdgeRuntimeCompiled(

// Check the page runtime as well since we cannot detect the runtime from
// compilation when it's for the client part of edge function
return staticInfo.runtime === 'edge'
return staticInfo.runtime === SERVER_RUNTIME.edge
}

export function getNodeBuiltinModuleNotSupportedInEdgeRuntimeMessage(
Expand Down
3 changes: 2 additions & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
PAGES_DIR_ALIAS,
ROOT_DIR_ALIAS,
APP_DIR_ALIAS,
SERVER_RUNTIME,
} from '../lib/constants'
import { fileExists } from '../lib/file-exists'
import { CustomRoutes } from '../lib/load-custom-routes.js'
Expand Down Expand Up @@ -385,7 +386,7 @@ export default async function getBaseWebpackConfig(
: config.experimental.disableOptimizedLoading

if (isClient) {
if (config.experimental.runtime === 'edge') {
if (config.experimental.runtime === SERVER_RUNTIME.edge) {
Log.warn(
'You are using the experimental Edge Runtime with `experimental.runtime`.'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
WebNextRequest,
WebNextResponse,
} from '../../../../server/base-http/web'
import { SERVER_RUNTIME } from '../../../../lib/constants'

export function getRender({
dev,
Expand Down Expand Up @@ -55,7 +56,7 @@ export function getRender({
page,
extendRenderOpts: {
buildId,
runtime: 'edge',
runtime: SERVER_RUNTIME.edge,
supportsDynamicHTML: true,
disableOptimizedLoading: true,
serverComponentManifest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SERVER_RUNTIME } from '../../../lib/constants'

export default async function transformSource(this: any): Promise<string> {
let { modules, runtime, ssr } = this.getOptions()
if (!Array.isArray(modules)) {
Expand All @@ -18,7 +20,7 @@ export default async function transformSource(this: any): Promise<string> {
export default function RSC() {};
` +
// Currently for the Edge runtime, we treat all RSC pages as SSR pages.
(runtime === 'edge'
(runtime === SERVER_RUNTIME.edge
? 'export const __N_SSP = true;'
: ssr
? `export const __N_SSP = true;`
Expand Down
5 changes: 4 additions & 1 deletion packages/next/build/webpack/plugins/client-entry-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
entries,
} from '../../../server/dev/on-demand-entry-handler'
import { getPageStaticInfo } from '../../analysis/get-page-static-info'
import { SERVER_RUNTIME } from '../../../lib/constants'

type Options = {
dev: boolean
Expand Down Expand Up @@ -111,7 +112,9 @@ export class ClientEntryPlugin {

const clientLoader = `next-flight-client-entry-loader?${stringify({
modules: clientComponentImports,
runtime: this.isEdgeServer ? 'edge' : 'nodejs',
runtime: this.isEdgeServer
? SERVER_RUNTIME.edge
: SERVER_RUNTIME.nodejs,
ssr: pageStaticInfo.ssr,
// Adding name here to make the entry key unique.
name,
Expand Down
6 changes: 6 additions & 0 deletions packages/next/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ServerRuntime } from '../server/config-shared'
import { join } from '../shared/lib/isomorphic/path'

export const NEXT_PROJECT_ROOT = join(__dirname, '..', '..')
Expand Down Expand Up @@ -84,3 +85,8 @@ export const ESLINT_PROMPT_VALUES = [
config: null,
},
]

export const SERVER_RUNTIME: Record<string, ServerRuntime> = {
edge: 'experimental-edge',
nodejs: 'nodejs',
}
3 changes: 2 additions & 1 deletion packages/next/server/app-render.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { LoadComponentsReturnType } from './load-components'
import type { ServerRuntime } from './config-shared'

import React from 'react'
import { ParsedUrlQuery, stringify as stringifyQuery } from 'querystring'
Expand Down Expand Up @@ -30,7 +31,7 @@ export type RenderOptsPartial = {
dev?: boolean
serverComponentManifest?: any
supportsDynamicHTML?: boolean
runtime?: 'nodejs' | 'edge'
runtime?: ServerRuntime
serverComponents?: boolean
}

Expand Down
8 changes: 6 additions & 2 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import type { FontManifest } from './font-utils'
import type { LoadComponentsReturnType } from './load-components'
import type { RouteMatch } from '../shared/lib/router/utils/route-matcher'
import type { Params } from '../shared/lib/router/utils/route-matcher'
import type { NextConfig, NextConfigComplete } from './config-shared'
import type {
NextConfig,
NextConfigComplete,
ServerRuntime,
} from './config-shared'
import type { NextParsedUrlQuery, NextUrlWithParsedQuery } from './request-meta'
import type { ParsedUrlQuery } from 'querystring'
import type { RenderOpts, RenderOptsPartial } from './render'
Expand Down Expand Up @@ -172,7 +176,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
defaultLocale?: string
domainLocales?: DomainLocale[]
distDir: string
runtime?: 'nodejs' | 'edge'
runtime?: ServerRuntime
serverComponents?: boolean
crossOrigin?: string
supportsDynamicHTML?: boolean
Expand Down
4 changes: 2 additions & 2 deletions packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
RemotePattern,
} from '../shared/lib/image-config'

export type PageRuntime = 'nodejs' | 'edge' | undefined
export type ServerRuntime = 'nodejs' | 'experimental-edge' | undefined

export type NextConfigComplete = Required<NextConfig> & {
images: Required<ImageConfigComplete>
Expand Down Expand Up @@ -112,7 +112,7 @@ export interface ExperimentalConfig {
craCompat?: boolean
esmExternals?: boolean | 'loose'
isrMemoryCacheSize?: number
runtime?: Exclude<PageRuntime, undefined>
runtime?: Exclude<ServerRuntime, undefined>
serverComponents?: boolean
fullySpecified?: boolean
urlImports?: NonNullable<webpack5.Configuration['experiments']>['buildHttp']
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { LoadComponentsReturnType, ManifestItem } from './load-components'
import type { GetServerSideProps, GetStaticProps, PreviewData } from '../types'
import type { UnwrapPromise } from '../lib/coalesced-function'
import type { ReactReadableStream } from './node-web-streams-helper'
import type { ServerRuntime } from './config-shared'

import React from 'react'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
Expand Down Expand Up @@ -238,7 +239,7 @@ export type RenderOptsPartial = {
domainLocales?: DomainLocale[]
disableOptimizedLoading?: boolean
supportsDynamicHTML?: boolean
runtime?: 'nodejs' | 'edge'
runtime?: ServerRuntime
serverComponents?: boolean
customServer?: boolean
crossOrigin?: string
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
{
...renderOpts,
disableOptimizedLoading: true,
runtime: 'edge',
runtime: 'experimental-edge',
}
)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/next/shared/lib/html-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BuildManifest } from '../../server/get-page-files'
import type { ServerRuntime } from '../../server/config-shared'
import type { NEXT_DATA } from './utils'

import { createContext } from 'react'
Expand Down Expand Up @@ -37,7 +38,7 @@ export type HtmlProps = {
optimizeCss?: boolean
optimizeFonts?: boolean
nextScriptWorkers?: boolean
runtime?: 'edge' | 'nodejs'
runtime?: ServerRuntime
hasConcurrentFeatures?: boolean
largePageDataBytes?: number
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/react-18-invalid-config/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function writeNextConfig(config, reactVersion = 17) {
describe('Invalid react 18 webpack config', () => {
it('should install react 18 when `experimental.runtime` is enabled', async () => {
writeNextConfig({
runtime: 'edge',
runtime: 'experimental-edge',
})
const { stderr } = await nextBuild(appDir, [], { stderr: true, nodeArgs })
nextConfig.restore()
Expand Down
2 changes: 1 addition & 1 deletion test/integration/react-18/app/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
// reactStrictMode: true,
experimental: {
// runtime: 'edge',
// runtime: 'experimental-edge',
},
images: {
deviceSizes: [480, 1024, 1600, 2000],
Expand Down
2 changes: 1 addition & 1 deletion test/integration/react-18/app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export default function Index() {
}

export const config = {
// runtime: 'edge'
// runtime: 'experimental-edge'
}
Loading

0 comments on commit a5f8382

Please sign in to comment.