[READ-ONLY] a fast, modern browser for the npm registry

fix: provide payload fallback json (#1186)

authored by

Daniel Roe and committed by
GitHub
3ea47eb2 f8ab6cb9

+28 -17
+4 -6
modules/isr-fallback.ts
··· 13 13 } 14 14 15 15 nuxt.hook('nitro:init', nitro => { 16 + const htmlFallback = 'spa.prerender-fallback.html' 17 + const jsonFallback = 'payload-fallback.json' 16 18 nitro.hooks.hook('compiled', () => { 17 19 const spaTemplate = readFileSync(nitro.options.output.publicDir + '/200.html', 'utf-8') 18 20 for (const path of [ ··· 26 28 'package/[org]/[name]/v/[version]', 27 29 '', 28 30 ]) { 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) 35 32 mkdirSync(resolve(nitro.options.output.serverDir, '..', path), { recursive: true }) 36 33 writeFileSync(outputPath, spaTemplate) 34 + writeFileSync(outputPath.replace(htmlFallback, jsonFallback), '{}') 37 35 } 38 36 }) 39 37 })
+24 -11
nuxt.config.ts
··· 108 108 '/api/registry/package-meta/**': { isr: 300 }, 109 109 '/:pkg/.well-known/skills/**': { isr: 3600 }, 110 110 '/:scope/:pkg/.well-known/skills/**': { isr: 3600 }, 111 - '/__og-image__/**': { isr: getISRConfig(60) }, 111 + '/__og-image__/**': getISRConfig(60), 112 112 '/_avatar/**': { isr: 3600, proxy: 'https://www.gravatar.com/avatar/**' }, 113 113 '/opensearch.xml': { isr: true }, 114 114 '/oauth-client-metadata.json': { prerender: true }, ··· 123 123 }, 124 124 }, 125 125 // 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' }), 130 134 // infinite cache (versioned - doesn't change) 131 135 '/package-code/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } }, 132 136 '/package-docs/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } }, ··· 314 318 }, 315 319 }) 316 320 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) { 319 326 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 + } 323 334 } 324 335 return { 325 - expiration: expirationSeconds, 336 + isr: { 337 + expiration: expirationSeconds, 338 + }, 326 339 } 327 340 }