Skip to content

Commit e7208a1

Browse files
ijjkSchniz
andcommitted
[edge] serialize custom config to middleware-manifest
Co-authored-by: Gal Schlezinger <[email protected]>
1 parent 980095d commit e7208a1

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

packages/next/build/analysis/get-page-static-info.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { RSC_MODULE_TYPES } from '../../shared/lib/constants'
1818
export interface MiddlewareConfig {
1919
matchers: MiddlewareMatcher[]
2020
unstable_allowDynamicGlobs: string[]
21+
regions: string[] | string
2122
}
2223

2324
export interface MiddlewareMatcher {
@@ -184,6 +185,14 @@ function getMiddlewareConfig(
184185
result.matchers = getMiddlewareMatchers(config.matcher, nextConfig)
185186
}
186187

188+
if (typeof config.regions === 'string' || Array.isArray(config.regions)) {
189+
result.regions = config.regions
190+
} else if (typeof config.regions !== 'undefined') {
191+
Log.warn(
192+
`The \`regions\` config was ignored: config must be empty, a string or an array of strings. (${pageFilePath})`
193+
)
194+
}
195+
187196
if (config.unstable_allowDynamic) {
188197
result.unstable_allowDynamicGlobs = Array.isArray(
189198
config.unstable_allowDynamic
@@ -256,7 +265,7 @@ export async function getPageStaticInfo(params: {
256265

257266
const fileContent = (await tryToReadFile(pageFilePath, !isDev)) || ''
258267
if (
259-
/runtime|getStaticProps|getServerSideProps|matcher|unstable_allowDynamic/.test(
268+
/runtime|getStaticProps|getServerSideProps|matcher|unstable_allowDynamic|export const config/.test(
260269
fileContent
261270
)
262271
) {

packages/next/build/webpack/plugins/middleware-plugin.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface EdgeFunctionDefinition {
3535
matchers: MiddlewareMatcher[]
3636
wasm?: AssetBinding[]
3737
assets?: AssetBinding[]
38+
regions?: string[] | string
3839
}
3940

4041
export interface MiddlewareManifest {
@@ -51,6 +52,7 @@ interface EntryMetadata {
5152
env: Set<string>
5253
wasmBindings: Map<string, string>
5354
assetBindings: Map<string, string>
55+
regions?: string[] | string
5456
}
5557

5658
const NAME = 'MiddlewarePlugin'
@@ -173,6 +175,7 @@ function getCreateAssets(params: {
173175
name,
174176
filePath,
175177
})),
178+
...(metadata.regions && { regions: metadata.regions }),
176179
}
177180

178181
if (metadata.edgeApiFunction || metadata.edgeSSR) {
@@ -732,6 +735,10 @@ function getExtractMetadata(params: {
732735
}
733736
}
734737

738+
if (edgeFunctionConfig?.config?.regions) {
739+
entryMetadata.regions = edgeFunctionConfig.config.regions
740+
}
741+
735742
/**
736743
* The entry module has to be either a page or a middleware and hold
737744
* the corresponding metadata.

test/e2e/middleware-general/app/middleware.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { NextRequest, NextResponse, URLPattern } from 'next/server'
33
import magicValue from 'shared-package'
44

5+
export const config = { regions: 'auto' }
6+
57
const PATTERNS = [
68
[
79
new URLPattern({ pathname: '/:locale/:id' }),

test/e2e/middleware-general/app/pages/api/edge-search-params.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextResponse } from 'next/server'
22

3-
export const config = { runtime: 'experimental-edge' }
3+
export const config = { runtime: 'experimental-edge', regions: 'default' }
44

55
/**
66
* @param {import('next/server').NextRequest}

test/e2e/middleware-general/test/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,22 @@ describe('Middleware Runtime', () => {
125125
matchers: [{ regexp: '^/.*$' }],
126126
wasm: [],
127127
assets: [],
128+
regions: 'auto',
128129
},
129130
})
130131
})
131132

133+
it('should have the custom config in the manifest', async () => {
134+
const manifest = await fs.readJSON(
135+
join(next.testDir, '.next/server/middleware-manifest.json')
136+
)
137+
138+
expect(manifest.functions['/api/edge-search-params']).toHaveProperty(
139+
'regions',
140+
'default'
141+
)
142+
})
143+
132144
it('should have correct files in manifest', async () => {
133145
const manifest = await fs.readJSON(
134146
join(next.testDir, '.next/server/middleware-manifest.json')

0 commit comments

Comments
 (0)