Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/next/src/build/templates/edge-app-route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { setManifestsSingleton } from '../../server/app-render/manifests-singleton'
import type { NextConfigComplete } from '../../server/config-shared'
import type { EdgeHandler } from '../../server/web/adapter'
import { EdgeRouteModuleWrapper } from '../../server/web/edge-route-module-wrapper'

// Import the userland code.
Expand All @@ -24,4 +25,8 @@ if (rscManifest && rscServerManifest) {

export const ComponentMod = module

export default EdgeRouteModuleWrapper.wrap(module.routeModule, { nextConfig })
const handler: EdgeHandler = EdgeRouteModuleWrapper.wrap(module.routeModule, {
nextConfig,
page: 'VAR_PAGE',
})
export default handler
11 changes: 8 additions & 3 deletions packages/next/src/build/templates/edge-ssr-app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import '../../server/web/globals'
import { adapter, type NextRequestHint } from '../../server/web/adapter'
import {
adapter,
type EdgeHandler,
type NextRequestHint,
} from '../../server/web/adapter'
import { IncrementalCache } from '../../server/lib/incremental-cache'

import * as pageMod from 'VAR_USERLAND'

import type { RequestData } from '../../server/web/types'
import type { NextConfigComplete } from '../../server/config-shared'
import { setManifestsSingleton } from '../../server/app-render/manifests-singleton'
import { initializeCacheHandlers } from '../../server/use-cache/handlers'
Expand Down Expand Up @@ -353,11 +356,13 @@ async function requestHandler(
)
}

export default function nHandler(opts: { page: string; request: RequestData }) {
const handler: EdgeHandler = (opts) => {
return adapter({
...opts,
IncrementalCache,
handler: requestHandler,
incrementalCacheHandler,
page: 'VAR_PAGE',
})
}
export default handler
11 changes: 8 additions & 3 deletions packages/next/src/build/templates/edge-ssr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import '../../server/web/globals'
import { adapter, type NextRequestHint } from '../../server/web/adapter'
import {
adapter,
type EdgeHandler,
type NextRequestHint,
} from '../../server/web/adapter'
import { IncrementalCache } from '../../server/lib/incremental-cache'
import { initializeCacheHandlers } from '../../server/use-cache/handlers'

Expand All @@ -18,7 +22,6 @@ import RouteModule, {
} from '../../server/route-modules/pages/module'
import { WebNextRequest, WebNextResponse } from '../../server/base-http/web'

import type { RequestData } from '../../server/web/types'
import type { NextConfigComplete } from '../../server/config-shared'
import type { NextFetchEvent } from '../../server/web/spec-extension/fetch-event'
import type RenderResult from '../../server/render-result'
Expand Down Expand Up @@ -354,12 +357,14 @@ async function requestHandler(
)
}

export default function nHandler(opts: { page: string; request: RequestData }) {
const handler: EdgeHandler = (opts) => {
return adapter({
...opts,
IncrementalCache,
handler: requestHandler,
incrementalCacheHandler,
bypassNextUrl: true,
page: 'VAR_PAGE',
})
}
export default handler
13 changes: 6 additions & 7 deletions packages/next/src/build/templates/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AdapterOptions } from '../../server/web/adapter'
import type { AdapterOptions, EdgeHandler } from '../../server/web/adapter'

import '../../server/web/globals'

Expand All @@ -13,7 +13,7 @@ const mod = { ..._mod }

const page: string = 'VAR_DEFINITION_PAGE'
const isProxy = page === '/proxy' || page === '/src/proxy'
const handler = (isProxy ? mod.proxy : mod.middleware) || mod.default
const handlerUserland = (isProxy ? mod.proxy : mod.middleware) || mod.default

