Unverified Commit 3d1a2872 authored by Gal Schlezinger's avatar Gal Schlezinger Committed by GitHub

support `runtime: edge` in api endpoints (#36947)

## Feature

This PR introduces the ability to provide `runtime: "edge"` in API endpoints, the same as the experimental RSC runtime configurations.

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 15354e9b
...@@ -164,6 +164,15 @@ export function getEdgeServerEntry(opts: { ...@@ -164,6 +164,15 @@ export function getEdgeServerEntry(opts: {
return `next-middleware-loader?${stringify(loaderParams)}!` return `next-middleware-loader?${stringify(loaderParams)}!`
} }
if (opts.page.startsWith('/api/')) {
const loaderParams: MiddlewareLoaderOptions = {
absolutePagePath: opts.absolutePagePath,
page: opts.page,
}
return `next-edge-function-loader?${stringify(loaderParams)}!`
}
const loaderParams: MiddlewareSSRLoaderQuery = { const loaderParams: MiddlewareSSRLoaderQuery = {
absolute500Path: opts.pages['/500'] || '', absolute500Path: opts.pages['/500'] || '',
absoluteAppPath: opts.pages['/_app'], absoluteAppPath: opts.pages['/_app'],
...@@ -409,7 +418,9 @@ export function runDependingOnPageType<T>(params: { ...@@ -409,7 +418,9 @@ export function runDependingOnPageType<T>(params: {
if (params.page === MIDDLEWARE_FILE) { if (params.page === MIDDLEWARE_FILE) {
return [params.onEdgeServer()] return [params.onEdgeServer()]
} else if (params.page.match(API_ROUTE)) { } else if (params.page.match(API_ROUTE)) {
return [params.onServer()] return params.pageRuntime === 'edge'
? [params.onEdgeServer()]
: [params.onServer()]
} else if (params.page === '/_document') { } else if (params.page === '/_document') {
return [params.onServer()] return [params.onServer()]
} else if ( } else if (
......
...@@ -94,7 +94,6 @@ import { ...@@ -94,7 +94,6 @@ import {
getUnresolvedModuleFromError, getUnresolvedModuleFromError,
copyTracedFiles, copyTracedFiles,
isReservedPage, isReservedPage,
isCustomErrorPage,
isServerComponentPage, isServerComponentPage,
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
...@@ -1255,10 +1254,7 @@ export default async function build( ...@@ -1255,10 +1254,7 @@ export default async function build(
isHybridAmp, isHybridAmp,
ssgPageRoutes, ssgPageRoutes,
initialRevalidateSeconds: false, initialRevalidateSeconds: false,
runtime: runtime: pageRuntime,
!isReservedPage(page) && !isCustomErrorPage(page)
? pageRuntime
: undefined,
pageDuration: undefined, pageDuration: undefined,
ssgPageDurations: undefined, ssgPageDurations: undefined,
}) })
......
...@@ -1232,6 +1232,7 @@ export default async function getBaseWebpackConfig( ...@@ -1232,6 +1232,7 @@ export default async function getBaseWebpackConfig(
'next-flight-client-entry-loader', 'next-flight-client-entry-loader',
'noop-loader', 'noop-loader',
'next-middleware-loader', 'next-middleware-loader',
'next-edge-function-loader',
'next-middleware-ssr-loader', 'next-middleware-ssr-loader',
'next-middleware-wasm-loader', 'next-middleware-wasm-loader',
'next-app-loader', 'next-app-loader',
......
...@@ -7,6 +7,7 @@ import { webpack5 } from 'next/dist/compiled/webpack/webpack' ...@@ -7,6 +7,7 @@ import { webpack5 } from 'next/dist/compiled/webpack/webpack'
export function getModuleBuildInfo(webpackModule: webpack5.Module) { export function getModuleBuildInfo(webpackModule: webpack5.Module) {
return webpackModule.buildInfo as { return webpackModule.buildInfo as {
nextEdgeMiddleware?: EdgeMiddlewareMeta nextEdgeMiddleware?: EdgeMiddlewareMeta
nextEdgeApiFunction?: EdgeMiddlewareMeta
nextEdgeSSR?: EdgeSSRMeta nextEdgeSSR?: EdgeSSRMeta
nextUsedEnvVars?: Set<string> nextUsedEnvVars?: Set<string>
nextWasmMiddlewareBinding?: WasmBinding nextWasmMiddlewareBinding?: WasmBinding
......
import { getModuleBuildInfo } from './get-module-build-info'
import { stringifyRequest } from '../stringify-request'
export type EdgeFunctionLoaderOptions = {
absolutePagePath: string
page: string
}
export default function middlewareLoader(this: any) {
const { absolutePagePath, page }: EdgeFunctionLoaderOptions =
this.getOptions()
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.nextEdgeApiFunction = {
page: page || '/',
}
return `
import { adapter } from 'next/dist/server/web/adapter'
// The condition is true when the "process" module is provided
if (process !== global.process) {
// prefer local process but global.process has correct "env"
process.env = global.process.env;
global.process = process;
}
var mod = require(${stringifiedPagePath})
var handler = mod.middleware || mod.default;
if (typeof handler !== 'function') {
throw new Error('The Edge Function "pages${page}" must export a \`default\` function');
}
export default function (opts) {
return adapter({
...opts,
page: ${JSON.stringify(page)},
handler,
})
}
`
}
...@@ -14,24 +14,26 @@ import { ...@@ -14,24 +14,26 @@ import {
NEXT_CLIENT_SSR_ENTRY_SUFFIX, NEXT_CLIENT_SSR_ENTRY_SUFFIX,
} from '../../../shared/lib/constants' } from '../../../shared/lib/constants'
interface EdgeFunctionDefinition {
env: string[]
files: string[]
name: string
page: string
regexp: string
wasm?: WasmBinding[]
}
export interface MiddlewareManifest { export interface MiddlewareManifest {
version: 1 version: 1
sortedMiddleware: string[] sortedMiddleware: string[]
clientInfo: [location: string, isSSR: boolean][] clientInfo: [location: string, isSSR: boolean][]
middleware: { middleware: { [page: string]: EdgeFunctionDefinition }
[page: string]: { functions: { [page: string]: EdgeFunctionDefinition }
env: string[]
files: string[]
name: string
page: string
regexp: string
wasm?: WasmBinding[]
}
}
} }
interface EntryMetadata { interface EntryMetadata {
edgeMiddleware?: EdgeMiddlewareMeta edgeMiddleware?: EdgeMiddlewareMeta
edgeApiFunction?: EdgeMiddlewareMeta
edgeSSR?: EdgeSSRMeta edgeSSR?: EdgeSSRMeta
env: Set<string> env: Set<string>
wasmBindings: Set<WasmBinding> wasmBindings: Set<WasmBinding>
...@@ -42,6 +44,7 @@ const middlewareManifest: MiddlewareManifest = { ...@@ -42,6 +44,7 @@ const middlewareManifest: MiddlewareManifest = {
sortedMiddleware: [], sortedMiddleware: [],
clientInfo: [], clientInfo: [],
middleware: {}, middleware: {},
functions: {},
version: 1, version: 1,
} }
...@@ -310,6 +313,8 @@ function getExtractMetadata(params: { ...@@ -310,6 +313,8 @@ function getExtractMetadata(params: {
entryMetadata.edgeSSR = buildInfo.nextEdgeSSR entryMetadata.edgeSSR = buildInfo.nextEdgeSSR
} else if (buildInfo?.nextEdgeMiddleware) { } else if (buildInfo?.nextEdgeMiddleware) {
entryMetadata.edgeMiddleware = buildInfo.nextEdgeMiddleware entryMetadata.edgeMiddleware = buildInfo.nextEdgeMiddleware
} else if (buildInfo?.nextEdgeApiFunction) {
entryMetadata.edgeApiFunction = buildInfo.nextEdgeApiFunction
} }
/** /**
...@@ -386,16 +391,19 @@ function getCreateAssets(params: { ...@@ -386,16 +391,19 @@ function getCreateAssets(params: {
// There should always be metadata for the entrypoint. // There should always be metadata for the entrypoint.
const metadata = metadataByEntry.get(entrypoint.name) const metadata = metadataByEntry.get(entrypoint.name)
const page = metadata?.edgeMiddleware?.page || metadata?.edgeSSR?.page const page =
metadata?.edgeMiddleware?.page ||
metadata?.edgeSSR?.page ||
metadata?.edgeApiFunction?.page
if (!page) { if (!page) {
continue continue
} }
const { namedRegex } = getNamedMiddlewareRegex(page, { const { namedRegex } = getNamedMiddlewareRegex(page, {
catchAll: !metadata.edgeSSR, catchAll: !metadata.edgeSSR && !metadata.edgeApiFunction,
}) })
middlewareManifest.middleware[page] = { const edgeFunctionDefinition: EdgeFunctionDefinition = {
env: Array.from(metadata.env), env: Array.from(metadata.env),
files: getEntryFiles(entrypoint.getFiles(), metadata), files: getEntryFiles(entrypoint.getFiles(), metadata),
name: entrypoint.name, name: entrypoint.name,
...@@ -403,6 +411,12 @@ function getCreateAssets(params: { ...@@ -403,6 +411,12 @@ function getCreateAssets(params: {
regexp: namedRegex, regexp: namedRegex,
wasm: Array.from(metadata.wasmBindings), wasm: Array.from(metadata.wasmBindings),
} }
if (metadata.edgeApiFunction /* || metadata.edgeSSR */) {
middlewareManifest.functions[page] = edgeFunctionDefinition
} else {
middlewareManifest.middleware[page] = edgeFunctionDefinition
}
} }
middlewareManifest.sortedMiddleware = getSortedRoutes( middlewareManifest.sortedMiddleware = getSortedRoutes(
......
...@@ -19,7 +19,7 @@ function requestToBodyStream(request: IncomingMessage): BodyStream { ...@@ -19,7 +19,7 @@ function requestToBodyStream(request: IncomingMessage): BodyStream {
return transform.readable as unknown as ReadableStream<Uint8Array> return transform.readable as unknown as ReadableStream<Uint8Array>
} }
function bodyStreamToNodeStream(bodyStream: BodyStream): Readable { export function bodyStreamToNodeStream(bodyStream: BodyStream): Readable {
const reader = bodyStream.getReader() const reader = bodyStream.getReader()
return Readable.from( return Readable.from(
(async function* () { (async function* () {
......
...@@ -354,7 +354,9 @@ export default class DevServer extends Server { ...@@ -354,7 +354,9 @@ export default class DevServer extends Server {
onClient: () => {}, onClient: () => {},
onServer: () => {}, onServer: () => {},
onEdgeServer: () => { onEdgeServer: () => {
routedMiddleware.push(pageName) if (!pageName.startsWith('/api/')) {
routedMiddleware.push(pageName)
}
ssrMiddleware.add(pageName) ssrMiddleware.add(pageName)
}, },
}) })
......
...@@ -72,9 +72,9 @@ import { getCustomRoute } from './server-route-utils' ...@@ -72,9 +72,9 @@ import { getCustomRoute } from './server-route-utils'
import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring' import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring'
import ResponseCache from '../server/response-cache' import ResponseCache from '../server/response-cache'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash' import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { clonableBodyForRequest } from './body-streams'
import { getMiddlewareRegex } from '../shared/lib/router/utils/route-regex'
import { getNextPathnameInfo } from '../shared/lib/router/utils/get-next-pathname-info' import { getNextPathnameInfo } from '../shared/lib/router/utils/get-next-pathname-info'
import { getMiddlewareRegex } from '../shared/lib/router/utils/route-regex'
import { bodyStreamToNodeStream, clonableBodyForRequest } from './body-streams'
export * from './base-server' export * from './base-server'
...@@ -540,6 +540,19 @@ export default class NextNodeServer extends BaseServer { ...@@ -540,6 +540,19 @@ export default class NextNodeServer extends BaseServer {
page: string, page: string,
builtPagePath: string builtPagePath: string
): Promise<boolean> { ): Promise<boolean> {
const handledAsEdgeFunction = await this.runEdgeFunctionApiEndpoint({
req,
res,
query,
params,
page,
builtPagePath,
})
if (handledAsEdgeFunction) {
return true
}
const pageModule = await require(builtPagePath) const pageModule = await require(builtPagePath)
query = { ...query, ...params } query = { ...query, ...params }
...@@ -1031,11 +1044,15 @@ export default class NextNodeServer extends BaseServer { ...@@ -1031,11 +1044,15 @@ export default class NextNodeServer extends BaseServer {
} }
/** /**
* Get information for the middleware located in the provided page * Get information for the edge function located in the provided page
* folder. If the middleware info can't be found it will throw * folder. If the edge function info can't be found it will throw
* an error. * an error.
*/ */
protected getMiddlewareInfo(page: string) { protected getEdgeFunctionInfo(params: {
page: string
/** Whether we should look for a middleware or not */
middleware: boolean
}) {
const manifest: MiddlewareManifest = require(join( const manifest: MiddlewareManifest = require(join(
this.serverDistDir, this.serverDistDir,
MIDDLEWARE_MANIFEST MIDDLEWARE_MANIFEST
...@@ -1044,12 +1061,14 @@ export default class NextNodeServer extends BaseServer { ...@@ -1044,12 +1061,14 @@ export default class NextNodeServer extends BaseServer {
let foundPage: string let foundPage: string
try { try {
foundPage = denormalizePagePath(normalizePagePath(page)) foundPage = denormalizePagePath(normalizePagePath(params.page))
} catch (err) { } catch (err) {
throw pageNotFoundError(page) throw pageNotFoundError(params.page)
} }
let pageInfo = manifest.middleware[foundPage] let pageInfo = params.middleware
? manifest.middleware[foundPage]
: manifest.functions[foundPage]
if (!pageInfo) { if (!pageInfo) {
throw pageNotFoundError(foundPage) throw pageNotFoundError(foundPage)
} }
...@@ -1075,7 +1094,10 @@ export default class NextNodeServer extends BaseServer { ...@@ -1075,7 +1094,10 @@ export default class NextNodeServer extends BaseServer {
_isSSR?: boolean _isSSR?: boolean
): Promise<boolean> { ): Promise<boolean> {
try { try {
return this.getMiddlewareInfo(pathname).paths.length > 0 return (
this.getEdgeFunctionInfo({ page: pathname, middleware: true }).paths
.length > 0
)
} catch (_) {} } catch (_) {}
return false return false
...@@ -1142,7 +1164,10 @@ export default class NextNodeServer extends BaseServer { ...@@ -1142,7 +1164,10 @@ export default class NextNodeServer extends BaseServer {
} }
await this.ensureMiddleware(middleware.page, middleware.ssr) await this.ensureMiddleware(middleware.page, middleware.ssr)
const middlewareInfo = this.getMiddlewareInfo(middleware.page) const middlewareInfo = this.getEdgeFunctionInfo({
page: middleware.page,
middleware: true,
})
result = await run({ result = await run({
name: middlewareInfo.name, name: middlewareInfo.name,
...@@ -1411,4 +1436,80 @@ export default class NextNodeServer extends BaseServer { ...@@ -1411,4 +1436,80 @@ export default class NextNodeServer extends BaseServer {
this.warnIfQueryParametersWereDeleted = () => {} this.warnIfQueryParametersWereDeleted = () => {}
} }
} }
private async runEdgeFunctionApiEndpoint(params: {
req: NodeNextRequest
res: NodeNextResponse
query: ParsedUrlQuery
params: Params | false
page: string
builtPagePath: string
}): Promise<boolean> {
let middlewareInfo: ReturnType<typeof this.getEdgeFunctionInfo> | undefined
try {
middlewareInfo = this.getEdgeFunctionInfo({
page: params.page,
middleware: false,
})
} catch {
return false
}
// For middleware to "fetch" we must always provide an absolute URL
const url = getRequestMeta(params.req, '__NEXT_INIT_URL')!
if (!url.startsWith('http')) {
throw new Error(
'To use middleware you must provide a `hostname` and `port` to the Next.js Server'
)
}
const result = await run({
name: middlewareInfo.name,
paths: middlewareInfo.paths,
env: middlewareInfo.env,
wasm: middlewareInfo.wasm,
request: {
headers: params.req.headers,
method: params.req.method,
nextConfig: {
basePath: this.nextConfig.basePath,
i18n: this.nextConfig.i18n,
trailingSlash: this.nextConfig.trailingSlash,
},
url,
page: {
name: params.page,
...(params.params && { params: params.params }),
},
// TODO(gal): complete body
// body: originalBody?.cloneBodyStream(),
},
useCache: !this.nextConfig.experimental.runtime,
onWarning: (_warning: Error) => {
// if (params.onWarning) {
// warning.message += ` "./${middlewareInfo.name}"`
// params.onWarning(warning)
// }
},
})
params.res.statusCode = result.response.status
params.res.statusMessage = result.response.statusText
result.response.headers.forEach((value, key) => {
params.res.appendHeader(key, value)
})
if (result.response.body) {
// TODO(gal): not sure that we always need to stream
bodyStreamToNodeStream(result.response.body).pipe(
params.res.originalResponse
)
} else {
params.res.originalResponse.end()
}
return true
}
} }
export default (req) => {
return new Response(`Hello from ${req.url}`)
}
export const config = {
runtime: 'edge',
}
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
renderViaHTTP, renderViaHTTP,
waitFor, waitFor,
} from 'next-test-utils' } from 'next-test-utils'
import { readJson } from 'fs-extra'
const appDir = join(__dirname, '../switchable-runtime') const appDir = join(__dirname, '../switchable-runtime')
...@@ -174,6 +175,31 @@ describe('Switchable runtime (prod)', () => { ...@@ -174,6 +175,31 @@ describe('Switchable runtime (prod)', () => {
}) })
}) })
it('should build /api/hello as an api route with edge runtime', async () => {
const response = await fetchViaHTTP(context.appPort, '/api/hello')
const text = await response.text()
expect(text).toMatch(/Hello from .+\/api\/hello/)
const manifest = await readJson(
join(context.appDir, '.next/server/middleware-manifest.json')
)
expect(manifest).toMatchObject({
functions: {
'/api/hello': {
env: [],
files: [
'server/edge-runtime-webpack.js',
'server/pages/api/hello.js',
],
name: 'pages/api/hello',
page: '/api/hello',
regexp: '^/api/hello$',
wasm: [],
},
},
})
})
it('should display correct tree view with page types in terminal', async () => { it('should display correct tree view with page types in terminal', async () => {
const stdoutLines = splitLines(context.stdout).filter((line) => const stdoutLines = splitLines(context.stdout).filter((line) =>
/^[┌├└/]/.test(line) /^[┌├└/]/.test(line)
...@@ -181,6 +207,8 @@ describe('Switchable runtime (prod)', () => { ...@@ -181,6 +207,8 @@ describe('Switchable runtime (prod)', () => {
const expectedOutputLines = splitLines(` const expectedOutputLines = splitLines(`
┌ /_app ┌ /_app
├ ○ /404 ├ ○ /404
├ ℇ /api/hello
├ λ /api/node
├ ℇ /edge ├ ℇ /edge
├ ℇ /edge-rsc ├ ℇ /edge-rsc
├ ○ /node ├ ○ /node
...@@ -192,12 +220,16 @@ describe('Switchable runtime (prod)', () => { ...@@ -192,12 +220,16 @@ describe('Switchable runtime (prod)', () => {
├ λ /node-ssr ├ λ /node-ssr
└ ○ /static └ ○ /static
`) `)
const isMatched = expectedOutputLines.every((line, index) => {
const matched = stdoutLines[index].startsWith(line) const mappedOutputLines = expectedOutputLines.map((_line, index) => {
return matched /** @type {string} */
const str = stdoutLines[index]
const beginningOfPath = str.indexOf('/')
const endOfPath = str.indexOf(' ', beginningOfPath)
return str.slice(0, endOfPath)
}) })
expect(isMatched).toBe(true) expect(mappedOutputLines).toEqual(expectedOutputLines)
}) })
it('should prefetch data for static pages', async () => { it('should prefetch data for static pages', async () => {
...@@ -339,4 +371,29 @@ describe('Switchable runtime (dev)', () => { ...@@ -339,4 +371,29 @@ describe('Switchable runtime (dev)', () => {
'This is a static RSC page.' 'This is a static RSC page.'
) )
}) })
it('should build /api/hello as an api route with edge runtime', async () => {
const response = await fetchViaHTTP(context.appPort, '/api/hello')
const text = await response.text()
expect(text).toMatch(/Hello from .+\/api\/hello/)
const manifest = await readJson(
join(context.appDir, '.next/server/middleware-manifest.json')
)
expect(manifest).toMatchObject({
functions: {
'/api/hello': {
env: [],
files: [
'server/edge-runtime-webpack.js',
'server/pages/api/hello.js',
],
name: 'pages/api/hello',
page: '/api/hello',
regexp: '^/api/hello$',
wasm: [],
},
},
})
})
}) })
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