Skip to content

Commit 6f719c3

Browse files
committed
fix: provide payload fallback json
1 parent 8edb91b commit 6f719c3

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

modules/isr-fallback.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export default defineNuxtModule({
1313
}
1414

1515
nuxt.hook('nitro:init', nitro => {
16+
const htmlFallback = 'spa.prerender-fallback.html'
17+
const jsonFallback = 'payload-fallback.json'
1618
nitro.hooks.hook('compiled', () => {
1719
const spaTemplate = readFileSync(nitro.options.output.publicDir + '/200.html', 'utf-8')
1820
for (const path of [
@@ -26,14 +28,10 @@ export default defineNuxtModule({
2628
'package/[org]/[name]/v/[version]',
2729
'',
2830
]) {
29-
const outputPath = resolve(
30-
nitro.options.output.serverDir,
31-
'..',
32-
path,
33-
'spa.prerender-fallback.html',
34-
)
31+
const outputPath = resolve(nitro.options.output.serverDir, '..', path, htmlFallback)
3532
mkdirSync(resolve(nitro.options.output.serverDir, '..', path), { recursive: true })
3633
writeFileSync(outputPath, spaTemplate)
34+
writeFileSync(outputPath.replace(htmlFallback, jsonFallback), '{}')
3735
}
3836
})
3937
})

nuxt.config.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,14 @@ export default defineNuxtConfig({
9595
},
9696
},
9797
// pages
98-
'/package/:name': { isr: getISRConfig(60, true) },
99-
'/package/:name/v/:version': { isr: getISRConfig(60, true) },
100-
'/package/:org/:name': { isr: getISRConfig(60, true) },
101-
'/package/:org/:name/v/:version': { isr: getISRConfig(60, true) },
98+
'/package/:name': { isr: getISRConfig(60, { fallback: 'html' }) },
99+
'/package/:name/_payload.json': { isr: getISRConfig(60, { fallback: 'json' }) },
100+
'/package/:name/v/:version': { isr: getISRConfig(60, { fallback: 'html' }) },
101+
'/package/:name/v/:version/_payload.json': { isr: getISRConfig(60, { fallback: 'json' }) },
102+
'/package/:org/:name': { isr: getISRConfig(60, { fallback: 'html' }) },
103+
'/package/:org/:name/_payload.json': { isr: getISRConfig(60, { fallback: 'json' }) },
104+
'/package/:org/:name/v/:version': { isr: getISRConfig(60, { fallback: 'html' }) },
105+
'/package/:org/:name/v/:version/_payload.json': { isr: getISRConfig(60, { fallback: 'json' }) },
102106
// infinite cache (versioned - doesn't change)
103107
'/package-code/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } },
104108
'/package-docs/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } },
@@ -281,11 +285,15 @@ export default defineNuxtConfig({
281285
},
282286
})
283287

284-
function getISRConfig(expirationSeconds: number, fallback = false) {
285-
if (fallback) {
288+
interface ISRConfigOptions {
289+
fallback?: 'html' | 'json'
290+
}
291+
function getISRConfig(expirationSeconds: number, options: ISRConfigOptions = {}) {
292+
if (options.fallback) {
286293
return {
287294
expiration: expirationSeconds,
288-
fallback: 'spa.prerender-fallback.html',
295+
fallback:
296+
options.fallback === 'html' ? 'spa.prerender-fallback.html' : 'payload-fallback.json',
289297
} as { expiration: number }
290298
}
291299
return {

0 commit comments

Comments
 (0)