Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename allowDynamic to unstable_allowDynamic #40496

Merged
merged 1 commit into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`
)
})
})