Unverified Commit bad909e4 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Update page config APIs (#41580)

This PR updates app configurations from `export const config = { ... }`
to be directly exported.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a 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 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: 's avatarJJ Kasper <jj@jjsweb.site>
parent d261e7cd
...@@ -48,11 +48,30 @@ export function getRSCModuleType(source: string): RSCModuleType { ...@@ -48,11 +48,30 @@ export function getRSCModuleType(source: string): RSCModuleType {
* requires a runtime to be specified. Those are: * requires a runtime to be specified. Those are:
* - Modules with `export function getStaticProps | getServerSideProps` * - Modules with `export function getStaticProps | getServerSideProps`
* - Modules with `export { getStaticProps | getServerSideProps } <from ...>` * - Modules with `export { getStaticProps | getServerSideProps } <from ...>`
* - Modules with `export const runtime = ...`
*/ */
export function checkExports(swcAST: any): { ssr: boolean; ssg: boolean } { function checkExports(swcAST: any): {
ssr: boolean
ssg: boolean
runtime?: string
} {
if (Array.isArray(swcAST?.body)) { if (Array.isArray(swcAST?.body)) {
try { try {
let runtime: string | undefined
let ssr: boolean = false
let ssg: boolean = false
for (const node of swcAST.body) { for (const node of swcAST.body) {
if (
node.type === 'ExportDeclaration' &&
node.declaration?.type === 'VariableDeclaration'
) {
const id = node.declaration?.declarations[0]?.id.value
if (id === 'runtime') {
runtime = node.declaration?.declarations[0]?.init.value
}
}
if ( if (
node.type === 'ExportDeclaration' && node.type === 'ExportDeclaration' &&
node.declaration?.type === 'FunctionDeclaration' && node.declaration?.type === 'FunctionDeclaration' &&
...@@ -60,10 +79,8 @@ export function checkExports(swcAST: any): { ssr: boolean; ssg: boolean } { ...@@ -60,10 +79,8 @@ export function checkExports(swcAST: any): { ssr: boolean; ssg: boolean } {
node.declaration.identifier?.value node.declaration.identifier?.value
) )
) { ) {
return { ssg = node.declaration.identifier.value === 'getStaticProps'
ssg: node.declaration.identifier.value === 'getStaticProps', ssr = node.declaration.identifier.value === 'getServerSideProps'
ssr: node.declaration.identifier.value === 'getServerSideProps',
}
} }
if ( if (
...@@ -72,10 +89,8 @@ export function checkExports(swcAST: any): { ssr: boolean; ssg: boolean } { ...@@ -72,10 +89,8 @@ export function checkExports(swcAST: any): { ssr: boolean; ssg: boolean } {
) { ) {
const id = node.declaration?.declarations[0]?.id.value const id = node.declaration?.declarations[0]?.id.value
if (['getStaticProps', 'getServerSideProps'].includes(id)) { if (['getStaticProps', 'getServerSideProps'].includes(id)) {
return { ssg = id === 'getStaticProps'
ssg: id === 'getStaticProps', ssr = id === 'getServerSideProps'
ssr: id === 'getServerSideProps',
}
} }
} }
...@@ -87,16 +102,14 @@ export function checkExports(swcAST: any): { ssr: boolean; ssg: boolean } { ...@@ -87,16 +102,14 @@ export function checkExports(swcAST: any): { ssr: boolean; ssg: boolean } {
specifier.orig?.value specifier.orig?.value
) )
return { ssg = values.some((value: any) => ['getStaticProps'].includes(value))
ssg: values.some((value: any) => ssr = values.some((value: any) =>
['getStaticProps'].includes(value) ['getServerSideProps'].includes(value)
), )
ssr: values.some((value: any) =>
['getServerSideProps'].includes(value)
),
}
} }
} }
return { ssr, ssg, runtime }
} catch (err) {} } catch (err) {}
} }
...@@ -270,7 +283,7 @@ export async function getPageStaticInfo(params: { ...@@ -270,7 +283,7 @@ export async function getPageStaticInfo(params: {
) )
) { ) {
const swcAST = await parseModule(pageFilePath, fileContent) const swcAST = await parseModule(pageFilePath, fileContent)
const { ssg, ssr } = checkExports(swcAST) const { ssg, ssr, runtime } = checkExports(swcAST)
const rsc = getRSCModuleType(fileContent) const rsc = getRSCModuleType(fileContent)
// default / failsafe value for config // default / failsafe value for config
...@@ -284,12 +297,18 @@ export async function getPageStaticInfo(params: { ...@@ -284,12 +297,18 @@ 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
} }
// Currently, we use `export const config = { runtime: '...' }` to specify the page runtime.
// But 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
// as the fallback value.
let resolvedRuntime = runtime || config.runtime
if ( if (
typeof config.runtime !== 'undefined' && typeof resolvedRuntime !== 'undefined' &&
!isServerRuntime(config.runtime) !isServerRuntime(resolvedRuntime)
) { ) {
const options = Object.values(SERVER_RUNTIME).join(', ') const options = Object.values(SERVER_RUNTIME).join(', ')
if (typeof config.runtime !== 'string') { if (typeof resolvedRuntime !== 'string') {
Log.error( Log.error(
`The \`runtime\` config must be a string. Please leave it empty or choose one of: ${options}` `The \`runtime\` config must be a string. Please leave it empty or choose one of: ${options}`
) )
...@@ -303,14 +322,14 @@ export async function getPageStaticInfo(params: { ...@@ -303,14 +322,14 @@ export async function getPageStaticInfo(params: {
} }
} }
let runtime = resolvedRuntime =
SERVER_RUNTIME.edge === config?.runtime SERVER_RUNTIME.edge === resolvedRuntime
? SERVER_RUNTIME.edge ? SERVER_RUNTIME.edge
: ssr || ssg : ssr || ssg
? config?.runtime || nextConfig.experimental?.runtime ? resolvedRuntime || nextConfig.experimental?.runtime
: undefined : undefined
if (runtime === SERVER_RUNTIME.edge) { if (resolvedRuntime === SERVER_RUNTIME.edge) {
warnAboutExperimentalEdgeApiFunctions() warnAboutExperimentalEdgeApiFunctions()
} }
...@@ -325,7 +344,7 @@ export async function getPageStaticInfo(params: { ...@@ -325,7 +344,7 @@ export async function getPageStaticInfo(params: {
ssg, ssg,
rsc, rsc,
...(middlewareConfig && { middleware: middlewareConfig }), ...(middlewareConfig && { middleware: middlewareConfig }),
...(runtime && { runtime }), ...(resolvedRuntime && { runtime: resolvedRuntime }),
} }
} }
......
...@@ -1044,13 +1044,41 @@ export type AppConfig = { ...@@ -1044,13 +1044,41 @@ export type AppConfig = {
preferredRegion?: string preferredRegion?: string
} }
type GenerateParams = Array<{ type GenerateParams = Array<{
config: AppConfig config?: AppConfig
segmentPath: string segmentPath: string
getStaticPaths?: GetStaticPaths getStaticPaths?: GetStaticPaths
generateStaticParams?: any generateStaticParams?: any
isLayout?: boolean isLayout?: boolean
}> }>
export const collectAppConfig = (mod: any): AppConfig | undefined => {
let hasConfig = false
const config: AppConfig = {}
if (typeof mod?.revalidate !== 'undefined') {
config.revalidate = mod.revalidate
hasConfig = true
}
if (typeof mod?.dynamicParams !== 'undefined') {
config.dynamicParams = mod.dynamicParams
hasConfig = true
}
if (typeof mod?.dynamic !== 'undefined') {
config.dynamic = mod.dynamic
hasConfig = true
}
if (typeof mod?.fetchCache !== 'undefined') {
config.fetchCache = mod.fetchCache
hasConfig = true
}
if (typeof mod?.preferredRegion !== 'undefined') {
config.preferredRegion = mod.preferredRegion
hasConfig = true
}
return hasConfig ? config : undefined
}
export const collectGenerateParams = async ( export const collectGenerateParams = async (
segment: any, segment: any,
parentSegments: string[] = [], parentSegments: string[] = [],
...@@ -1059,13 +1087,14 @@ export const collectGenerateParams = async ( ...@@ -1059,13 +1087,14 @@ export const collectGenerateParams = async (
if (!Array.isArray(segment)) return generateParams if (!Array.isArray(segment)) return generateParams
const isLayout = !!segment[2]?.layout const isLayout = !!segment[2]?.layout
const mod = await (isLayout ? segment[2]?.layout?.() : segment[2]?.page?.()) const mod = await (isLayout ? segment[2]?.layout?.() : segment[2]?.page?.())
const config = collectAppConfig(mod)
const result = { const result = {
isLayout, isLayout,
segmentPath: `/${parentSegments.join('/')}${ segmentPath: `/${parentSegments.join('/')}${
segment[0] && parentSegments.length > 0 ? '/' : '' segment[0] && parentSegments.length > 0 ? '/' : ''
}${segment[0]}`, }${segment[0]}`,
config: mod?.config, config,
getStaticPaths: mod?.getStaticPaths, getStaticPaths: mod?.getStaticPaths,
generateStaticParams: mod?.generateStaticParams, generateStaticParams: mod?.generateStaticParams,
} }
......
...@@ -31,17 +31,14 @@ interface IEntry { ...@@ -31,17 +31,14 @@ interface IEntry {
? `default: (props: { children: React.ReactNode; params?: any }) => React.ReactNode | null` ? `default: (props: { children: React.ReactNode; params?: any }) => React.ReactNode | null`
: `default: (props: { params?: any }) => React.ReactNode | null` : `default: (props: { params?: any }) => React.ReactNode | null`
} }
config?: {}
generateStaticParams?: (params?:any) => Promise<any[]> generateStaticParams?: (params?:any) => Promise<any[]>
config?: {
// TODO: remove revalidate here
revalidate?: number | boolean
${options.type === 'page' ? 'runtime?: string' : ''}
}
revalidate?: RevalidateRange<TEntry> | false revalidate?: RevalidateRange<TEntry> | false
dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static' dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static'
dynamicParams?: boolean dynamicParams?: boolean
fetchCache?: 'auto' | 'force-no-store' | 'only-no-store' | 'default-no-store' | 'default-cache' | 'only-cache' | 'force-cache' fetchCache?: 'auto' | 'force-no-store' | 'only-no-store' | 'default-no-store' | 'default-cache' | 'only-cache' | 'force-cache'
preferredRegion?: 'auto' | 'home' | 'edge' preferredRegion?: 'auto' | 'home' | 'edge'
${options.type === 'page' ? "runtime?: 'nodejs' | 'experimental-edge'" : ''}
} }
// ============= // =============
......
...@@ -947,8 +947,8 @@ export async function renderToHTMLOrFlight( ...@@ -947,8 +947,8 @@ export async function renderToHTMLOrFlight(
? await page() ? await page()
: undefined : undefined
if (layoutOrPageMod?.config) { if (typeof layoutOrPageMod?.revalidate !== 'undefined') {
defaultRevalidate = layoutOrPageMod.config.revalidate defaultRevalidate = layoutOrPageMod.revalidate
if (isStaticGeneration && defaultRevalidate === 0) { if (isStaticGeneration && defaultRevalidate === 0) {
const { DynamicServerError } = const { DynamicServerError } =
......
export default function Page() { export default function Page() {
return <p>app-edge-ssr</p> return <p>app-edge-ssr</p>
} }
export const config = { runtime: 'experimental-edge' } export const runtime = 'experimental-edge'
import { experimental_use as use } from 'react' import { experimental_use as use } from 'react'
export const config = { export const revalidate = 0
revalidate: 0,
}
async function getData() { async function getData() {
await new Promise((resolve) => setTimeout(resolve, 3000)) await new Promise((resolve) => setTimeout(resolve, 3000))
......
import { experimental_use as use } from 'react' import { experimental_use as use } from 'react'
export const config = { export const revalidate = 1
revalidate: 1,
}
async function getData() { async function getData() {
return { return {
......
import { experimental_use as use } from 'react' import { experimental_use as use } from 'react'
export const config = { export const revalidate = 0
revalidate: 0,
}
async function getData() { async function getData() {
return { return {
......
import { experimental_use as use } from 'react' import { experimental_use as use } from 'react'
export const config = { export const revalidate = false
revalidate: false,
}
async function getData() { async function getData() {
return { return {
......
import { experimental_use as use } from 'react' import { experimental_use as use } from 'react'
export const config = { export const revalidate = false
revalidate: false,
}
async function getData() { async function getData() {
await new Promise((resolve) => setTimeout(resolve, 5000)) await new Promise((resolve) => setTimeout(resolve, 5000))
......
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Page() { export default function Page() {
return <p>new root ssr</p> return <p>new root ssr</p>
......
export const config = { export const dynamicParams = true
dynamicParams: true,
}
export default function Page({ params }) { export default function Page({ params }) {
return ( return (
......
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 (
......
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Page({ params }) { export default function Page({ params }) {
return ( return (
......
...@@ -20,6 +20,4 @@ export default function Page() { ...@@ -20,6 +20,4 @@ export default function Page() {
} }
// TODO-APP: remove revalidate config once next.revalidate is supported // TODO-APP: remove revalidate config once next.revalidate is supported
export const config = { export const revalidate = 0
revalidate: 0,
}
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Page() { export default function Page() {
return ( return (
......
/* eslint-disable */ /* eslint-disable */
export const config = {
revalidate: 0,
}
export const revalidate = -1 export const revalidate = -1
export default function Root({ children }) { export default function Root({ children }) {
......
...@@ -6,6 +6,4 @@ export default function HelloPage(props) { ...@@ -6,6 +6,4 @@ export default function HelloPage(props) {
) )
} }
export const config = { export const runtime = 'experimental-edge'
runtime: 'experimental-edge',
}
...@@ -13,6 +13,4 @@ export default function DashboardPage(props) { ...@@ -13,6 +13,4 @@ export default function DashboardPage(props) {
) )
} }
export const config = { export const runtime = 'experimental-edge'
runtime: 'experimental-edge',
}
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Page() { export default function Page() {
const err = new Error('this is a test') const err = new Error('this is a test')
......
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Page() { export default function Page() {
throw new Error('this is a test') throw new Error('this is a test')
......
...@@ -3,9 +3,7 @@ import { experimental_use as use } from 'react' ...@@ -3,9 +3,7 @@ import { experimental_use as use } from 'react'
import '../styles/global.css' import '../styles/global.css'
import './style.css' import './style.css'
export const config = { export const revalidate = 0
revalidate: 0,
}
async function getData() { async function getData() {
return { return {
......
...@@ -13,6 +13,4 @@ export default function SlowPage(props) { ...@@ -13,6 +13,4 @@ export default function SlowPage(props) {
return <h1 id="slow-page-message">{data.message}</h1> return <h1 id="slow-page-message">{data.message}</h1>
} }
export const config = { export const runtime = 'experimental-edge'
runtime: 'experimental-edge',
}
...@@ -13,6 +13,4 @@ export default function SlowPage(props) { ...@@ -13,6 +13,4 @@ export default function SlowPage(props) {
return <h1 id="slow-page-message">{data.message}</h1> return <h1 id="slow-page-message">{data.message}</h1>
} }
export const config = { export const runtime = 'experimental-edge'
runtime: 'experimental-edge',
}
...@@ -13,4 +13,4 @@ export default function HomePage() { ...@@ -13,4 +13,4 @@ export default function HomePage() {
) )
} }
export const config = { runtime: 'experimental-edge' } export const runtime = 'experimental-edge'
// TODO-APP: remove after fixing filtering static flight data // TODO-APP: remove after fixing filtering static flight data
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Root({ children }) { export default function Root({ children }) {
return ( return (
......
// TODO-APP: remove after fixing filtering static flight data // TODO-APP: remove after fixing filtering static flight data
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Root({ children }) { export default function Root({ children }) {
return ( return (
......
// TODO-APP: remove after fixing filtering static flight data // TODO-APP: remove after fixing filtering static flight data
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Root({ children }) { export default function Root({ children }) {
return ( return (
......
// TODO-APP: remove after fixing filtering static flight data // TODO-APP: remove after fixing filtering static flight data
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Root({ children }) { export default function Root({ children }) {
return ( return (
......
// TODO-APP: remove after fixing filtering static flight data // TODO-APP: remove after fixing filtering static flight data
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Root({ one, two }) { export default function Root({ one, two }) {
return ( return (
......
// TODO-APP: remove after fixing filtering static flight data // TODO-APP: remove after fixing filtering static flight data
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Root({ children }) { export default function Root({ children }) {
return ( return (
......
// TODO-APP: remove after fixing filtering static flight data // TODO-APP: remove after fixing filtering static flight data
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function Root({ children }) { export default function Root({ children }) {
return children return children
......
export const config = { export const dynamicParams = false
dynamicParams: false,
}
export default function Page({ params }) { export default function Page({ params }) {
return <p>Static page</p> return <p>Static page</p>
......
...@@ -2,6 +2,4 @@ export default function page() { ...@@ -2,6 +2,4 @@ export default function page() {
return 'dynamic route [id] page' return 'dynamic route [id] page'
} }
export const config = { export const runtime = 'experimental-edge'
runtime: 'experimental-edge',
}
...@@ -2,6 +2,4 @@ export default function page() { ...@@ -2,6 +2,4 @@ export default function page() {
return 'dynamic route index page' return 'dynamic route index page'
} }
export const config = { export const runtime = 'experimental-edge'
runtime: 'experimental-edge',
}
import React from 'react' import React from 'react'
import RootStyleRegistry from './root-style-registry' import RootStyleRegistry from './root-style-registry'
export const config = { export const revalidate = 0
revalidate: 0,
}
export default function AppLayout({ children }) { export default function AppLayout({ children }) {
return ( return (
......
...@@ -11,6 +11,4 @@ export default function Page() { ...@@ -11,6 +11,4 @@ export default function Page() {
) )
} }
export const config = { export const runtime = 'nodejs'
runtime: 'nodejs',
}
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