Skip to content

Commit d0fda7e

Browse files
committed
fix: create cache entries for fallback pages to support next@canary
1 parent eb6c2a1 commit d0fda7e

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/build/content/prerendered.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ const routeToFilePath = (path: string) => (path === '/' ? '/index' : path)
4848
const buildPagesCacheValue = async (
4949
path: string,
5050
shouldUseEnumKind: boolean,
51+
shouldSkipJson = false,
5152
): Promise<NetlifyCachedPageValue> => ({
5253
kind: shouldUseEnumKind ? 'PAGES' : 'PAGE',
5354
html: await readFile(`${path}.html`, 'utf-8'),
54-
pageData: JSON.parse(await readFile(`${path}.json`, 'utf-8')),
55+
pageData: shouldSkipJson ? {} : JSON.parse(await readFile(`${path}.json`, 'utf-8')),
5556
headers: undefined,
5657
status: undefined,
5758
})
@@ -146,8 +147,8 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void>
146147
})
147148
: false
148149

149-
await Promise.all(
150-
Object.entries(manifest.routes).map(
150+
await Promise.all([
151+
...Object.entries(manifest.routes).map(
151152
([route, meta]): Promise<void> =>
152153
limitConcurrentPrerenderContentHandling(async () => {
153154
const lastModified = meta.initialRevalidateSeconds
@@ -195,7 +196,35 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void>
195196
await writeCacheEntry(key, value, lastModified, ctx)
196197
}),
197198
),
198-
)
199+
...Object.entries(manifest.dynamicRoutes).map(async ([route, meta]) => {
200+
// fallback can be `string | false | null`
201+
// - `string` - when user use pages router with `fallback: true`, and then it's html file path
202+
// - `null` - when user use pages router with `fallback: 'block'` or app router with `export const dynamicParams = true`
203+
// - `false` - when user use pages router with `fallback: false` or app router with `export const dynamicParams = false`
204+
if (typeof meta.fallback === 'string') {
205+
// https://github.com/vercel/next.js/pull/68603 started using route cache to serve fallbacks
206+
// so we have to seed blobs with fallback entries
207+
208+
// create cache entry for pages router with `fallback: true` case
209+
await limitConcurrentPrerenderContentHandling(async () => {
210+
const lastModified = Date.now()
211+
const key = routeToFilePath(route)
212+
213+
const value = await buildPagesCacheValue(
214+
join(ctx.publishDir, 'server/pages', key),
215+
shouldUseEnumKind,
216+
true, // there is no corresponding json file for fallback, so we are skipping it for this entry
217+
)
218+
// Netlify Forms are not support and require a workaround
219+
if (value.kind === 'PAGE' || value.kind === 'PAGES' || value.kind === 'APP_PAGE') {
220+
verifyNetlifyForms(ctx, value.html)
221+
}
222+
223+
await writeCacheEntry(key, value, lastModified, ctx)
224+
})
225+
}
226+
}),
227+
])
199228

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

0 commit comments

Comments
 (0)