Skip to content

Commit

Permalink
[edge] serialize custom config to middleware-manifest (#40881)
Browse files Browse the repository at this point in the history
This PR serializes `regions` into `middleware-manifest.json`,
allowing to extend Edge Functions and Middleware for deployment
providers.

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: Seiya Nuta <nuta@seiya.me>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
3 people committed Sep 27, 2022
1 parent 9657f99 commit b24800b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
11 changes: 10 additions & 1 deletion 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 Down Expand Up @@ -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
Expand Down Expand Up @@ -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|export const config/.test(
fileContent
)
) {
Expand Down
7 changes: 7 additions & 0 deletions packages/next/build/webpack/plugins/middleware-plugin.ts
Expand Up @@ -36,6 +36,7 @@ export interface EdgeFunctionDefinition {
matchers: MiddlewareMatcher[]
wasm?: AssetBinding[]
assets?: AssetBinding[]
regions?: string[] | string
}

export interface MiddlewareManifest {
Expand All @@ -52,6 +53,7 @@ interface EntryMetadata {
env: Set<string>
wasmBindings: Map<string, string>
assetBindings: Map<string, string>
regions?: string[] | string
}

const NAME = 'MiddlewarePlugin'
Expand Down Expand Up @@ -178,6 +180,7 @@ function getCreateAssets(params: {
name,
filePath,
})),
...(metadata.regions && { regions: metadata.regions }),
}

if (metadata.edgeApiFunction || metadata.edgeSSR) {
Expand Down Expand Up @@ -737,6 +740,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.
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/middleware-general/app/middleware.js
Expand Up @@ -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' }),
Expand Down
@@ -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}
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/middleware-general/test/index.test.ts
Expand Up @@ -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')
Expand Down

0 comments on commit b24800b

Please sign in to comment.