Unverified Commit 80f2293a authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Revalidate generateStaticParams/Paths in background in dev (#46940)

Follow-up to https://github.com/vercel/next.js/pull/46906 this ensures
we revalidate `generateStaticParams`/`getStaticPaths` in the background
in development so that we aren't blocking refreshes an much
un-necessarily if the paths cache is already populated.

Fixes: https://github.com/vercel/next.js/issues/44646
Fixes: https://github.com/vercel/next.js/issues/38757
x-ref:
https://github.com/vercel/next.js/discussions/17977#discussioncomment-142104
x-ref: https://github.com/vercel/next.js/discussions/20076
x-ref: https://github.com/vercel/next.js/discussions/14378
parent de6a1e3d
...@@ -98,6 +98,7 @@ import { NextBuildContext } from '../../build/build-context' ...@@ -98,6 +98,7 @@ 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' import { IncrementalCache } from '../lib/incremental-cache'
import LRUCache from 'next/dist/compiled/lru-cache'
// Load ReactDevOverlay only when needed // Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: FunctionComponent let ReactDevOverlayImpl: FunctionComponent
...@@ -133,6 +134,10 @@ export default class DevServer extends Server { ...@@ -133,6 +134,10 @@ export default class DevServer extends Server {
private verifyingTypeScript?: boolean private verifyingTypeScript?: boolean
private usingTypeScript?: boolean private usingTypeScript?: boolean
private originalFetch?: typeof fetch private originalFetch?: typeof fetch
private staticPathsCache: LRUCache<
string,
UnwrapPromise<ReturnType<DevServer['getStaticPaths']>>
>
protected staticPathsWorker?: { [key: string]: any } & { protected staticPathsWorker?: { [key: string]: any } & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
...@@ -187,6 +192,13 @@ export default class DevServer extends Server { ...@@ -187,6 +192,13 @@ export default class DevServer extends Server {
this.devReady = new Promise((resolve) => { this.devReady = new Promise((resolve) => {
this.setDevReady = resolve this.setDevReady = resolve
}) })
this.staticPathsCache = new LRUCache({
// 5MB
max: 5 * 1024 * 1024,
length(value) {
return JSON.stringify(value.staticPaths).length
},
})
;(this.renderOpts as any).ampSkipValidation = ;(this.renderOpts as any).ampSkipValidation =
this.nextConfig.experimental?.amp?.skipValidation ?? false this.nextConfig.experimental?.amp?.skipValidation ?? false
;(this.renderOpts as any).ampValidator = ( ;(this.renderOpts as any).ampValidator = (
...@@ -1557,10 +1569,14 @@ export default class DevServer extends Server { ...@@ -1557,10 +1569,14 @@ export default class DevServer extends Server {
}) })
return pathsResult return pathsResult
} }
const { paths: staticPaths, fallback } = ( const result = this.staticPathsCache.get(pathname)
await withCoalescedInvoke(__getStaticPaths)(`staticPaths-${pathname}`, [])
).value
const nextInvoke = withCoalescedInvoke(__getStaticPaths)(
`staticPaths-${pathname}`,
[]
)
.then((res) => {
const { paths: staticPaths = [], fallback } = res.value
if (this.nextConfig.output === 'export') { if (this.nextConfig.output === 'export') {
if (fallback === 'blocking') { if (fallback === 'blocking') {
throw new Error( throw new Error(
...@@ -1572,8 +1588,10 @@ export default class DevServer extends Server { ...@@ -1572,8 +1588,10 @@ export default class DevServer extends Server {
) )
} }
} }
const value: {
return { staticPaths: string[]
fallbackMode: 'blocking' | 'static' | false | undefined
} = {
staticPaths, staticPaths,
fallbackMode: fallbackMode:
fallback === 'blocking' fallback === 'blocking'
...@@ -1582,6 +1600,20 @@ export default class DevServer extends Server { ...@@ -1582,6 +1600,20 @@ export default class DevServer extends Server {
? 'static' ? 'static'
: fallback, : fallback,
} }
this.staticPathsCache.set(pathname, value)
return value
})
.catch((err) => {
this.staticPathsCache.del(pathname)
if (!result) throw err
Log.error(`Failed to generate static paths for ${pathname}:`)
console.error(err)
})
if (result) {
return result
}
return nextInvoke as NonNullable<typeof result>
} }
private persistPatchedGlobals(): void { private persistPatchedGlobals(): void {
......
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