Unverified Commit 1c677406 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Drop object config export support for app dir (#48489)

We've supported `export const config = { ... }` to sepecify `runtime`
etc. configs for pages under `app/` for a while. This PR is going to
drop it and only support `export const [config name] = [config value]`
per config

Add a warning if the legacy usage is detectd

Closes NEXT-1016
parent a92c5465
...@@ -314,7 +314,7 @@ export async function getPageStaticInfo(params: { ...@@ -314,7 +314,7 @@ export async function getPageStaticInfo(params: {
pageFilePath: string pageFilePath: string
isDev?: boolean isDev?: boolean
page?: string page?: string
pageType?: 'pages' | 'app' pageType: 'pages' | 'app' | 'root'
}): Promise<PageStaticInfo> { }): Promise<PageStaticInfo> {
const { isDev, pageFilePath, nextConfig, page, pageType } = params const { isDev, pageFilePath, nextConfig, page, pageType } = params
...@@ -329,7 +329,7 @@ export async function getPageStaticInfo(params: { ...@@ -329,7 +329,7 @@ export async function getPageStaticInfo(params: {
const rsc = getRSCModuleInformation(fileContent).type const rsc = getRSCModuleInformation(fileContent).type
// default / failsafe value for config // default / failsafe value for config
let config: any = {} let config: any
try { try {
config = extractExportedConstValue(swcAST, 'config') config = extractExportedConstValue(swcAST, 'config')
} catch (e) { } catch (e) {
...@@ -338,12 +338,26 @@ export async function getPageStaticInfo(params: { ...@@ -338,12 +338,26 @@ export async function getPageStaticInfo(params: {
} }
// `export config` doesn't exist, or other unknown error throw by swc, silence them // `export config` doesn't exist, or other unknown error throw by swc, silence them
} }
if (pageType === 'app') {
if (config) {
Log.warnOnce(
`\`export const config\` in ${pageFilePath} is deprecated. Please change it to segment export config. See https://beta.nextjs.org/docs/api-reference/segment-config`
)
config = {}
}
}
if (!config) config = {}
// Currently, we use `export const config = { runtime: '...' }` to specify the page runtime. // We use `export const config = { runtime: '...' }` to specify the page runtime for pages/.
// But in the new app directory, we prefer to use `export const runtime = '...'` // In the new app directory, we prefer to use `export const runtime = '...'`
// and deprecate the old way. To prevent breaking changes for `pages`, we use the exported config // and deprecate the old way. To prevent breaking changes for `pages`, we use the exported config
// as the fallback value. // as the fallback value.
let resolvedRuntime = runtime || config.runtime let resolvedRuntime
if (pageType === 'app') {
resolvedRuntime = runtime
} else {
resolvedRuntime = runtime || config.runtime
}
if ( if (
typeof resolvedRuntime !== 'undefined' && typeof resolvedRuntime !== 'undefined' &&
......
...@@ -37,3 +37,11 @@ export function event(...message: any[]) { ...@@ -37,3 +37,11 @@ export function event(...message: any[]) {
export function trace(...message: any[]) { export function trace(...message: any[]) {
console.log(prefixes.trace, ...message) console.log(prefixes.trace, ...message)
} }
const warnOnceMessages = new Set()
export function warnOnce(...message: any[]) {
if (!warnOnceMessages.has(message[0])) {
warnOnceMessages.add(message.join(' '))
warn(...message)
}
}
...@@ -605,6 +605,7 @@ async function findEntryEdgeFunctionConfig( ...@@ -605,6 +605,7 @@ async function findEntryEdgeFunctionConfig(
nextConfig: {}, nextConfig: {},
pageFilePath, pageFilePath,
isDev: false, isDev: false,
pageType: 'root',
}) })
).middleware, ).middleware,
} }
......
...@@ -714,6 +714,7 @@ export default class HotReloader { ...@@ -714,6 +714,7 @@ export default class HotReloader {
const isAppPath = hasAppDir && bundlePath.startsWith('app/') const isAppPath = hasAppDir && bundlePath.startsWith('app/')
const staticInfo = isEntry const staticInfo = isEntry
? await getPageStaticInfo({ ? await getPageStaticInfo({
page,
pageFilePath: entryData.absolutePagePath, pageFilePath: entryData.absolutePagePath,
nextConfig: this.config, nextConfig: this.config,
isDev: true, isDev: true,
......
...@@ -768,6 +768,7 @@ export function onDemandEntryHandler({ ...@@ -768,6 +768,7 @@ export function onDemandEntryHandler({
} }
const staticInfo = await getPageStaticInfo({ const staticInfo = await getPageStaticInfo({
page,
pageFilePath: pagePathData.absolutePagePath, pageFilePath: pagePathData.absolutePagePath,
nextConfig, nextConfig,
isDev: true, isDev: true,
......
...@@ -9,13 +9,21 @@ createNextDescribe( ...@@ -9,13 +9,21 @@ createNextDescribe(
}, },
({ next }) => { ({ next }) => {
it('should handle edge only routes', async () => { it('should handle edge only routes', async () => {
const appHtml = await next.render('/app-edge') const appHtml = await next.render('/edge/basic')
expect(appHtml).toContain('<p>Edge!</p>') expect(appHtml).toContain('<p>Edge!</p>')
const pageHtml = await next.render('/pages-edge') const pageHtml = await next.render('/pages-edge')
expect(pageHtml).toContain('<p>pages-edge-ssr</p>') expect(pageHtml).toContain('<p>pages-edge-ssr</p>')
}) })
it('should warn the legacy object config export', async () => {
await next.render('/edge/legacy')
expect(next.cliOutput).toContain('`export const config`')
expect(next.cliOutput).toContain(
'app/edge/legacy/page.tsx is deprecated. Please change it to segment export config. See https://beta.nextjs.org/docs/api-reference/segment-config'
)
})
it('should retrieve cookies in a server component in the edge runtime', async () => { it('should retrieve cookies in a server component in the edge runtime', async () => {
const res = await next.fetch('/edge-apis/cookies') const res = await next.fetch('/edge-apis/cookies')
expect(await res.text()).toInclude('Hello') expect(await res.text()).toInclude('Hello')
...@@ -34,21 +42,21 @@ createNextDescribe( ...@@ -34,21 +42,21 @@ createNextDescribe(
}) })
it('should handle edge rsc hmr', async () => { it('should handle edge rsc hmr', async () => {
const pageFile = 'app/app-edge/page.tsx' const pageFile = 'app/edge/basic/page.tsx'
const content = await next.readFile(pageFile) const content = await next.readFile(pageFile)
// Update rendered content // Update rendered content
const updatedContent = content.replace('Edge!', 'edge-hmr') const updatedContent = content.replace('Edge!', 'edge-hmr')
await next.patchFile(pageFile, updatedContent) await next.patchFile(pageFile, updatedContent)
await check(async () => { await check(async () => {
const html = await next.render('/app-edge') const html = await next.render('/edge/basic')
return html return html
}, /edge-hmr/) }, /edge-hmr/)
// Revert // Revert
await next.patchFile(pageFile, content) await next.patchFile(pageFile, content)
await check(async () => { await check(async () => {
const html = await next.render('/app-edge') const html = await next.render('/edge/basic')
return html return html
}, /Edge!/) }, /Edge!/)
}) })
......
...@@ -5,4 +5,4 @@ export default function Page() { ...@@ -5,4 +5,4 @@ export default function Page() {
return <p>Node!</p> return <p>Node!</p>
} }
export const runtime = 'experimental-edge' export const runtime = 'edge'
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
export const runtime = 'experimental-edge' export const runtime = 'edge'
export default function Page() { export default function Page() {
cookies() cookies()
......
...@@ -5,4 +5,4 @@ export default function Page() { ...@@ -5,4 +5,4 @@ export default function Page() {
return <p>Node!</p> return <p>Node!</p>
} }
export const runtime = 'experimental-edge' export const runtime = 'edge'
export default function Page() {
if ('EdgeRuntime' in globalThis) {
return <p>Edge!</p>
}
return <p>Node!</p>
}
export const config = {
runtime: 'edge',
}
{
"compilerOptions": {
"target": "ES6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": ".",
"paths": {
"@/ui/*": ["ui/*"]
},
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
'use client' 'use client'
import { usePathname } from 'next/navigation' import { usePathname } from 'next/navigation'
export const config = { export const dynamicParams = false
dynamicParams: false,
}
export default function Page() { export default function Page() {
const pathname = usePathname() const pathname = usePathname()
......
import Link from 'next/link' import Link from 'next/link'
export const config = { export const dynamicParams = false
dynamicParams: false,
}
export default function Page({ params }) { export default function Page({ params }) {
return ( return (
......
...@@ -198,6 +198,11 @@ createNextDescribe( ...@@ -198,6 +198,11 @@ createNextDescribe(
'Basic route' 'Basic route'
) )
expect(await browser.eval('window.__TEST_NO_RELOAD')).toBeUndefined() expect(await browser.eval('window.__TEST_NO_RELOAD')).toBeUndefined()
const res = await next.fetch(
`${next.url}/static-mpa-navigation/slug-not-existed`
)
expect(res.status).toBe(404)
}) })
}) })
......
...@@ -3,5 +3,3 @@ ...@@ -3,5 +3,3 @@
export default function foo() { export default function foo() {
return 'foo.client' return 'foo.client'
} }
export const config = 'this is not page config'
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