[edge] serialize custom config to middleware-manifest

Co-authored-by: 's avatarGal Schlezinger <gal@spitfire.co.il>
parent 980095d5
...@@ -18,6 +18,7 @@ import { RSC_MODULE_TYPES } from '../../shared/lib/constants' ...@@ -18,6 +18,7 @@ import { RSC_MODULE_TYPES } from '../../shared/lib/constants'
export interface MiddlewareConfig { export interface MiddlewareConfig {
matchers: MiddlewareMatcher[] matchers: MiddlewareMatcher[]
unstable_allowDynamicGlobs: string[] unstable_allowDynamicGlobs: string[]
regions: string[] | string
} }
export interface MiddlewareMatcher { export interface MiddlewareMatcher {
...@@ -184,6 +185,14 @@ function getMiddlewareConfig( ...@@ -184,6 +185,14 @@ function getMiddlewareConfig(
result.matchers = getMiddlewareMatchers(config.matcher, nextConfig) result.matchers = getMiddlewareMatchers(config.matcher, nextConfig)
} }
if (typeof config.regions === 'string' || Array.isArray(config.regions)) {
result.regions = config.regions
} else if (typeof config.regions !== 'undefined') {
Log.warn(
`The \`regions\` config was ignored: config must be empty, a string or an array of strings. (${pageFilePath})`
)
}
if (config.unstable_allowDynamic) { if (config.unstable_allowDynamic) {
result.unstable_allowDynamicGlobs = Array.isArray( result.unstable_allowDynamicGlobs = Array.isArray(
config.unstable_allowDynamic config.unstable_allowDynamic
...@@ -256,7 +265,7 @@ export async function getPageStaticInfo(params: { ...@@ -256,7 +265,7 @@ export async function getPageStaticInfo(params: {
const fileContent = (await tryToReadFile(pageFilePath, !isDev)) || '' const fileContent = (await tryToReadFile(pageFilePath, !isDev)) || ''
if ( if (
/runtime|getStaticProps|getServerSideProps|matcher|unstable_allowDynamic/.test( /runtime|getStaticProps|getServerSideProps|matcher|unstable_allowDynamic|export const config/.test(
fileContent fileContent
) )
) { ) {
......
...@@ -35,6 +35,7 @@ export interface EdgeFunctionDefinition { ...@@ -35,6 +35,7 @@ export interface EdgeFunctionDefinition {
matchers: MiddlewareMatcher[] matchers: MiddlewareMatcher[]
wasm?: AssetBinding[] wasm?: AssetBinding[]
assets?: AssetBinding[] assets?: AssetBinding[]
regions?: string[] | string
} }
export interface MiddlewareManifest { export interface MiddlewareManifest {
...@@ -51,6 +52,7 @@ interface EntryMetadata { ...@@ -51,6 +52,7 @@ interface EntryMetadata {
env: Set<string> env: Set<string>
wasmBindings: Map<string, string> wasmBindings: Map<string, string>
assetBindings: Map<string, string> assetBindings: Map<string, string>
regions?: string[] | string
} }
const NAME = 'MiddlewarePlugin' const NAME = 'MiddlewarePlugin'
...@@ -173,6 +175,7 @@ function getCreateAssets(params: { ...@@ -173,6 +175,7 @@ function getCreateAssets(params: {
name, name,
filePath, filePath,
})), })),
...(metadata.regions && { regions: metadata.regions }),
} }
if (metadata.edgeApiFunction || metadata.edgeSSR) { if (metadata.edgeApiFunction || metadata.edgeSSR) {
...@@ -732,6 +735,10 @@ function getExtractMetadata(params: { ...@@ -732,6 +735,10 @@ function getExtractMetadata(params: {
} }
} }
if (edgeFunctionConfig?.config?.regions) {
entryMetadata.regions = edgeFunctionConfig.config.regions
}
/** /**
* The entry module has to be either a page or a middleware and hold * The entry module has to be either a page or a middleware and hold
* the corresponding metadata. * the corresponding metadata.
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
import { NextRequest, NextResponse, URLPattern } from 'next/server' import { NextRequest, NextResponse, URLPattern } from 'next/server'
import magicValue from 'shared-package' import magicValue from 'shared-package'
export const config = { regions: 'auto' }
const PATTERNS = [ const PATTERNS = [
[ [
new URLPattern({ pathname: '/:locale/:id' }), new URLPattern({ pathname: '/:locale/:id' }),
......
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
export const config = { runtime: 'experimental-edge' } export const config = { runtime: 'experimental-edge', regions: 'default' }
/** /**
* @param {import('next/server').NextRequest} * @param {import('next/server').NextRequest}
......
...@@ -125,10 +125,22 @@ describe('Middleware Runtime', () => { ...@@ -125,10 +125,22 @@ describe('Middleware Runtime', () => {
matchers: [{ regexp: '^/.*$' }], matchers: [{ regexp: '^/.*$' }],
wasm: [], wasm: [],
assets: [], assets: [],
regions: 'auto',
}, },
}) })
}) })
it('should have the custom config in the manifest', async () => {
const manifest = await fs.readJSON(
join(next.testDir, '.next/server/middleware-manifest.json')
)
expect(manifest.functions['/api/edge-search-params']).toHaveProperty(
'regions',
'default'
)
})
it('should have correct files in manifest', async () => { it('should have correct files in manifest', async () => {
const manifest = await fs.readJSON( const manifest = await fs.readJSON(
join(next.testDir, '.next/server/middleware-manifest.json') join(next.testDir, '.next/server/middleware-manifest.json')
......
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