Skip to content

Commit 3ea47eb

Browse files
authored
fix: provide payload fallback json (#1186)
1 parent f8ab6cb commit 3ea47eb

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
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: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export default defineNuxtConfig({
108108
'/api/registry/package-meta/**': { isr: 300 },
109109
'/:pkg/.well-known/skills/**': { isr: 3600 },
110110
'/:scope/:pkg/.well-known/skills/**': { isr: 3600 },
111-
'/__og-image__/**': { isr: getISRConfig(60) },
111+
'/__og-image__/**': getISRConfig(60),
112112
'/_avatar/**': { isr: 3600, proxy: 'https://www.gravatar.com/avatar/**' },
113113
'/opensearch.xml': { isr: true },
114114
'/oauth-client-metadata.json': { prerender: true },
@@ -123,10 +123,14 @@ export default defineNuxtConfig({
123123
},
124124
},
125125
// pages
126-
'/package/:name': { isr: getISRConfig(60, true) },
127-
'/package/:name/v/:version': { isr: getISRConfig(60, true) },
128-
'/package/:org/:name': { isr: getISRConfig(60, true) },
129-
'/package/:org/:name/v/:version': { isr: getISRConfig(60, true) },
126+
'/package/:name': getISRConfig(60, { fallback: 'html' }),
127+
'/package/:name/_payload.json': getISRConfig(60, { fallback: 'json' }),
128+
'/package/:name/v/:version': getISRConfig(60, { fallback: 'html' }),
129+
'/package/:name/v/:version/_payload.json': getISRConfig(60, { fallback: 'json' }),
130+
'/package/:org/:name': getISRConfig(60, { fallback: 'html' }),
131+
'/package/:org/:name/_payload.json': getISRConfig(60, { fallback: 'json' }),
132+
'/package/:org/:name/v/:version': getISRConfig(60, { fallback: 'html' }),
133+
'/package/:org/:name/v/:version/_payload.json': getISRConfig(60, { fallback: 'json' }),
130134
// infinite cache (versioned - doesn't change)
131135
'/package-code/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } },
132136
'/package-docs/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } },
@@ -314,14 +318,23 @@ export default defineNuxtConfig({
314318
},
315319
})
316320

317-
function getISRConfig(expirationSeconds: number, fallback = false) {
318-
if (fallback) {
321+
interface ISRConfigOptions {
322+
fallback?: 'html' | 'json'
323+
}
324+
function getISRConfig(expirationSeconds: number, options: ISRConfigOptions = {}) {
325+
if (options.fallback) {
319326
return {
320-
expiration: expirationSeconds,
321-
fallback: 'spa.prerender-fallback.html',
322-
} as { expiration: number }
327+
isr: {
328+
expiration: expirationSeconds,
329+
fallback:
330+
options.fallback === 'html' ? 'spa.prerender-fallback.html' : 'payload-fallback.json',
331+
initialHeaders: options.fallback === 'json' ? { 'content-type': 'application/json' } : {},
332+
} as { expiration: number },
333+
}
323334
}
324335
return {
325-
expiration: expirationSeconds,
336+
isr: {
337+
expiration: expirationSeconds,
338+
},
326339
}
327340
}

0 commit comments

Comments
 (0)