Skip to content

Commit

Permalink
apply code review suggestion by @feugy
Browse files Browse the repository at this point in the history
  • Loading branch information
Schniz committed Sep 27, 2022
1 parent 8cc7b08 commit 8d59882
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 41 deletions.
8 changes: 5 additions & 3 deletions packages/next/build/analysis/get-page-static-info.ts
Expand Up @@ -18,6 +18,7 @@ import { RSC_MODULE_TYPES } from '../../shared/lib/constants'
export interface MiddlewareConfig {
matchers: MiddlewareMatcher[]
unstable_allowDynamicGlobs: string[]
regions: string[] | string
}

export interface MiddlewareMatcher {
Expand All @@ -31,7 +32,6 @@ export interface PageStaticInfo {
ssg?: boolean
ssr?: boolean
rsc?: RSCModuleType
userConfig: Record<string, unknown> | undefined
middleware?: Partial<MiddlewareConfig>
}

Expand Down Expand Up @@ -185,6 +185,10 @@ function getMiddlewareConfig(
result.matchers = getMiddlewareMatchers(config.matcher, nextConfig)
}

if (typeof config.regions === 'string' || Array.isArray(config.regions)) {
result.regions = config.regions
}

if (config.unstable_allowDynamic) {
result.unstable_allowDynamicGlobs = Array.isArray(
config.unstable_allowDynamic
Expand Down Expand Up @@ -316,7 +320,6 @@ export async function getPageStaticInfo(params: {
ssr,
ssg,
rsc,
userConfig: config,
...(middlewareConfig && { middleware: middlewareConfig }),
...(runtime && { runtime }),
}
Expand All @@ -327,6 +330,5 @@ export async function getPageStaticInfo(params: {
ssg: false,
rsc: RSC_MODULE_TYPES.server,
runtime: nextConfig.experimental?.runtime,
userConfig: undefined,
}
}
5 changes: 0 additions & 5 deletions packages/next/build/entries.ts
Expand Up @@ -166,7 +166,6 @@ export function getEdgeServerEntry(opts: {
middleware?: Partial<MiddlewareConfig>
pagesType?: 'app' | 'pages' | 'root'
appDirLoader?: string
userConfig: Record<string, unknown> | undefined
}) {
if (isMiddlewareFile(opts.page)) {
const loaderParams: MiddlewareLoaderOptions = {
Expand All @@ -176,7 +175,6 @@ export function getEdgeServerEntry(opts: {
matchers: opts.middleware?.matchers
? encodeMatchers(opts.middleware.matchers)
: '',
...(opts.userConfig && { userConfig: JSON.stringify(opts.userConfig) }),
}

return `next-middleware-loader?${stringify(loaderParams)}!`
Expand All @@ -187,7 +185,6 @@ export function getEdgeServerEntry(opts: {
absolutePagePath: opts.absolutePagePath,
page: opts.page,
rootDir: opts.rootDir,
...(opts.userConfig && { userConfig: JSON.stringify(opts.userConfig) }),
}

return `next-edge-function-loader?${stringify(loaderParams)}!`
Expand All @@ -208,7 +205,6 @@ export function getEdgeServerEntry(opts: {
appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'),
sriEnabled: !opts.isDev && !!opts.config.experimental.sri?.algorithm,
hasFontLoaders: !!opts.config.experimental.fontLoaders,
...(opts.userConfig && { userConfig: JSON.stringify(opts.userConfig) }),
}

return {
Expand Down Expand Up @@ -500,7 +496,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
middleware: staticInfo?.middleware,
pagesType,
appDirLoader,
userConfig: staticInfo?.userConfig,
})
},
})
Expand Down
Expand Up @@ -14,7 +14,6 @@ export function getModuleBuildInfo(webpackModule: webpack.Module) {
nextEdgeApiFunction?: EdgeMiddlewareMeta
nextEdgeSSR?: EdgeSSRMeta
nextUsedEnvVars?: Set<string>
nextUserConfig?: Record<string, unknown> | undefined
nextWasmMiddlewareBinding?: AssetBinding
nextAssetMiddlewareBinding?: AssetBinding
usingIndirectEval?: boolean | Set<string>
Expand Down
10 changes: 2 additions & 8 deletions packages/next/build/webpack/loaders/next-edge-function-loader.ts
Expand Up @@ -5,19 +5,13 @@ export type EdgeFunctionLoaderOptions = {
absolutePagePath: string
page: string
rootDir: string
userConfig?: string
}

export default function middlewareLoader(this: any) {
const {
absolutePagePath,
page,
rootDir,
userConfig,
}: EdgeFunctionLoaderOptions = this.getOptions()
const { absolutePagePath, page, rootDir }: EdgeFunctionLoaderOptions =
this.getOptions()
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.nextUserConfig = userConfig ? JSON.parse(userConfig) : undefined
buildInfo.nextEdgeApiFunction = {
page: page || '/',
}
Expand Down
Expand Up @@ -16,7 +16,6 @@ export type EdgeSSRLoaderQuery = {
pagesType?: 'app' | 'pages' | 'root'
sriEnabled: boolean
hasFontLoaders: boolean
userConfig?: string
}

/*
Expand Down Expand Up @@ -47,7 +46,6 @@ export default async function edgeSSRLoader(this: any) {
pagesType,
sriEnabled,
hasFontLoaders,
userConfig,
} = this.getOptions()

const appDirLoader = Buffer.from(
Expand All @@ -62,7 +60,6 @@ export default async function edgeSSRLoader(this: any) {
page: page,
isAppDir,
}
buildInfo.nextUserConfig = userConfig ? JSON.parse(userConfig) : undefined
buildInfo.route = {
page,
absolutePagePath,
Expand Down
3 changes: 0 additions & 3 deletions packages/next/build/webpack/loaders/next-middleware-loader.ts
Expand Up @@ -8,7 +8,6 @@ export type MiddlewareLoaderOptions = {
page: string
rootDir: string
matchers?: string
userConfig?: string
}

// matchers can have special characters that break the loader params
Expand All @@ -29,12 +28,10 @@ export default function middlewareLoader(this: any) {
page,
rootDir,
matchers: encodedMatchers,
userConfig,
}: MiddlewareLoaderOptions = this.getOptions()
const matchers = encodedMatchers ? decodeMatchers(encodedMatchers) : undefined
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.nextUserConfig = userConfig ? JSON.parse(userConfig) : undefined
buildInfo.nextEdgeMiddleware = {
matchers,
page:
Expand Down
29 changes: 14 additions & 15 deletions packages/next/build/webpack/plugins/middleware-plugin.ts
Expand Up @@ -36,7 +36,6 @@ export interface EdgeFunctionDefinition {
matchers: MiddlewareMatcher[]
wasm?: AssetBinding[]
assets?: AssetBinding[]
// TODO(schniz): mark as optional
regions?: string[] | string
}

Expand All @@ -51,10 +50,10 @@ interface EntryMetadata {
edgeMiddleware?: EdgeMiddlewareMeta
edgeApiFunction?: EdgeMiddlewareMeta
edgeSSR?: EdgeSSRMeta
userConfig: Record<string, unknown> | undefined
env: Set<string>
wasmBindings: Map<string, string>
assetBindings: Map<string, string>
regions?: string[] | string
}

const NAME = 'MiddlewarePlugin'
Expand Down Expand Up @@ -167,13 +166,6 @@ function getCreateAssets(params: {
{ regexp: namedRegex },
]

const regions =
metadata.userConfig &&
(Array.isArray(metadata.userConfig.regions) ||
typeof metadata.userConfig.regions === 'string')
? metadata.userConfig.regions
: undefined

const edgeFunctionDefinition: EdgeFunctionDefinition = {
env: Array.from(metadata.env),
files: getEntryFiles(entrypoint.getFiles(), metadata, opts),
Expand All @@ -188,7 +180,7 @@ function getCreateAssets(params: {
name,
filePath,
})),
...(regions && { regions }),
...(metadata.regions && { regions: metadata.regions }),
}

if (metadata.edgeApiFunction || metadata.edgeSSR) {
Expand Down Expand Up @@ -686,7 +678,6 @@ function getExtractMetadata(params: {
env: new Set<string>(),
wasmBindings: new Map(),
assetBindings: new Map(),
userConfig: undefined,
}

for (const module of modules) {
Expand Down Expand Up @@ -749,6 +740,18 @@ function getExtractMetadata(params: {
}
}

if (edgeFunctionConfig?.config?.regions) {
telemetry.record({
eventName: 'NEXT_EDGE_REGIONS_USED',
payload: {
...edgeFunctionConfig,
file: edgeFunctionConfig.file.replace(rootDir ?? '', ''),
},
})

entryMetadata.regions = edgeFunctionConfig.config.regions
}

/**
* The entry module has to be either a page or a middleware and hold
* the corresponding metadata.
Expand All @@ -771,10 +774,6 @@ function getExtractMetadata(params: {
}
}

if (buildInfo?.nextUserConfig) {
entryMetadata.userConfig = buildInfo.nextUserConfig
}

/**
* If the module is a WASM module we read the binding information and
* append it to the entry wasm bindings.
Expand Down
4 changes: 1 addition & 3 deletions packages/next/server/dev/hot-reloader.ts
Expand Up @@ -46,7 +46,6 @@ import { getProperError } from '../../lib/is-error'
import ws from 'next/dist/compiled/ws'
import { promises as fs } from 'fs'
import { getPageStaticInfo } from '../../build/analysis/get-page-static-info'
import type { PageStaticInfo } from '../../build/analysis/get-page-static-info'
import { UnwrapPromise } from '../../lib/coalesced-function'

function diff(a: Set<any>, b: Set<any>) {
Expand Down Expand Up @@ -597,7 +596,7 @@ export default class HotReloader {
}

const isAppPath = !!this.appDir && bundlePath.startsWith('app/')
const staticInfo: Partial<PageStaticInfo> = isEntry
const staticInfo = isEntry
? await getPageStaticInfo({
pageFilePath: entryData.absolutePagePath,
nextConfig: this.config,
Expand Down Expand Up @@ -646,7 +645,6 @@ export default class HotReloader {
pages: this.pagesMapping,
isServerComponent,
appDirLoader,
userConfig: undefined,
pagesType: isAppPath ? 'app' : undefined,
}),
appDir: this.config.experimental.appDir,
Expand Down

0 comments on commit 8d59882

Please sign in to comment.