Skip to content

Commit

Permalink
Rename allowDynamic to unstable_allowDynamic (#40496)
Browse files Browse the repository at this point in the history
Follow-up to #39539 as discussed
this renames to `unstable_` prefix initially while we test this out
further.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## 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 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.md#adding-examples)
  • Loading branch information
ijjk committed Sep 12, 2022
1 parent 97ac344 commit 8bf6a87
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions docs/api-reference/edge-runtime.md
Expand Up @@ -145,14 +145,14 @@ You can relax the check to allow specific files with your Middleware or Edge API
```javascript
export const config = {
runtime: 'experimental-edge', // for Edge API Routes only
allowDynamic: [
unstable_allowDynamic: [
'/lib/utilities.js', // allows a single file
'/node_modules/function-bind/**', // use a glob to allow anything in the function-bind 3rd party module
],
}
```

`allowDynamic` is a [glob](https://github.com/micromatch/micromatch#matching-features), or an array of globs, ignoring dynamic code evaluation for specific files. The globs are relative to your application root folder.
`unstable_allowDynamic` is a [glob](https://github.com/micromatch/micromatch#matching-features), or an array of globs, ignoring dynamic code evaluation for specific files. The globs are relative to your application root folder.

Be warned that if these statements are executed on the Edge, _they will throw and cause a runtime error_.

Expand Down
18 changes: 10 additions & 8 deletions packages/next/build/analysis/get-page-static-info.ts
Expand Up @@ -16,7 +16,7 @@ import { matcher } from 'next/dist/compiled/micromatch'

export interface MiddlewareConfig {
matchers: MiddlewareMatcher[]
allowDynamicGlobs: string[]
unstable_allowDynamicGlobs: string[]
}

export interface MiddlewareMatcher {
Expand Down Expand Up @@ -174,16 +174,18 @@ function getMiddlewareConfig(
result.matchers = getMiddlewareMatchers(config.matcher, nextConfig)
}

if (config.allowDynamic) {
result.allowDynamicGlobs = Array.isArray(config.allowDynamic)
? config.allowDynamic
: [config.allowDynamic]
for (const glob of result.allowDynamicGlobs ?? []) {
if (config.unstable_allowDynamic) {
result.unstable_allowDynamicGlobs = Array.isArray(
config.unstable_allowDynamic
)
? config.unstable_allowDynamic
: [config.unstable_allowDynamic]
for (const glob of result.unstable_allowDynamicGlobs ?? []) {
try {
matcher(glob)
} catch (err) {
throw new Error(
`${pageFilePath} exported 'config.allowDynamic' contains invalid pattern '${glob}': ${
`${pageFilePath} exported 'config.unstable_allowDynamic' contains invalid pattern '${glob}': ${
(err as Error).message
}`
)
Expand Down Expand Up @@ -244,7 +246,7 @@ export async function getPageStaticInfo(params: {

const fileContent = (await tryToReadFile(pageFilePath, !isDev)) || ''
if (
/runtime|getStaticProps|getServerSideProps|matcher|allowDynamic/.test(
/runtime|getStaticProps|getServerSideProps|matcher|unstable_allowDynamic/.test(
fileContent
)
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/next/build/webpack/plugins/middleware-plugin.ts
Expand Up @@ -255,7 +255,7 @@ function isDynamicCodeEvaluationAllowed(
rootDir?: string
) {
const name = fileName.replace(rootDir ?? '', '')
return isMatch(name, edgeFunctionConfig?.allowDynamicGlobs ?? [])
return isMatch(name, edgeFunctionConfig?.unstable_allowDynamicGlobs ?? [])
}

function buildUnsupportedApiError({
Expand Down Expand Up @@ -694,7 +694,7 @@ function getExtractMetadata(params: {
continue
}

if (edgeFunctionConfig?.config?.allowDynamicGlobs) {
if (edgeFunctionConfig?.config?.unstable_allowDynamicGlobs) {
telemetry.record({
eventName: 'NEXT_EDGE_ALLOW_DYNAMIC_USED',
payload: {
Expand Down
Expand Up @@ -63,7 +63,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next()
}
export const config = {
allowDynamic: '/middleware.js'
unstable_allowDynamic: '/middleware.js'
}
`)
context.api.write(`
Expand All @@ -73,7 +73,7 @@ describe('Edge runtime configurable guards', () => {
}
export const config = {
runtime: 'experimental-edge',
allowDynamic: '/lib/**'
unstable_allowDynamic: '/lib/**'
}
`)
})
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('Edge runtime configurable guards', () => {
}
export const config = {
runtime: 'experimental-edge',
allowDynamic: '**'
unstable_allowDynamic: '**'
}
`)
},
Expand All @@ -143,7 +143,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next()
}
export const config = {
allowDynamic: '**'
unstable_allowDynamic: '**'
}
`)
},
Expand All @@ -160,7 +160,7 @@ describe('Edge runtime configurable guards', () => {
}
export const config = {
runtime: 'experimental-edge',
allowDynamic: '/lib/**'
unstable_allowDynamic: '/lib/**'
}
`)
context.lib.write(`
Expand All @@ -184,7 +184,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next()
}
export const config = {
allowDynamic: '/lib/**'
unstable_allowDynamic: '/lib/**'
}
`)
context.lib.write(`
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('Edge runtime configurable guards', () => {
}
export const config = {
runtime: 'experimental-edge',
allowDynamic: '**'
unstable_allowDynamic: '**'
}
`)
},
Expand All @@ -241,7 +241,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next()
}
export const config = {
allowDynamic: '**'
unstable_allowDynamic: '**'
}
`)
},
Expand All @@ -258,7 +258,7 @@ describe('Edge runtime configurable guards', () => {
}
export const config = {
runtime: 'experimental-edge',
allowDynamic: '/lib/**'
unstable_allowDynamic: '/lib/**'
}
`)
context.lib.write(`
Expand All @@ -283,7 +283,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next()
}
export const config = {
allowDynamic: '/lib/**'
unstable_allowDynamic: '/lib/**'
}
`)
context.lib.write(`
Expand Down Expand Up @@ -329,7 +329,7 @@ describe('Edge runtime configurable guards', () => {
}
export const config = {
runtime: 'experimental-edge',
allowDynamic: '/pages/**'
unstable_allowDynamic: '/pages/**'
}
`)
context.lib.write(`
Expand All @@ -351,7 +351,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next()
}
export const config = {
allowDynamic: '/pages/**'
unstable_allowDynamic: '/pages/**'
}
`)
context.lib.write(`
Expand Down
6 changes: 3 additions & 3 deletions test/production/edge-config-validations/index.test.ts
Expand Up @@ -6,7 +6,7 @@ describe('Edge config validations', () => {

afterAll(() => next.destroy())

it('fails to build when allowDynamic is not a string', async () => {
it('fails to build when unstable_allowDynamic is not a string', async () => {
next = await createNext({
skipStart: true,
files: {
Expand All @@ -23,13 +23,13 @@ describe('Edge config validations', () => {
eval('toto')
export const config = { allowDynamic: true }
export const config = { unstable_allowDynamic: true }
`,
},
})
await expect(next.start()).rejects.toThrow('next build failed')
expect(next.cliOutput).toMatch(
`/middleware exported 'config.allowDynamic' contains invalid pattern 'true': Expected pattern to be a non-empty string`
`/middleware exported 'config.unstable_allowDynamic' contains invalid pattern 'true': Expected pattern to be a non-empty string`
)
})
})

0 comments on commit 8bf6a87

Please sign in to comment.