Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove edge functions tracking in dev server #51122

Merged
10 changes: 6 additions & 4 deletions packages/next/src/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { RSC_MODULE_TYPES } from '../../shared/lib/constants'
import type { RSCMeta } from '../webpack/loaders/get-module-build-info'

export interface MiddlewareConfig {
matchers: MiddlewareMatcher[]
unstable_allowDynamicGlobs: string[]
regions: string[] | string
matchers?: MiddlewareMatcher[]
unstable_allowDynamicGlobs?: string[]
regions?: string[] | string
}

export interface MiddlewareMatcher {
Expand All @@ -39,7 +39,7 @@ export interface PageStaticInfo {
ssg?: boolean
ssr?: boolean
rsc?: RSCModuleType
middleware?: Partial<MiddlewareConfig>
middleware?: MiddlewareConfig
amp?: boolean | 'hybrid'
}

Expand Down Expand Up @@ -329,6 +329,8 @@ function warnAboutExperimentalEdge(apiRoute: string | null) {
if (apiRouteWarnings.has(apiRoute)) {
return
}

console.log({ apiRoute })
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
Log.warn(
apiRoute
? `${apiRoute} provided runtime 'experimental-edge'. It can be updated to 'edge' instead.`
Expand Down
24 changes: 24 additions & 0 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export function getEdgeServerEntry(opts: {
appDirLoader?: string
hasInstrumentationHook?: boolean
preferredRegion: string | string[] | undefined
middlewareConfig?: MiddlewareConfig
}) {
if (
opts.pagesType === 'app' &&
Expand All @@ -306,6 +307,9 @@ export function getEdgeServerEntry(opts: {
appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'),
nextConfigOutput: opts.config.output,
preferredRegion: opts.preferredRegion,
middlewareConfig: Buffer.from(
JSON.stringify(opts.middlewareConfig || {})
).toString('base64'),
}

return {
Expand All @@ -321,6 +325,10 @@ export function getEdgeServerEntry(opts: {
matchers: opts.middleware?.matchers
? encodeMatchers(opts.middleware.matchers)
: '',
preferredRegion: opts.preferredRegion,
middlewareConfig: Buffer.from(
JSON.stringify(opts.middlewareConfig || {})
).toString('base64'),
}

return `next-middleware-loader?${stringify(loaderParams)}!`
Expand All @@ -332,6 +340,9 @@ export function getEdgeServerEntry(opts: {
page: opts.page,
rootDir: opts.rootDir,
preferredRegion: opts.preferredRegion,
middlewareConfig: Buffer.from(
JSON.stringify(opts.middlewareConfig || {})
).toString('base64'),
}

return `next-edge-function-loader?${stringify(loaderParams)}!`
Expand Down Expand Up @@ -363,6 +374,9 @@ export function getEdgeServerEntry(opts: {
incrementalCacheHandlerPath:
opts.config.experimental.incrementalCacheHandlerPath,
preferredRegion: opts.preferredRegion,
middlewareConfig: Buffer.from(
JSON.stringify(opts.middlewareConfig || {})
).toString('base64'),
}

return {
Expand Down Expand Up @@ -569,6 +583,9 @@ export async function createEntrypoints(
assetPrefix: config.assetPrefix,
nextConfigOutput: config.output,
preferredRegion: staticInfo.preferredRegion,
middlewareConfig: Buffer.from(
JSON.stringify(staticInfo.middleware || {})
).toString('base64'),
})
} else if (isInstrumentationHookFile(page) && pagesType === 'root') {
server[serverBundlePath.replace('src/', '')] = {
Expand All @@ -586,6 +603,9 @@ export async function createEntrypoints(
page,
absolutePagePath,
preferredRegion: staticInfo.preferredRegion,
middlewareConfig: Buffer.from(
JSON.stringify(staticInfo.middleware || {})
).toString('base64'),
}),
]
} else {
Expand All @@ -609,6 +629,9 @@ export async function createEntrypoints(
// This isn't used with edge as it needs to be set on the entry module, which will be the `edgeServerEntry` instead.
// Still passing it here for consistency.
preferredRegion: staticInfo.preferredRegion,
middlewareConfig: Buffer.from(
JSON.stringify(staticInfo.middleware || {})
).toString('base64'),
}).import
}
const normalizedServerBundlePath =
Expand All @@ -627,6 +650,7 @@ export async function createEntrypoints(
pagesType,
appDirLoader,
preferredRegion: staticInfo.preferredRegion,
middlewareConfig: staticInfo.middleware,
})
},
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
MiddlewareConfig,
MiddlewareMatcher,
RSCModuleType,
} from '../../analysis/get-page-static-info'
Expand Down Expand Up @@ -56,6 +57,7 @@ export interface RouteMeta {
page: string
absolutePagePath: string
preferredRegion: string | string[] | undefined
middlewareConfig: MiddlewareConfig
}

export interface EdgeMiddlewareMeta {
Expand Down
7 changes: 7 additions & 0 deletions packages/next/src/build/webpack/loaders/next-app-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { RouteKind } from '../../../server/future/route-kind'
import { AppRouteRouteModuleOptions } from '../../../server/future/route-modules/app-route/module'
import { AppBundlePathNormalizer } from '../../../server/future/normalizers/built/app/app-bundle-path-normalizer'
import { FileType, fileExists } from '../../../lib/file-exists'
import { MiddlewareConfig } from '../../analysis/get-page-static-info'

export type AppLoaderOptions = {
name: string
Expand All @@ -37,6 +38,7 @@ export type AppLoaderOptions = {
isDev?: boolean
basePath: string
nextConfigOutput?: NextConfig['output']
middlewareConfig: string
}
type AppLoader = webpack.LoaderDefinitionFunction<AppLoaderOptions>

Expand Down Expand Up @@ -435,14 +437,19 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
nextConfigOutput,
preferredRegion,
basePath,
middlewareConfig: middlewareConfigBase64,
} = loaderOptions

const buildInfo = getModuleBuildInfo((this as any)._module)
const page = name.replace(/^app/, '')
const middlewareConfig: MiddlewareConfig = JSON.parse(
Buffer.from(middlewareConfigBase64, 'base64').toString()
)
buildInfo.route = {
page,
absolutePagePath: createAbsolutePath(appDir, pagePath),
preferredRegion,
middlewareConfig,
}

const extensions = pageExtensions.map((extension) => `.${extension}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { stringifyRequest } from '../../stringify-request'
import { NextConfig } from '../../../../server/config-shared'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { WEBPACK_RESOURCE_QUERIES } from '../../../../lib/constants'
import { MiddlewareConfig } from '../../../analysis/get-page-static-info'

export type EdgeAppRouteLoaderQuery = {
absolutePagePath: string
page: string
appDirLoader: string
preferredRegion: string | string[] | undefined
nextConfigOutput: NextConfig['output']
middlewareConfig: string
}

const EdgeAppRouteLoader: webpack.LoaderDefinitionFunction<EdgeAppRouteLoaderQuery> =
Expand All @@ -19,9 +21,13 @@ const EdgeAppRouteLoader: webpack.LoaderDefinitionFunction<EdgeAppRouteLoaderQue
absolutePagePath,
preferredRegion,
appDirLoader: appDirLoaderBase64 = '',
middlewareConfig: middlewareConfigBase64 = '',
} = this.getOptions()

const appDirLoader = Buffer.from(appDirLoaderBase64, 'base64').toString()
const middlewareConfig: MiddlewareConfig = JSON.parse(
Buffer.from(middlewareConfigBase64, 'base64').toString()
)

// Ensure we only run this loader for as a module.
if (!this._module) throw new Error('This loader is only usable as a module')
Expand All @@ -37,6 +43,7 @@ const EdgeAppRouteLoader: webpack.LoaderDefinitionFunction<EdgeAppRouteLoaderQue
page,
absolutePagePath,
preferredRegion,
middlewareConfig,
}

const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type webpack from 'webpack'
import { getModuleBuildInfo } from './get-module-build-info'
import { stringifyRequest } from '../stringify-request'
import { MiddlewareConfig } from '../../analysis/get-page-static-info'

export type EdgeFunctionLoaderOptions = {
absolutePagePath: string
page: string
rootDir: string
preferredRegion: string | string[] | undefined
middlewareConfig: string
}

const nextEdgeFunctionLoader: webpack.LoaderDefinitionFunction<EdgeFunctionLoaderOptions> =
Expand All @@ -16,13 +18,18 @@ const nextEdgeFunctionLoader: webpack.LoaderDefinitionFunction<EdgeFunctionLoade
page,
rootDir,
preferredRegion,
middlewareConfig: middlewareConfigBase64,
}: EdgeFunctionLoaderOptions = this.getOptions()
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const buildInfo = getModuleBuildInfo(this._module as any)
const middlewareConfig: MiddlewareConfig = JSON.parse(
Buffer.from(middlewareConfigBase64, 'base64').toString()
)
buildInfo.route = {
page: page || '/',
absolutePagePath,
preferredRegion,
middlewareConfig,
}
buildInfo.nextEdgeApiFunction = {
page: page || '/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type webpack from 'webpack'
import { getModuleBuildInfo } from '../get-module-build-info'
import { WEBPACK_RESOURCE_QUERIES } from '../../../../lib/constants'
import { stringifyRequest } from '../../stringify-request'
import { MiddlewareConfig } from '../../../analysis/get-page-static-info'

export type EdgeSSRLoaderQuery = {
absolute500Path: string
Expand All @@ -19,6 +20,7 @@ export type EdgeSSRLoaderQuery = {
sriEnabled: boolean
incrementalCacheHandlerPath?: string
preferredRegion: string | string[] | undefined
middlewareConfig: string
}

/*
Expand Down Expand Up @@ -51,8 +53,13 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
sriEnabled,
incrementalCacheHandlerPath,
preferredRegion,
middlewareConfig: middlewareConfigBase64,
} = this.getOptions()

const middlewareConfig: MiddlewareConfig = JSON.parse(
Buffer.from(middlewareConfigBase64, 'base64').toString()
)

const stringifiedConfig = Buffer.from(
stringifiedConfigBase64 || '',
'base64'
Expand All @@ -74,6 +81,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
page,
absolutePagePath,
preferredRegion,
middlewareConfig,
}

const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { MiddlewareMatcher } from '../../analysis/get-page-static-info'
import type {
MiddlewareConfig,
MiddlewareMatcher,
} from '../../analysis/get-page-static-info'
import { getModuleBuildInfo } from './get-module-build-info'
import { stringifyRequest } from '../stringify-request'
import { MIDDLEWARE_LOCATION_REGEXP } from '../../../lib/constants'
Expand All @@ -8,6 +11,8 @@ export type MiddlewareLoaderOptions = {
page: string
rootDir: string
matchers?: string
preferredRegion: string | string[] | undefined
middlewareConfig: string
}

// matchers can have special characters that break the loader params
Expand All @@ -28,16 +33,27 @@ export default function middlewareLoader(this: any) {
page,
rootDir,
matchers: encodedMatchers,
preferredRegion,
middlewareConfig: middlewareConfigBase64,
}: MiddlewareLoaderOptions = this.getOptions()
const matchers = encodedMatchers ? decodeMatchers(encodedMatchers) : undefined
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const middlewareConfig: MiddlewareConfig = JSON.parse(
Buffer.from(middlewareConfigBase64, 'base64').toString()
)
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.nextEdgeMiddleware = {
matchers,
page:
page.replace(new RegExp(`/${MIDDLEWARE_LOCATION_REGEXP}$`), '') || '/',
}
buildInfo.rootDir = rootDir
buildInfo.route = {
page,
absolutePagePath,
preferredRegion,
middlewareConfig,
}

return `
import 'next/dist/esm/server/web/globals'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getModuleBuildInfo } from '../get-module-build-info'
import { PagesRouteModuleOptions } from '../../../../server/future/route-modules/pages/module'
import { RouteKind } from '../../../../server/future/route-kind'
import { normalizePagePath } from '../../../../shared/lib/page-path/normalize-page-path'
import { MiddlewareConfig } from '../../../analysis/get-page-static-info'

/**
* The options for the route loader.
Expand All @@ -24,6 +25,8 @@ type RouteLoaderOptions = {
* The absolute path to the userland page file.
*/
absolutePagePath: string

middlewareConfig: string
}

/**
Expand All @@ -42,19 +45,29 @@ export function getRouteLoaderEntry(query: RouteLoaderOptions): string {
*/
const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
function () {
const { page, preferredRegion, absolutePagePath } = this.getOptions()
const {
page,
preferredRegion,
absolutePagePath,
middlewareConfig: middlewareConfigBase64,
} = this.getOptions()

// Ensure we only run this loader for as a module.
if (!this._module) {
throw new Error('Invariant: expected this to reference a module')
}

const middlewareConfig: MiddlewareConfig = JSON.parse(
Buffer.from(middlewareConfigBase64, 'base64').toString()
)

// Attach build info to the module.
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.route = {
page,
absolutePagePath,
preferredRegion,
middlewareConfig,
}

const options: Omit<PagesRouteModuleOptions, 'userland'> = {
Expand Down