Unverified Commit bfc3849b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update generateStaticParams handling with fetch cache (#46906)

This ensures we leverage the fetch cache when calling
`generateStaticParams` and also ensures paths with
`generateStaticParams` without `dynamicParams = false` don't error when
only partial params are provided.

x-ref: [slack
thread](https://vercel.slack.com/archives/C035J346QQL/p1678149362238089)
parent 9a1798b6
...@@ -1463,6 +1463,11 @@ export default async function build( ...@@ -1463,6 +1463,11 @@ export default async function build(
edgeInfo, edgeInfo,
pageType, pageType,
hasServerComponents: !!appDir, hasServerComponents: !!appDir,
incrementalCacheHandlerPath:
config.experimental.incrementalCacheHandlerPath,
isrFlushToDisk: config.experimental.isrFlushToDisk,
maxMemoryCacheSize:
config.experimental.isrMemoryCacheSize,
}) })
} }
) )
......
...@@ -55,6 +55,10 @@ import { ...@@ -55,6 +55,10 @@ import {
overrideBuiltInReactPackages, overrideBuiltInReactPackages,
} from './webpack/require-hook' } from './webpack/require-hook'
import { isClientReference } from './is-client-reference' import { isClientReference } from './is-client-reference'
import { StaticGenerationAsyncStorageWrapper } from '../server/async-storage/static-generation-async-storage-wrapper'
import { IncrementalCache } from '../server/lib/incremental-cache'
import { patchFetch } from '../server/lib/patch-fetch'
import { nodeFs } from '../server/lib/node-fs-methods'
loadRequireHook() loadRequireHook()
if (process.env.NEXT_PREBUNDLED_REACT) { if (process.env.NEXT_PREBUNDLED_REACT) {
...@@ -998,6 +1002,15 @@ export async function buildStaticPaths({ ...@@ -998,6 +1002,15 @@ export async function buildStaticPaths({
(repeat && !Array.isArray(paramValue)) || (repeat && !Array.isArray(paramValue)) ||
(!repeat && typeof paramValue !== 'string') (!repeat && typeof paramValue !== 'string')
) { ) {
// If from appDir and not all params were provided from
// generateStaticParams we can just filter this entry out
// as it's meant to be generated at runtime
if (appDir) {
builtPage = ''
encodedBuiltPage = ''
return
}
throw new Error( throw new Error(
`A required parameter (${validParamKey}) was not provided as ${ `A required parameter (${validParamKey}) was not provided as ${
repeat ? 'an array' : 'a string' repeat ? 'an array' : 'a string'
...@@ -1031,6 +1044,10 @@ export async function buildStaticPaths({ ...@@ -1031,6 +1044,10 @@ export async function buildStaticPaths({
.replace(/(?!^)\/$/, '') .replace(/(?!^)\/$/, '')
}) })
if (!builtPage && !encodedBuiltPage) {
return
}
if (entry.locale && !locales?.includes(entry.locale)) { if (entry.locale && !locales?.includes(entry.locale)) {
throw new Error( throw new Error(
`Invalid locale returned from getStaticPaths for ${page}, the locale ${entry.locale} is not specified in ${configFileName}` `Invalid locale returned from getStaticPaths for ${page}, the locale ${entry.locale} is not specified in ${configFileName}`
...@@ -1143,13 +1160,75 @@ export const collectGenerateParams = async ( ...@@ -1143,13 +1160,75 @@ export const collectGenerateParams = async (
export async function buildAppStaticPaths({ export async function buildAppStaticPaths({
page, page,
distDir,
configFileName, configFileName,
generateParams, generateParams,
isrFlushToDisk,
incrementalCacheHandlerPath,
requestHeaders,
maxMemoryCacheSize,
fetchCacheKeyPrefix,
staticGenerationAsyncStorage,
serverHooks,
}: { }: {
page: string page: string
configFileName: string configFileName: string
generateParams: GenerateParams generateParams: GenerateParams
incrementalCacheHandlerPath?: string
distDir: string
isrFlushToDisk?: boolean
fetchCacheKeyPrefix?: string
maxMemoryCacheSize?: number
requestHeaders: IncrementalCache['requestHeaders']
staticGenerationAsyncStorage: Parameters<
typeof patchFetch
>[0]['staticGenerationAsyncStorage']
serverHooks: Parameters<typeof patchFetch>[0]['serverHooks']
}) { }) {
patchFetch({
staticGenerationAsyncStorage,
serverHooks,
})
let CacheHandler: any
if (incrementalCacheHandlerPath) {
CacheHandler = require(incrementalCacheHandlerPath)
CacheHandler = CacheHandler.default || CacheHandler
}
const incrementalCache = new IncrementalCache({
fs: nodeFs,
dev: true,
appDir: true,
flushToDisk: isrFlushToDisk,
serverDistDir: path.join(distDir, 'server'),
fetchCacheKeyPrefix,
maxMemoryCacheSize,
getPrerenderManifest: () => ({
version: -1 as any, // letting us know this doesn't conform to spec
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: null as any, // `preview` is special case read in next-dev-server
}),
CurCacheHandler: CacheHandler,
requestHeaders,
})
const wrapper = new StaticGenerationAsyncStorageWrapper()
return wrapper.wrap(
staticGenerationAsyncStorage,
{
pathname: page,
renderOpts: {
incrementalCache,
supportsDynamicHTML: true,
isRevalidate: false,
isBot: false,
},
},
async () => {
const pageEntry = generateParams[generateParams.length - 1] const pageEntry = generateParams[generateParams.length - 1]
// if the page has legacy getStaticPaths we call it like normal // if the page has legacy getStaticPaths we call it like normal
...@@ -1200,8 +1279,10 @@ export async function buildAppStaticPaths({ ...@@ -1200,8 +1279,10 @@ export async function buildAppStaticPaths({
} }
const builtParams = await buildParams() const builtParams = await buildParams()
const fallback = !generateParams.some( const fallback = !generateParams.some(
// TODO: check complementary configs that can impact // TODO: dynamic params should be allowed
// dynamicParams behavior // to be granular per segment but we need
// additional information stored/leveraged in
// the prerender-manifest to allow this behavior
(generate) => generate.config?.dynamicParams === false (generate) => generate.config?.dynamicParams === false
) )
...@@ -1226,6 +1307,8 @@ export async function buildAppStaticPaths({ ...@@ -1226,6 +1307,8 @@ export async function buildAppStaticPaths({
appDir: true, appDir: true,
}) })
} }
}
)
} }
export async function isPageStatic({ export async function isPageStatic({
...@@ -1243,6 +1326,9 @@ export async function isPageStatic({ ...@@ -1243,6 +1326,9 @@ export async function isPageStatic({
pageType, pageType,
hasServerComponents, hasServerComponents,
originalAppPath, originalAppPath,
isrFlushToDisk,
maxMemoryCacheSize,
incrementalCacheHandlerPath,
}: { }: {
page: string page: string
distDir: string distDir: string
...@@ -1258,6 +1344,9 @@ export async function isPageStatic({ ...@@ -1258,6 +1344,9 @@ export async function isPageStatic({
pageRuntime?: ServerRuntime pageRuntime?: ServerRuntime
hasServerComponents?: boolean hasServerComponents?: boolean
originalAppPath?: string originalAppPath?: string
isrFlushToDisk?: boolean
maxMemoryCacheSize?: number
incrementalCacheHandlerPath?: string
}): Promise<{ }): Promise<{
isStatic?: boolean isStatic?: boolean
isAmpOnly?: boolean isAmpOnly?: boolean
...@@ -1335,6 +1424,9 @@ export async function isPageStatic({ ...@@ -1335,6 +1424,9 @@ export async function isPageStatic({
isClientComponent = isClientReference(componentsResult.ComponentMod) isClientComponent = isClientReference(componentsResult.ComponentMod)
const tree = componentsResult.ComponentMod.tree const tree = componentsResult.ComponentMod.tree
const handlers = componentsResult.ComponentMod.handlers const handlers = componentsResult.ComponentMod.handlers
const staticGenerationAsyncStorage =
componentsResult.ComponentMod.staticGenerationAsyncStorage
const serverHooks = componentsResult.ComponentMod.serverHooks
const generateParams: GenerateParams = handlers const generateParams: GenerateParams = handlers
? [ ? [
...@@ -1399,8 +1491,15 @@ export async function isPageStatic({ ...@@ -1399,8 +1491,15 @@ export async function isPageStatic({
encodedPaths: encodedPrerenderRoutes, encodedPaths: encodedPrerenderRoutes,
} = await buildAppStaticPaths({ } = await buildAppStaticPaths({
page, page,
serverHooks,
staticGenerationAsyncStorage,
configFileName, configFileName,
generateParams, generateParams,
distDir,
requestHeaders: {},
isrFlushToDisk,
maxMemoryCacheSize,
incrementalCacheHandlerPath,
})) }))
} }
} else { } else {
......
...@@ -49,7 +49,8 @@ export class Worker { ...@@ -49,7 +49,8 @@ export class Worker {
_child: ChildProcess _child: ChildProcess
}[]) { }[]) {
worker._child.on('exit', (code, signal) => { worker._child.on('exit', (code, signal) => {
if (code || signal) { // log unexpected exit if .end() wasn't called
if ((code || signal) && this._worker) {
console.error( console.error(
`Static worker unexpectedly exited with code: ${code} and signal: ${signal}` `Static worker unexpectedly exited with code: ${code} and signal: ${signal}`
) )
......
...@@ -1096,6 +1096,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1096,6 +1096,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
pathname, pathname,
}: { }: {
pathname: string pathname: string
requestHeaders: import('./lib/incremental-cache').IncrementalCache['requestHeaders']
originalAppPath?: string originalAppPath?: string
}): Promise<{ }): Promise<{
staticPaths?: string[] staticPaths?: string[]
...@@ -1162,6 +1163,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1162,6 +1163,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const pathsResult = await this.getStaticPaths({ const pathsResult = await this.getStaticPaths({
pathname, pathname,
originalAppPath: components.pathname, originalAppPath: components.pathname,
requestHeaders: req.headers,
}) })
staticPaths = pathsResult.staticPaths staticPaths = pathsResult.staticPaths
...@@ -1607,7 +1609,10 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1607,7 +1609,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (!staticPaths) { if (!staticPaths) {
;({ staticPaths, fallbackMode } = hasStaticPaths ;({ staticPaths, fallbackMode } = hasStaticPaths
? await this.getStaticPaths({ pathname }) ? await this.getStaticPaths({
pathname,
requestHeaders: req.headers,
})
: { staticPaths: undefined, fallbackMode: false }) : { staticPaths: undefined, fallbackMode: false })
} }
......
...@@ -97,6 +97,7 @@ import { DefaultFileReader } from '../future/route-matcher-providers/dev/helpers ...@@ -97,6 +97,7 @@ import { DefaultFileReader } from '../future/route-matcher-providers/dev/helpers
import { NextBuildContext } from '../../build/build-context' import { NextBuildContext } from '../../build/build-context'
import { logAppDirError } from './log-app-dir-error' import { logAppDirError } from './log-app-dir-error'
import { createClientRouterFilter } from '../../lib/create-client-router-filter' import { createClientRouterFilter } from '../../lib/create-client-router-filter'
import { IncrementalCache } from '../lib/incremental-cache'
// Load ReactDevOverlay only when needed // Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: FunctionComponent let ReactDevOverlayImpl: FunctionComponent
...@@ -1511,9 +1512,11 @@ export default class DevServer extends Server { ...@@ -1511,9 +1512,11 @@ export default class DevServer extends Server {
protected async getStaticPaths({ protected async getStaticPaths({
pathname, pathname,
originalAppPath, originalAppPath,
requestHeaders,
}: { }: {
pathname: string pathname: string
originalAppPath?: string originalAppPath?: string
requestHeaders: IncrementalCache['requestHeaders']
}): Promise<{ }): Promise<{
staticPaths?: string[] staticPaths?: string[]
fallbackMode?: false | 'static' | 'blocking' fallbackMode?: false | 'static' | 'blocking'
...@@ -1545,6 +1548,12 @@ export default class DevServer extends Server { ...@@ -1545,6 +1548,12 @@ export default class DevServer extends Server {
defaultLocale, defaultLocale,
originalAppPath, originalAppPath,
isAppPath: !!originalAppPath, isAppPath: !!originalAppPath,
requestHeaders,
incrementalCacheHandlerPath:
this.nextConfig.experimental.incrementalCacheHandlerPath,
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
isrFlushToDisk: this.nextConfig.experimental.isrFlushToDisk,
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize,
}) })
return pathsResult return pathsResult
} }
......
...@@ -13,6 +13,9 @@ import { ...@@ -13,6 +13,9 @@ import {
loadRequireHook, loadRequireHook,
overrideBuiltInReactPackages, overrideBuiltInReactPackages,
} from '../../build/webpack/require-hook' } from '../../build/webpack/require-hook'
import { IncrementalCache } from '../lib/incremental-cache'
import * as serverHooks from '../../client/components/hooks-server-context'
import { staticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage'
type RuntimeConfig = any type RuntimeConfig = any
...@@ -40,6 +43,11 @@ export async function loadStaticPaths({ ...@@ -40,6 +43,11 @@ export async function loadStaticPaths({
defaultLocale, defaultLocale,
isAppPath, isAppPath,
originalAppPath, originalAppPath,
isrFlushToDisk,
fetchCacheKeyPrefix,
maxMemoryCacheSize,
requestHeaders,
incrementalCacheHandlerPath,
}: { }: {
distDir: string distDir: string
pathname: string pathname: string
...@@ -50,6 +58,11 @@ export async function loadStaticPaths({ ...@@ -50,6 +58,11 @@ export async function loadStaticPaths({
defaultLocale?: string defaultLocale?: string
isAppPath?: boolean isAppPath?: boolean
originalAppPath?: string originalAppPath?: string
isrFlushToDisk?: boolean
fetchCacheKeyPrefix?: string
maxMemoryCacheSize?: number
requestHeaders: IncrementalCache['requestHeaders']
incrementalCacheHandlerPath?: string
}): Promise<{ }): Promise<{
paths?: string[] paths?: string[]
encodedPaths?: string[] encodedPaths?: string[]
...@@ -104,6 +117,14 @@ export async function loadStaticPaths({ ...@@ -104,6 +117,14 @@ export async function loadStaticPaths({
page: pathname, page: pathname,
generateParams, generateParams,
configFileName: config.configFileName, configFileName: config.configFileName,
distDir,
requestHeaders,
incrementalCacheHandlerPath,
serverHooks,
staticGenerationAsyncStorage,
isrFlushToDisk,
fetchCacheKeyPrefix,
maxMemoryCacheSize,
}) })
} }
......
import _fs from 'fs'
import { CacheFs } from '../../shared/lib/utils'
export const nodeFs: CacheFs = {
readFile: (f) => _fs.promises.readFile(f, 'utf8'),
readFileSync: (f) => _fs.readFileSync(f, 'utf8'),
writeFile: (f, d) => _fs.promises.writeFile(f, d, 'utf8'),
mkdir: (dir) => _fs.promises.mkdir(dir, { recursive: true }),
stat: (f) => _fs.promises.stat(f),
}
...@@ -99,6 +99,7 @@ import { MatchOptions } from './future/route-matcher-managers/route-matcher-mana ...@@ -99,6 +99,7 @@ import { MatchOptions } from './future/route-matcher-managers/route-matcher-mana
import { INSTRUMENTATION_HOOK_FILENAME } from '../lib/constants' import { INSTRUMENTATION_HOOK_FILENAME } from '../lib/constants'
import { getTracer } from './lib/trace/tracer' import { getTracer } from './lib/trace/tracer'
import { NextNodeServerSpan } from './lib/trace/constants' import { NextNodeServerSpan } from './lib/trace/constants'
import { nodeFs } from './lib/node-fs-methods'
export * from './base-server' export * from './base-server'
...@@ -1336,13 +1337,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1336,13 +1337,7 @@ export default class NextNodeServer extends BaseServer {
} }
protected getCacheFilesystem(): CacheFs { protected getCacheFilesystem(): CacheFs {
return { return nodeFs
readFile: (f) => fs.promises.readFile(f, 'utf8'),
readFileSync: (f) => fs.readFileSync(f, 'utf8'),
writeFile: (f, d) => fs.promises.writeFile(f, d, 'utf8'),
mkdir: (dir) => fs.promises.mkdir(dir, { recursive: true }),
stat: (f) => fs.promises.stat(f),
}
} }
private normalizeReq( private normalizeReq(
......
export const dynamicParams = true
export async function generateStaticParams() {
const res = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?staticGen'
)
const data = await res.text()
const fetchSlug = Math.round(Number(data) * 100)
console.log('partial-gen-params fetch', fetchSlug)
return [
{
slug: 'first',
},
{
slug: 'second',
},
{
slug: fetchSlug + '',
},
]
}
export default function Page({ params }) {
return (
<>
<p id="page">/partial-gen-params/[lang]/[slug]</p>
<p id="params">{JSON.stringify(params)}</p>
</>
)
}
export const dynamicParams = false
export function generateStaticParams() {
return [{ lang: 'en' }, { lang: 'fr' }]
}
export default function Layout({ children }) {
return <>{children}</>
}
export const dynamicParams = false
export async function generateStaticParams() {
const res = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?staticGen'
)
const data = await res.text()
const fetchSlug = Math.round(Number(data) * 100)
console.log('partial-gen-params fetch', fetchSlug)
return [
{
slug: 'first',
},
{
slug: 'second',
},
{
slug: fetchSlug + '',
},
]
}
export default function Page({ params }) {
return (
<>
<p id="page">/partial-gen-params/[lang]/[slug]</p>
<p id="params">{JSON.stringify(params)}</p>
</>
)
}
export const dynamicParams = true
export function generateStaticParams() {
return [{ lang: 'en' }, { lang: 'fr' }]
}
export default function Layout({ children }) {
return <>{children}</>
}
export default function Page({ params }) {
return (
<>
<p id="page">/partial-gen-params/[lang]/[slug]</p>
<p id="params">{JSON.stringify(params)}</p>
</>
)
}
export function generateStaticParams() {
return [{ lang: 'en' }, { lang: 'fr' }]
}
export default function Layout({ children }) {
return <>{children}</>
}
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