Unverified Commit 8bf6a873 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Rename allowDynamic to unstable_allowDynamic (#40496)

Follow-up to https://github.com/vercel/next.js/pull/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)
parent 97ac3444
...@@ -145,14 +145,14 @@ You can relax the check to allow specific files with your Middleware or Edge API ...@@ -145,14 +145,14 @@ You can relax the check to allow specific files with your Middleware or Edge API
```javascript ```javascript
export const config = { export const config = {
runtime: 'experimental-edge', // for Edge API Routes only runtime: 'experimental-edge', // for Edge API Routes only
allowDynamic: [ unstable_allowDynamic: [
'/lib/utilities.js', // allows a single file '/lib/utilities.js', // allows a single file
'/node_modules/function-bind/**', // use a glob to allow anything in the function-bind 3rd party module '/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_. Be warned that if these statements are executed on the Edge, _they will throw and cause a runtime error_.
......
...@@ -16,7 +16,7 @@ import { matcher } from 'next/dist/compiled/micromatch' ...@@ -16,7 +16,7 @@ import { matcher } from 'next/dist/compiled/micromatch'
export interface MiddlewareConfig { export interface MiddlewareConfig {
matchers: MiddlewareMatcher[] matchers: MiddlewareMatcher[]
allowDynamicGlobs: string[] unstable_allowDynamicGlobs: string[]
} }
export interface MiddlewareMatcher { export interface MiddlewareMatcher {
...@@ -174,16 +174,18 @@ function getMiddlewareConfig( ...@@ -174,16 +174,18 @@ function getMiddlewareConfig(
result.matchers = getMiddlewareMatchers(config.matcher, nextConfig) result.matchers = getMiddlewareMatchers(config.matcher, nextConfig)
} }
if (config.allowDynamic) { if (config.unstable_allowDynamic) {
result.allowDynamicGlobs = Array.isArray(config.allowDynamic) result.unstable_allowDynamicGlobs = Array.isArray(
? config.allowDynamic config.unstable_allowDynamic
: [config.allowDynamic] )
for (const glob of result.allowDynamicGlobs ?? []) { ? config.unstable_allowDynamic
: [config.unstable_allowDynamic]
for (const glob of result.unstable_allowDynamicGlobs ?? []) {
try { try {
matcher(glob) matcher(glob)
} catch (err) { } catch (err) {
throw new Error( throw new Error(
`${pageFilePath} exported 'config.allowDynamic' contains invalid pattern '${glob}': ${ `${pageFilePath} exported 'config.unstable_allowDynamic' contains invalid pattern '${glob}': ${
(err as Error).message (err as Error).message
}` }`
) )
...@@ -244,7 +246,7 @@ export async function getPageStaticInfo(params: { ...@@ -244,7 +246,7 @@ export async function getPageStaticInfo(params: {
const fileContent = (await tryToReadFile(pageFilePath, !isDev)) || '' const fileContent = (await tryToReadFile(pageFilePath, !isDev)) || ''
if ( if (
/runtime|getStaticProps|getServerSideProps|matcher|allowDynamic/.test( /runtime|getStaticProps|getServerSideProps|matcher|unstable_allowDynamic/.test(
fileContent fileContent
) )
) { ) {
......
...@@ -255,7 +255,7 @@ function isDynamicCodeEvaluationAllowed( ...@@ -255,7 +255,7 @@ function isDynamicCodeEvaluationAllowed(
rootDir?: string rootDir?: string
) { ) {
const name = fileName.replace(rootDir ?? '', '') const name = fileName.replace(rootDir ?? '', '')
return isMatch(name, edgeFunctionConfig?.allowDynamicGlobs ?? []) return isMatch(name, edgeFunctionConfig?.unstable_allowDynamicGlobs ?? [])
} }
function buildUnsupportedApiError({ function buildUnsupportedApiError({
...@@ -694,7 +694,7 @@ function getExtractMetadata(params: { ...@@ -694,7 +694,7 @@ function getExtractMetadata(params: {
continue continue
} }
if (edgeFunctionConfig?.config?.allowDynamicGlobs) { if (edgeFunctionConfig?.config?.unstable_allowDynamicGlobs) {
telemetry.record({ telemetry.record({
eventName: 'NEXT_EDGE_ALLOW_DYNAMIC_USED', eventName: 'NEXT_EDGE_ALLOW_DYNAMIC_USED',
payload: { payload: {
......
...@@ -63,7 +63,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -63,7 +63,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next() return NextResponse.next()
} }
export const config = { export const config = {
allowDynamic: '/middleware.js' unstable_allowDynamic: '/middleware.js'
} }
`) `)
context.api.write(` context.api.write(`
...@@ -73,7 +73,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -73,7 +73,7 @@ describe('Edge runtime configurable guards', () => {
} }
export const config = { export const config = {
runtime: 'experimental-edge', runtime: 'experimental-edge',
allowDynamic: '/lib/**' unstable_allowDynamic: '/lib/**'
} }
`) `)
}) })
...@@ -126,7 +126,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -126,7 +126,7 @@ describe('Edge runtime configurable guards', () => {
} }
export const config = { export const config = {
runtime: 'experimental-edge', runtime: 'experimental-edge',
allowDynamic: '**' unstable_allowDynamic: '**'
} }
`) `)
}, },
...@@ -143,7 +143,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -143,7 +143,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next() return NextResponse.next()
} }
export const config = { export const config = {
allowDynamic: '**' unstable_allowDynamic: '**'
} }
`) `)
}, },
...@@ -160,7 +160,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -160,7 +160,7 @@ describe('Edge runtime configurable guards', () => {
} }
export const config = { export const config = {
runtime: 'experimental-edge', runtime: 'experimental-edge',
allowDynamic: '/lib/**' unstable_allowDynamic: '/lib/**'
} }
`) `)
context.lib.write(` context.lib.write(`
...@@ -184,7 +184,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -184,7 +184,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next() return NextResponse.next()
} }
export const config = { export const config = {
allowDynamic: '/lib/**' unstable_allowDynamic: '/lib/**'
} }
`) `)
context.lib.write(` context.lib.write(`
...@@ -222,7 +222,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -222,7 +222,7 @@ describe('Edge runtime configurable guards', () => {
} }
export const config = { export const config = {
runtime: 'experimental-edge', runtime: 'experimental-edge',
allowDynamic: '**' unstable_allowDynamic: '**'
} }
`) `)
}, },
...@@ -241,7 +241,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -241,7 +241,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next() return NextResponse.next()
} }
export const config = { export const config = {
allowDynamic: '**' unstable_allowDynamic: '**'
} }
`) `)
}, },
...@@ -258,7 +258,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -258,7 +258,7 @@ describe('Edge runtime configurable guards', () => {
} }
export const config = { export const config = {
runtime: 'experimental-edge', runtime: 'experimental-edge',
allowDynamic: '/lib/**' unstable_allowDynamic: '/lib/**'
} }
`) `)
context.lib.write(` context.lib.write(`
...@@ -283,7 +283,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -283,7 +283,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next() return NextResponse.next()
} }
export const config = { export const config = {
allowDynamic: '/lib/**' unstable_allowDynamic: '/lib/**'
} }
`) `)
context.lib.write(` context.lib.write(`
...@@ -329,7 +329,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -329,7 +329,7 @@ describe('Edge runtime configurable guards', () => {
} }
export const config = { export const config = {
runtime: 'experimental-edge', runtime: 'experimental-edge',
allowDynamic: '/pages/**' unstable_allowDynamic: '/pages/**'
} }
`) `)
context.lib.write(` context.lib.write(`
...@@ -351,7 +351,7 @@ describe('Edge runtime configurable guards', () => { ...@@ -351,7 +351,7 @@ describe('Edge runtime configurable guards', () => {
return NextResponse.next() return NextResponse.next()
} }
export const config = { export const config = {
allowDynamic: '/pages/**' unstable_allowDynamic: '/pages/**'
} }
`) `)
context.lib.write(` context.lib.write(`
......
...@@ -6,7 +6,7 @@ describe('Edge config validations', () => { ...@@ -6,7 +6,7 @@ describe('Edge config validations', () => {
afterAll(() => next.destroy()) 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({ next = await createNext({
skipStart: true, skipStart: true,
files: { files: {
...@@ -23,13 +23,13 @@ describe('Edge config validations', () => { ...@@ -23,13 +23,13 @@ describe('Edge config validations', () => {
eval('toto') eval('toto')
export const config = { allowDynamic: true } export const config = { unstable_allowDynamic: true }
`, `,
}, },
}) })
await expect(next.start()).rejects.toThrow('next build failed') await expect(next.start()).rejects.toThrow('next build failed')
expect(next.cliOutput).toMatch( 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`
) )
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment