diff --git a/packages/next/build/analysis/get-page-static-info.ts b/packages/next/build/analysis/get-page-static-info.ts index 3e6c4de580e67..704d343bf57f6 100644 --- a/packages/next/build/analysis/get-page-static-info.ts +++ b/packages/next/build/analysis/get-page-static-info.ts @@ -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 { @@ -184,6 +185,14 @@ function getMiddlewareConfig( 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) { result.unstable_allowDynamicGlobs = Array.isArray( config.unstable_allowDynamic @@ -256,7 +265,7 @@ export async function getPageStaticInfo(params: { const fileContent = (await tryToReadFile(pageFilePath, !isDev)) || '' if ( - /runtime|getStaticProps|getServerSideProps|matcher|unstable_allowDynamic/.test( + /runtime|getStaticProps|getServerSideProps|matcher|unstable_allowDynamic|export const config/.test( fileContent ) ) { diff --git a/packages/next/build/webpack/plugins/middleware-plugin.ts b/packages/next/build/webpack/plugins/middleware-plugin.ts index cee3db5e520e0..7a09595941a0e 100644 --- a/packages/next/build/webpack/plugins/middleware-plugin.ts +++ b/packages/next/build/webpack/plugins/middleware-plugin.ts @@ -35,6 +35,7 @@ export interface EdgeFunctionDefinition { matchers: MiddlewareMatcher[] wasm?: AssetBinding[] assets?: AssetBinding[] + regions?: string[] | string } export interface MiddlewareManifest { @@ -51,6 +52,7 @@ interface EntryMetadata { env: Set wasmBindings: Map assetBindings: Map + regions?: string[] | string } const NAME = 'MiddlewarePlugin' @@ -173,6 +175,7 @@ function getCreateAssets(params: { name, filePath, })), + ...(metadata.regions && { regions: metadata.regions }), } if (metadata.edgeApiFunction || metadata.edgeSSR) { @@ -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 corresponding metadata. diff --git a/test/e2e/middleware-general/app/middleware.js b/test/e2e/middleware-general/app/middleware.js index 5a5f2f903d019..54e5b196af44d 100644 --- a/test/e2e/middleware-general/app/middleware.js +++ b/test/e2e/middleware-general/app/middleware.js @@ -2,6 +2,8 @@ import { NextRequest, NextResponse, URLPattern } from 'next/server' import magicValue from 'shared-package' +export const config = { regions: 'auto' } + const PATTERNS = [ [ new URLPattern({ pathname: '/:locale/:id' }), diff --git a/test/e2e/middleware-general/app/pages/api/edge-search-params.js b/test/e2e/middleware-general/app/pages/api/edge-search-params.js index a67cecdf18ec0..01a968ee9d806 100644 --- a/test/e2e/middleware-general/app/pages/api/edge-search-params.js +++ b/test/e2e/middleware-general/app/pages/api/edge-search-params.js @@ -1,6 +1,6 @@ import { NextResponse } from 'next/server' -export const config = { runtime: 'experimental-edge' } +export const config = { runtime: 'experimental-edge', regions: 'default' } /** * @param {import('next/server').NextRequest} diff --git a/test/e2e/middleware-general/test/index.test.ts b/test/e2e/middleware-general/test/index.test.ts index e5e19c6bc1e95..d7ce3a5853712 100644 --- a/test/e2e/middleware-general/test/index.test.ts +++ b/test/e2e/middleware-general/test/index.test.ts @@ -125,10 +125,22 @@ describe('Middleware Runtime', () => { matchers: [{ regexp: '^/.*$' }], wasm: [], 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 () => { const manifest = await fs.readJSON( join(next.testDir, '.next/server/middleware-manifest.json')