Skip to content

Commit

Permalink
fix: create cache entries for fallback pages to support next@canary
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Oct 8, 2024
1 parent 0b74e13 commit b2e11d0
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/build/content/prerendered.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ const routeToFilePath = (path: string) => (path === '/' ? '/index' : path)
const buildPagesCacheValue = async (
path: string,
shouldUseEnumKind: boolean,
shouldSkipJson = false,
): Promise<NetlifyCachedPageValue> => ({
kind: shouldUseEnumKind ? 'PAGES' : 'PAGE',
html: await readFile(`${path}.html`, 'utf-8'),
pageData: JSON.parse(await readFile(`${path}.json`, 'utf-8')),
pageData: shouldSkipJson ? {} : JSON.parse(await readFile(`${path}.json`, 'utf-8')),
headers: undefined,
status: undefined,
})
Expand Down Expand Up @@ -146,8 +147,8 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void>
})
: false

await Promise.all(
Object.entries(manifest.routes).map(
await Promise.all([
...Object.entries(manifest.routes).map(
([route, meta]): Promise<void> =>
limitConcurrentPrerenderContentHandling(async () => {
const lastModified = meta.initialRevalidateSeconds
Expand Down Expand Up @@ -195,7 +196,35 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void>
await writeCacheEntry(key, value, lastModified, ctx)
}),
),
)
...Object.entries(manifest.dynamicRoutes).map(async ([route, meta]) => {
// fallback can be `string | false | null`
// - `string` - when user use pages router with `fallback: true`, and then it's html file path
// - `null` - when user use pages router with `fallback: 'block'` or app router with `export const dynamicParams = true`
// - `false` - when user use pages router with `fallback: false` or app router with `export const dynamicParams = false`
if (typeof meta.fallback === 'string') {
// https://github.com/vercel/next.js/pull/68603 started using route cache to serve fallbacks
// so we have to seed blobs with fallback entries

// create cache entry for pages router with `fallback: true` case
await limitConcurrentPrerenderContentHandling(async () => {
const lastModified = Date.now()
const key = routeToFilePath(route)

const value = await buildPagesCacheValue(
join(ctx.publishDir, 'server/pages', key),
shouldUseEnumKind,
true, // there is no corresponding json file for fallback, so we are skipping it for this entry
)
// Netlify Forms are not support and require a workaround
if (value.kind === 'PAGE' || value.kind === 'PAGES' || value.kind === 'APP_PAGE') {
verifyNetlifyForms(ctx, value.html)
}

await writeCacheEntry(key, value, lastModified, ctx)
})
}
}),
])

// app router 404 pages are not in the prerender manifest
// so we need to check for them manually
Expand Down

0 comments on commit b2e11d0

Please sign in to comment.