class ProxyMissingExportError extends Error {
constructor(message: string) {
Expand All @@ -25,7 +25,7 @@ class ProxyMissingExportError extends Error {

// TODO: This spams logs during development. Find a better way to handle this.
// Removing this will spam "fn is not a function" logs which is worse.
if (typeof handler !== 'function') {
if (typeof handlerUserland !== 'function') {
throw new ProxyMissingExportError(
`The ${isProxy ? 'Proxy' : 'Middleware'} file "${page}" must export a function named \`${isProxy ? 'proxy' : 'middleware'}\` or a default function.`
)
Expand Down Expand Up @@ -69,12 +69,11 @@ function errorHandledHandler(fn: AdapterOptions['handler']) {
}
}

export default function nHandler(
opts: Omit<AdapterOptions, 'IncrementalCache' | 'page' | 'handler'>
) {
const handler: EdgeHandler = (opts) => {
return adapter({
...opts,
page,
handler: errorHandledHandler(handler),
handler: errorHandledHandler(handlerUserland),
})
}
export default handler
13 changes: 6 additions & 7 deletions packages/next/src/build/templates/pages-edge-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AdapterOptions } from '../../server/web/adapter'
import type { EdgeHandler } from '../../server/web/adapter'

import '../../server/web/globals'

Expand All @@ -7,23 +7,22 @@ import { IncrementalCache } from '../../server/lib/incremental-cache'
import { wrapApiHandler } from '../../server/api-utils'

// Import the userland code.
import handler from 'VAR_USERLAND'
import handlerUserland from 'VAR_USERLAND'

const page = 'VAR_DEFINITION_PAGE'

if (typeof handler !== 'function') {
if (typeof handlerUserland !== 'function') {
throw new Error(
`The Edge Function "pages${page}" must export a \`default\` function`
)
}

export default function (
opts: Omit<AdapterOptions, 'IncrementalCache' | 'page' | 'handler'>
) {
const handler: EdgeHandler = (opts) => {
return adapter({
...opts,
IncrementalCache,
page: 'VAR_DEFINITION_PATHNAME',
handler: wrapApiHandler(page, handler),
handler: wrapApiHandler(page, handlerUserland),
})
}
export default handler
6 changes: 6 additions & 0 deletions packages/next/src/server/web/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export type AdapterOptions = {
bypassNextUrl?: boolean
}

// This has to be compatible with what the Vercel builder does as well:
// https://github.com/vercel/vercel/blob/0e0a6eb9f12216202ae2f5ee37e4ada1796361fd/packages/next/src/edge-function-source/get-edge-function.ts#L112-L136
export type EdgeHandler = (opts: {
request: AdapterOptions['request']
}) => Promise<FetchEventResult>

let propagator: <T>(request: NextRequestHint, fn: () => T) => T = (
request,
fn
Expand Down
11 changes: 8 additions & 3 deletions packages/next/src/server/web/edge-route-module-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {

import './globals'

import { adapter, type AdapterOptions } from './adapter'
import { adapter, type EdgeHandler } from './adapter'
import { IncrementalCache } from '../lib/incremental-cache'
import { RouteMatcher } from '../route-matchers/route-matcher'
import type { NextFetchEvent } from './spec-extension/fetch-event'
Expand All @@ -19,6 +19,7 @@ import type { NextConfigComplete } from '../config-shared'

export interface WrapOptions {
nextConfig: NextConfigComplete
page: string
}

/**
Expand Down Expand Up @@ -52,17 +53,21 @@ export class EdgeRouteModuleWrapper {
* override the ones passed from the runtime
* @returns a function that can be used as a handler for the edge runtime
*/
public static wrap(routeModule: AppRouteRouteModule, options: WrapOptions) {
public static wrap(
routeModule: AppRouteRouteModule,
options: WrapOptions
): EdgeHandler {
// Create the module wrapper.
const wrapper = new EdgeRouteModuleWrapper(routeModule, options.nextConfig)

// Return the wrapping function.
return (opts: AdapterOptions) => {
return (opts) => {
return adapter({
...opts,
IncrementalCache,
// Bind the handler method to the wrapper so it still has context.
handler: wrapper.handler.bind(wrapper),
page: options.page,
})
}
}
Expand Down
8 changes: 3 additions & 5 deletions packages/next/src/server/web/sandbox/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NodejsRequestData, FetchEventResult, RequestData } from '../types'
import type { NodejsRequestData, FetchEventResult } from '../types'
import type { EdgeFunctionDefinition } from '../../../build/webpack/plugins/middleware-plugin'
import type { EdgeRuntime } from 'next/dist/compiled/edge-runtime'
import {
Expand All @@ -16,6 +16,7 @@ import {
RouterServerContextSymbol,
routerServerGlobal,
} from '../../lib/router-utils/router-server-context'
import type { EdgeHandler } from '../adapter'

export const ErrorSource = Symbol('SandboxError')

Expand Down Expand Up @@ -104,10 +105,7 @@ export async function getRuntimeContext(
export const run = withTaggedErrors(async function runWithTaggedErrors(params) {
const runtime = await getRuntimeContext(params)

// TODO better typesafety here
const edgeFunction: (args: {
request: RequestData
}) => Promise<FetchEventResult> = (
const edgeFunction: EdgeHandler = (
await runtime.context._ENTRIES[`middleware_${params.name}`]
).default

Expand Down
Loading