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'
import { logAppDirError } from './log-app-dir-error'
import { createClientRouterFilter } from '../../lib/create-client-router-filter'
import { IncrementalCache } from '../lib/incremental-cache'
import LRUCache from 'next/dist/compiled/lru-cache'
// Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: FunctionComponent
......@@ -133,6 +134,10 @@ export default class DevServer extends Server {
private verifyingTypeScript?: boolean
private usingTypeScript?: boolean
private originalFetch?: typeof fetch
private staticPathsCache: LRUCache<
string,
UnwrapPromise<ReturnType<DevServer['getStaticPaths']>>
>
protected staticPathsWorker?: { [key: string]: any } & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
......@@ -187,6 +192,13 @@ export default class DevServer extends Server {
this.devReady = new Promise((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.nextConfig.experimental?.amp?.skipValidation ?? false
;(this.renderOpts as any).ampValidator = (
......@@ -1557,31 +1569,51 @@ export default class DevServer extends Server {
})
return pathsResult
}
const { paths: staticPaths, fallback } = (
await withCoalescedInvoke(__getStaticPaths)(`staticPaths-${pathname}`, [])
).value
if (this.nextConfig.output === 'export') {
if (fallback === 'blocking') {
throw new Error(
'getStaticPaths with "fallback: blocking" cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export'
)
} else if (fallback === true) {
throw new Error(
'getStaticPaths with "fallback: true" cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export'
)
}
}
const result = this.staticPathsCache.get(pathname)
return {
staticPaths,
fallbackMode:
fallback === 'blocking'
? 'blocking'
: fallback === true
? 'static'
: fallback,
const nextInvoke = withCoalescedInvoke(__getStaticPaths)(
`staticPaths-${pathname}`,
[]
)
.then((res) => {
const { paths: staticPaths = [], fallback } = res.value
if (this.nextConfig.output === 'export') {
if (fallback === 'blocking') {
throw new Error(
'getStaticPaths with "fallback: blocking" cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export'
)
} else if (fallback === true) {
throw new Error(
'getStaticPaths with "fallback: true" cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export'
)
}
}
const value: {
staticPaths: string[]
fallbackMode: 'blocking' | 'static' | false | undefined
} = {
staticPaths,
fallbackMode:
fallback === 'blocking'
? 'blocking'
: fallback === true
? 'static'
: 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 {
......
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