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

Rename page runtime edge to experimental-edge (#38041)

* Rename page runtime edge to experimental-edge

* fix ut

* fix lint

* PageRuntime -> ServerRuntime

* rename constant
parent 252d3b72
......@@ -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.
......
......@@ -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
......@@ -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 = {
......
......@@ -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
......
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'
......@@ -6,13 +6,14 @@ 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>
......@@ -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)
......
......@@ -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'
......@@ -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,
......@@ -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') {
......@@ -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() }
}
......
......@@ -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'
......@@ -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 =
......
import type {
NextConfig,
NextConfigComplete,
PageRuntime,
ServerRuntime,
} from '../server/config-shared'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
......@@ -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'
......@@ -76,7 +77,7 @@ export interface PageInfo {
initialRevalidateSeconds: number | false
pageDuration: number | undefined
ssgPageDurations: number[] | undefined
runtime: PageRuntime
runtime: ServerRuntime
}
export async function printTreeView(
......@@ -193,7 +194,7 @@ export async function printTreeView(
? '○'
: pageInfo?.isSsg
? '●'
: pageInfo?.runtime === 'edge'
: pageInfo?.runtime === SERVER_RUNTIME.edge
? 'ℇ'
: 'λ'
......@@ -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(
......
......@@ -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'
......@@ -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`.'
)
......
......@@ -10,6 +10,7 @@ import {
WebNextRequest,
WebNextResponse,
} from '../../../../server/base-http/web'
import { SERVER_RUNTIME } from '../../../../lib/constants'
export function getRender({
dev,
......@@ -55,7 +56,7 @@ export function getRender({
page,
extendRenderOpts: {
buildId,
runtime: 'edge',
runtime: SERVER_RUNTIME.edge,
supportsDynamicHTML: true,
disableOptimizedLoading: true,
serverComponentManifest,
......
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)) {
......@@ -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;`
......
......@@ -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
......@@ -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,
......
import type { ServerRuntime } from '../server/config-shared'
import { join } from '../shared/lib/isomorphic/path'
export const NEXT_PROJECT_ROOT = join(__dirname, '..', '..')
......@@ -84,3 +85,8 @@ export const ESLINT_PROMPT_VALUES = [
config: null,
},
]
export const SERVER_RUNTIME: Record<string, ServerRuntime> = {
edge: 'experimental-edge',
nodejs: 'nodejs',
}
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'
......@@ -30,7 +31,7 @@ export type RenderOptsPartial = {
dev?: boolean
serverComponentManifest?: any
supportsDynamicHTML?: boolean
runtime?: 'nodejs' | 'edge'
runtime?: ServerRuntime
serverComponents?: boolean
}
......
......@@ -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'
......@@ -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
......
......@@ -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>
......@@ -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']
......
......@@ -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'
......@@ -238,7 +239,7 @@ export type RenderOptsPartial = {
domainLocales?: DomainLocale[]
disableOptimizedLoading?: boolean
supportsDynamicHTML?: boolean
runtime?: 'nodejs' | 'edge'
runtime?: ServerRuntime
serverComponents?: boolean
customServer?: boolean
crossOrigin?: string
......
......@@ -138,7 +138,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
{
...renderOpts,
disableOptimizedLoading: true,
runtime: 'edge',
runtime: 'experimental-edge',
}
)
}
......
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'
......@@ -37,7 +38,7 @@ export type HtmlProps = {
optimizeCss?: boolean
optimizeFonts?: boolean
nextScriptWorkers?: boolean
runtime?: 'edge' | 'nodejs'
runtime?: ServerRuntime
hasConcurrentFeatures?: boolean
largePageDataBytes?: number
}
......
......@@ -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()
......
module.exports = {
// reactStrictMode: true,
experimental: {
// runtime: 'edge',
// runtime: 'experimental-edge',
},
images: {
deviceSizes: [480, 1024, 1600, 2000],
......
......@@ -22,5 +22,5 @@ export default function Index() {
}
export const config = {
// runtime: 'edge'
// runtime: 'experimental-edge'
}
......@@ -67,8 +67,14 @@ function runTestsAgainstRuntime(runtime) {
if (env === 'dev') {
invalidPage.write(`export const value = 1`)
}
nextConfig.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
indexPage.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
nextConfig.replace(
"// runtime: 'experimental-edge'",
`runtime: '${runtime}'`
)
indexPage.replace(
"// runtime: 'experimental-edge'",
`runtime: '${runtime}'`
)
},
afterAll: (env) => {
if (env === 'dev') {
......@@ -81,7 +87,7 @@ function runTestsAgainstRuntime(runtime) {
)
}
runTestsAgainstRuntime('edge')
runTestsAgainstRuntime('experimental-edge')
runTestsAgainstRuntime('nodejs')
function runTests(name, fn, opts) {
......
......@@ -6,6 +6,6 @@ module.exports = {
pageExtensions: ['js', 'ts', 'jsx'], // .tsx won't be treat as page,
experimental: {
serverComponents: true,
runtime: 'edge',
runtime: 'experimental-edge',
},
}
......@@ -7,5 +7,5 @@ export default function MyError() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -20,5 +20,5 @@ export default function page() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -32,5 +32,5 @@ export default function Page() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -9,5 +9,5 @@ const Page = () => {
export default Page
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -26,8 +26,8 @@ function Data() {
export default function () {
return (
<>
{process.version
? `Runtime: Node.js ${process.version}`
{process.env.NEXT_RUNTIME === 'edge'
? `Runtime: Node.js`
: 'Runtime: Edge/Browser'}
<br />
<div className="suspense">
......@@ -42,5 +42,5 @@ export default function () {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -31,5 +31,5 @@ export default function Page() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -23,5 +23,5 @@ export default function Page() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -9,5 +9,5 @@ export default function Page() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -2,6 +2,6 @@ module.exports = {
reactStrictMode: true,
experimental: {
serverComponents: true,
// runtime: 'edge',
// runtime: 'experimental-edge',
},
}
......@@ -3,5 +3,5 @@ export default (req) => {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -14,5 +14,5 @@ export default function Page() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -14,5 +14,5 @@ export default function Page() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
......@@ -2,7 +2,7 @@
import webdriver from 'next-webdriver'
export default function (context) {
it("should include global styles under `runtime: 'edge'`", async () => {
it("should include global styles under `runtime: 'experimental-edge'`", async () => {
const browser = await webdriver(context.appPort, '/global-styles')
const currentColor = await browser.eval(
`window.getComputedStyle(document.querySelector('#red')).color`
......
......@@ -59,7 +59,7 @@ describe('Edge runtime - errors', () => {
const edgeRuntimeBasicSuite = {
runTests: (context, env) => {
const options = { runtime: 'edge', env }
const options = { runtime: 'experimental-edge', env }
const distDir = join(appDir, '.next')
basic(context, options)
streaming(context, options)
......@@ -152,7 +152,7 @@ const nodejsRuntimeBasicSuite = {
},
beforeAll: () => {
error500Page.write(page500)
nextConfig.replace("runtime: 'edge'", "runtime: 'nodejs'")
nextConfig.replace("runtime: 'experimental-edge'", "runtime: 'nodejs'")
},
afterAll: () => {
error500Page.delete()
......
......@@ -10,5 +10,5 @@ export default function Index() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
}
module.exports = {
trailingSlash: true,
experimental: {
runtime: 'edge',
runtime: 'experimental-edge',
},
}
......@@ -11,4 +11,4 @@ export default function Page() {
)
}
export const config = { runtime: 'edge' }
export const config = { runtime: 'experimental-edge' }
......@@ -11,4 +11,4 @@ export default function Page() {
)
}
export const config = { runtime: 'edge' }
export const config = { runtime: 'experimental-edge' }
......@@ -6,4 +6,4 @@ export default function Page() {
)
}
export const config = { runtime: 'edge' }
export const config = { runtime: 'experimental-edge' }
......@@ -3,6 +3,6 @@ export default function Edge() {
}
export const config = {
runtime: 'edge',
runtime: 'experimental-edge',
amp: false,
}
......@@ -3,7 +3,7 @@ import { join } from 'path'
const fixtureDir = join(__dirname, 'fixtures')
function createNextConfig(runtime?: 'edge' | 'nodejs') {
function createNextConfig(runtime?: 'experimental-edge' | 'nodejs') {
return {
experimental: { runtime },
}
......@@ -31,7 +31,7 @@ describe('parse page runtime config', () => {
pageFilePath: join(fixtureDir, 'page-runtime/edge.js'),
nextConfig: createNextConfig(),
})
expect(runtime).toBe('edge')
expect(runtime).toBe('experimental-edge')
})
it('should return undefined if no runtime is specified', async () => {
......@@ -47,16 +47,16 @@ describe('fallback to the global runtime configuration', () => {
it('should fallback when gSP is defined and exported', async () => {
const { runtime } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/fallback-with-gsp.js'),
nextConfig: createNextConfig('edge'),
nextConfig: createNextConfig('experimental-edge'),
})
expect(runtime).toBe('edge')
expect(runtime).toBe('experimental-edge')
})
it('should fallback when gSP is re-exported from other module', async () => {
const { runtime } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'),
nextConfig: createNextConfig('edge'),
nextConfig: createNextConfig('experimental-edge'),
})
expect(runtime).toBe('edge')
expect(runtime).toBe('experimental-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