Skip to content

Commit

Permalink
feat: enables 'edge' as a possible runtime for API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy committed Dec 9, 2022
1 parent 5f27b9d commit d03539b
Show file tree
Hide file tree
Showing 49 changed files with 112 additions and 102 deletions.
2 changes: 1 addition & 1 deletion docs/advanced-features/react-18/switchable-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ For example, for API routes that rely on native Node.js APIs, they need to run w

```typescript
export const config = {
runtime: 'experimental-edge',
runtime: 'edge',
}

export default (req) => new Response('Hello world!')
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference/edge-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ You can relax the check to allow specific files with your Middleware or Edge API

```javascript
export const config = {
runtime: 'experimental-edge', // for Edge API Routes only
runtime: 'edge', // for Edge API Routes only
unstable_allowDynamic: [
'/lib/utilities.js', // allows a single file
'/node_modules/function-bind/**', // use a glob to allow anything in the function-bind 3rd party module
Expand Down
10 changes: 5 additions & 5 deletions docs/api-routes/edge-api-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Any file inside the folder `pages/api` is mapped to `/api/*` and will be treated

```typescript
export const config = {
runtime: 'experimental-edge',
runtime: 'edge',
}

export default (req) => new Response('Hello world!')
Expand All @@ -26,7 +26,7 @@ export default (req) => new Response('Hello world!')
import type { NextRequest } from 'next/server'

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

export default async function handler(req: NextRequest) {
Expand All @@ -50,7 +50,7 @@ export default async function handler(req: NextRequest) {
import type { NextRequest } from 'next/server'

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

export default async function handler(req: NextRequest) {
Expand All @@ -75,7 +75,7 @@ export default async function handler(req: NextRequest) {
import type { NextRequest } from 'next/server'

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

export default async function handler(req: NextRequest) {
Expand All @@ -91,7 +91,7 @@ export default async function handler(req: NextRequest) {
import { type NextRequest } from 'next/server'

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

export default async function handler(req: NextRequest) {
Expand Down
2 changes: 1 addition & 1 deletion errors/edge-dynamic-code-evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You can relax the check to allow specific files with your Middleware or Edge API

```typescript
export const config = {
runtime: 'experimental-edge', // for Edge API Routes only
runtime: 'edge', // for Edge API Routes only
unstable_allowDynamic: [
'/lib/utilities.js', // allows a single file
'/node_modules/function-bind/**', // use a glob to allow anything in the function-bind 3rd party module
Expand Down
2 changes: 1 addition & 1 deletion errors/middleware-upgrade-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ If you were previously using Middleware to forward headers to an external API, y
import { type NextRequest } from 'next/server'

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

export default async function handler(req: NextRequest) {
Expand Down
2 changes: 1 addition & 1 deletion examples/with-webassembly/pages/api/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export default async function edgeExample() {
return new Response(`got: ${number}`)
}

export const config = { runtime: 'experimental-edge' }
export const config = { runtime: 'edge' }
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 @@ -204,7 +204,7 @@ function extractValue(node: Node, path?: string[]): any {

/**
* Extracts the value of an exported const variable named `exportedName`
* (e.g. "export const config = { runtime: 'experimental-edge' }") from swc's AST.
* (e.g. "export const config = { runtime: 'edge' }") from swc's AST.
* The value must be one of (or throws UnsupportedValueError):
* - string
* - boolean
Expand Down
25 changes: 12 additions & 13 deletions packages/next/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isServerRuntime } from '../../server/config-shared'
import { isEdgeRuntime, isServerRuntime } from '../../server/config-shared'
import type { NextConfig } from '../../server/config-shared'
import type { Middleware, RouteHas } from '../../lib/load-custom-routes'
import {
Expand Down Expand Up @@ -229,13 +229,13 @@ function getMiddlewareConfig(
return result
}

let warnedAboutExperimentalEdgeApiFunctions = false
function warnAboutExperimentalEdgeApiFunctions() {
if (warnedAboutExperimentalEdgeApiFunctions) {
let warnedAboutExperimentalEdge = false
function warnAboutExperimentalEdge() {
if (warnedAboutExperimentalEdge) {
return
}
Log.warn(`You are using an experimental edge runtime, the API might change.`)
warnedAboutExperimentalEdgeApiFunctions = true
warnedAboutExperimentalEdge = true
}

const warnedUnsupportedValueMap = new Map<string, boolean>()
Expand Down Expand Up @@ -326,15 +326,14 @@ export async function getPageStaticInfo(params: {

const requiresServerRuntime = ssr || ssg || pageType === 'app'

resolvedRuntime =
SERVER_RUNTIME.edge === resolvedRuntime
? SERVER_RUNTIME.edge
: requiresServerRuntime
? resolvedRuntime || nextConfig.experimental?.runtime
: undefined
resolvedRuntime = isEdgeRuntime(resolvedRuntime)
? resolvedRuntime
: requiresServerRuntime
? resolvedRuntime || nextConfig.experimental?.runtime
: undefined

if (resolvedRuntime === SERVER_RUNTIME.edge) {
warnAboutExperimentalEdgeApiFunctions()
if (resolvedRuntime === SERVER_RUNTIME.experimentalEdge) {
warnAboutExperimentalEdge()
}

const middlewareConfig = getMiddlewareConfig(
Expand Down
6 changes: 3 additions & 3 deletions packages/next/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
PAGES_DIR_ALIAS,
ROOT_DIR_ALIAS,
APP_DIR_ALIAS,
SERVER_RUNTIME,
WEBPACK_LAYERS,
} from '../lib/constants'
import { APP_CLIENT_INTERNALS, RSC_MODULE_TYPES } from '../shared/lib/constants'
Expand All @@ -31,6 +30,7 @@ import {
EDGE_RUNTIME_WEBPACK,
} from '../shared/lib/constants'
import { __ApiPreviewProps } from '../server/api-utils'
import { isEdgeRuntime } from '../server/config-shared'
import { warn } from './output/log'
import {
isMiddlewareFile,
Expand Down Expand Up @@ -258,7 +258,7 @@ export async function runDependingOnPageType<T>(params: {
return
}
if (params.page.match(API_ROUTE)) {
if (params.pageRuntime === SERVER_RUNTIME.edge) {
if (isEdgeRuntime(params.pageRuntime)) {
await params.onEdgeServer()
return
}
Expand All @@ -279,7 +279,7 @@ export async function runDependingOnPageType<T>(params: {
await Promise.all([params.onClient(), params.onServer()])
return
}
if (params.pageRuntime === SERVER_RUNTIME.edge) {
if (isEdgeRuntime(params.pageRuntime)) {
await Promise.all([params.onClient(), params.onEdgeServer()])
return
}
Expand Down
9 changes: 4 additions & 5 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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 @@ -107,7 +106,7 @@ import getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
import { writeBuildId } from './write-build-id'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { NextConfigComplete } from '../server/config-shared'
import { isEdgeRuntime, NextConfigComplete } from '../server/config-shared'
import isError, { NextError } from '../lib/is-error'
import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
import { MiddlewareManifest } from './webpack/plugins/middleware-plugin'
Expand Down Expand Up @@ -1427,7 +1426,7 @@ export default async function build(
try {
let edgeInfo: any

if (pageRuntime === SERVER_RUNTIME.edge) {
if (isEdgeRuntime(pageRuntime)) {
if (pageType === 'app') {
edgeRuntimeAppCount++
} else {
Expand Down Expand Up @@ -1466,7 +1465,7 @@ export default async function build(
if (pageType === 'app' && originalAppPath) {
appNormalizedPaths.set(originalAppPath, page)
// TODO-APP: handle prerendering with edge
if (pageRuntime === 'experimental-edge') {
if (isEdgeRuntime(pageRuntime)) {
isStatic = false
isSsg = false
} else {
Expand Down Expand Up @@ -1504,7 +1503,7 @@ export default async function build(
)
}
} else {
if (pageRuntime === SERVER_RUNTIME.edge) {
if (isEdgeRuntime(pageRuntime)) {
if (workerResult.hasStaticProps) {
console.warn(
`"getStaticProps" is not yet supported fully with "experimental-edge", detected on ${page}`
Expand Down
6 changes: 3 additions & 3 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import {
SERVER_PROPS_GET_INIT_PROPS_CONFLICT,
SERVER_PROPS_SSG_CONFLICT,
MIDDLEWARE_FILENAME,
SERVER_RUNTIME,
} from '../lib/constants'
import { MODERN_BROWSERSLIST_TARGET } from '../shared/lib/constants'
import prettyBytes from '../lib/pretty-bytes'
import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher'
import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
import escapePathDelimiters from '../shared/lib/router/utils/escape-path-delimiters'
import { isEdgeRuntime } from '../server/config-shared'
import { findPageFile } from '../server/lib/find-page-file'
import { GetStaticPaths, PageConfig, ServerRuntime } from 'next/types'
import { BuildManifest } from '../server/get-page-files'
Expand Down Expand Up @@ -423,7 +423,7 @@ export async function printTreeView(
? '○'
: pageInfo?.isSsg
? '●'
: pageInfo?.runtime === SERVER_RUNTIME.edge
: isEdgeRuntime(pageInfo?.runtime)
? 'ℇ'
: 'λ'

Expand Down Expand Up @@ -1267,7 +1267,7 @@ export async function isPageStatic({
let prerenderFallback: boolean | 'blocking' | undefined
let appConfig: AppConfig = {}

if (pageRuntime === SERVER_RUNTIME.edge) {
if (isEdgeRuntime(pageRuntime)) {
const runtime = await getRuntimeContext({
paths: edgeInfo.files.map((file: string) => path.join(distDir, file)),
env: edgeInfo.env,
Expand Down
5 changes: 2 additions & 3 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
PAGES_DIR_ALIAS,
ROOT_DIR_ALIAS,
APP_DIR_ALIAS,
SERVER_RUNTIME,
WEBPACK_LAYERS,
RSC_MOD_REF_PROXY_ALIAS,
} from '../lib/constants'
Expand All @@ -31,7 +30,7 @@ import {
CompilerNameValues,
} from '../shared/lib/constants'
import { execOnce } from '../shared/lib/utils'
import { NextConfigComplete } from '../server/config-shared'
import { isEdgeRuntime, NextConfigComplete } from '../server/config-shared'
import { finalizeEntrypoint } from './entries'
import * as Log from './output/log'
import { buildConfiguration } from './webpack/config'
Expand Down Expand Up @@ -620,7 +619,7 @@ export default async function getBaseWebpackConfig(
const disableOptimizedLoading = true

if (isClient) {
if (config.experimental.runtime === SERVER_RUNTIME.edge) {
if (isEdgeRuntime(config.experimental.runtime)) {
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 @@ -72,7 +72,7 @@ export function getRender({
pagesType,
extendRenderOpts: {
buildId,
runtime: SERVER_RUNTIME.edge,
runtime: SERVER_RUNTIME.experimentalEdge,
supportsDynamicHTML: true,
disableOptimizedLoading: true,
serverComponentManifest,
Expand Down
6 changes: 5 additions & 1 deletion packages/next/build/webpack/plugins/flight-types-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ interface IEntry {
dynamicParams?: boolean
fetchCache?: 'auto' | 'force-no-store' | 'only-no-store' | 'default-no-store' | 'default-cache' | 'only-cache' | 'force-cache'
preferredRegion?: 'auto' | 'home' | 'edge'
${options.type === 'page' ? "runtime?: 'nodejs' | 'experimental-edge'" : ''}
${
options.type === 'page'
? "runtime?: 'nodejs' | 'experimental-edge' | 'edge'"
: ''
}
}
// =============
Expand Down
3 changes: 2 additions & 1 deletion packages/next/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export const ESLINT_PROMPT_VALUES = [
]

export const SERVER_RUNTIME: Record<string, ServerRuntime> = {
edge: 'experimental-edge',
edge: 'edge',
experimentalEdge: 'experimental-edge',
nodejs: 'nodejs',
}

Expand Down
5 changes: 3 additions & 2 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { RouteMatch } from '../shared/lib/router/utils/route-matcher'
import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher'
import type { Params } from '../shared/lib/router/utils/route-matcher'
import type { NextConfig, NextConfigComplete } from './config-shared'
import { isEdgeRuntime } 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 @@ -1080,7 +1081,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}
// strip header so we generate HTML still
if (
opts.runtime !== 'experimental-edge' ||
!isEdgeRuntime(opts.runtime) ||
(this.serverOptions as any).webServerConfig
) {
for (const param of FLIGHT_PARAMETERS) {
Expand Down Expand Up @@ -1293,7 +1294,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
})
if (
this.nextConfig.experimental.fetchCache &&
(opts.runtime !== 'experimental-edge' ||
(!isEdgeRuntime(opts.runtime) ||
(this.serverOptions as any).webServerConfig)
) {
delete req.headers[FETCH_CACHE_HEADER]
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/config-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextConfig } from './config'
import type { JSONSchemaType } from 'ajv'
import { VALID_LOADERS } from '../shared/lib/image-config'
import { SERVER_RUNTIME } from '../lib/constants'

const configSchema = {
type: 'object',
Expand Down Expand Up @@ -353,7 +354,7 @@ const configSchema = {
},
runtime: {
// automatic typing doesn't like enum
enum: ['experimental-edge', 'nodejs'] as any,
enum: Object.values(SERVER_RUNTIME) as any,
type: 'string',
},
serverComponentsExternalPackages: {
Expand Down
11 changes: 10 additions & 1 deletion packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { ServerRuntime } from 'next/types'
import { SubresourceIntegrityAlgorithm } from '../build/webpack/plugins/subresource-integrity-plugin'
import { WEB_VITALS } from '../shared/lib/utils'
import { SERVER_RUNTIME } from '../lib/constants'

export type NextConfigComplete = Required<NextConfig> & {
images: Required<ImageConfigComplete>
Expand Down Expand Up @@ -623,9 +624,17 @@ export async function normalizeConfig(phase: string, config: any) {
return await config
}

export function isEdgeRuntime(value?: string): value is ServerRuntime {
return (
value === SERVER_RUNTIME.experimentalEdge || value === SERVER_RUNTIME.edge
)
}

export function isServerRuntime(value?: string): value is ServerRuntime {
return (
value === undefined || value === 'nodejs' || value === 'experimental-edge'
value === undefined ||
value === SERVER_RUNTIME.nodejs ||
isEdgeRuntime(value)
)
}

Expand Down
1 change: 1 addition & 0 deletions packages/next/server/next-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const API_DOCS: Record<
'The `runtime` option controls the preferred runtime to render this route.',
options: {
'"nodejs"': 'Prefer the Node.js runtime.',
'"edge"': 'Prefer the Edge runtime.',
'"experimental-edge"': 'Prefer the experimental Edge runtime.',
},
link: 'https://beta.nextjs.org/docs/api-reference/segment-config#runtime',
Expand Down
2 changes: 1 addition & 1 deletion packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
// @ts-ignore This path is generated at build time and conflicts otherwise
import next from '../dist/server/next'

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

// @ts-ignore This path is generated at build time and conflicts otherwise
export { NextConfig } from '../dist/server/config'
Expand Down
Loading

0 comments on commit d03539b

Please sign in to comment.