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

perf: use spa fallback on cold cache (#878)

authored by

Daniel Roe and committed by
GitHub
75d1bf28 338b86a2

+54 -2
+7
app/plugins/fix.client.ts
··· 1 + export default defineNuxtPlugin({ 2 + enforce: 'pre', 3 + setup(nuxtApp) { 4 + // TODO: investigate why this is needed 5 + nuxtApp.payload.data ||= {} 6 + }, 7 + })
+30
modules/isr-fallback.ts
··· 1 + import { readFileSync, writeFileSync } from 'node:fs' 2 + import { resolve } from 'node:path' 3 + import { defineNuxtModule } from 'nuxt/kit' 4 + import { provider } from 'std-env' 5 + 6 + export default defineNuxtModule({ 7 + meta: { 8 + name: 'isr-fallback', 9 + }, 10 + setup(_, nuxt) { 11 + if (provider !== 'vercel') { 12 + return 13 + } 14 + 15 + nuxt.hook('nitro:init', nitro => { 16 + nitro.hooks.hook('compiled', () => { 17 + const spaTemplate = readFileSync(nitro.options.output.publicDir + '/200.html', 'utf-8') 18 + for (const path of ['package', '']) { 19 + const outputPath = resolve( 20 + nitro.options.output.serverDir, 21 + '..', 22 + path, 23 + 'spa.prerender-fallback.html', 24 + ) 25 + writeFileSync(outputPath, spaTemplate) 26 + } 27 + }) 28 + }) 29 + }, 30 + })
+17 -2
nuxt.config.ts
··· 59 59 app: { 60 60 head: { 61 61 htmlAttrs: { lang: 'en-US' }, 62 + title: 'npmx', 62 63 link: [ 63 64 { 64 65 rel: 'search', ··· 85 86 routeRules: { 86 87 '/': { prerender: true }, 87 88 '/opensearch.xml': { isr: true }, 88 - '/**': { isr: 60 }, 89 - '/package/**': { isr: 60 }, 89 + '/**': { isr: getISRConfig(60, true) }, 90 + '/api/**': { isr: 60 }, 91 + '/200.html': { prerender: true }, 92 + '/package/**': { isr: getISRConfig(60, true) }, 90 93 '/:pkg/.well-known/skills/**': { isr: 3600 }, 91 94 '/:scope/:pkg/.well-known/skills/**': { isr: 3600 }, 92 95 // never cache ··· 279 282 dirs: ['~/composables', '~/composables/*/*.ts'], 280 283 }, 281 284 }) 285 + 286 + function getISRConfig(expirationSeconds: number, fallback = false) { 287 + if (fallback) { 288 + return { 289 + expiration: expirationSeconds, 290 + fallback: 'spa.prerender-fallback.html', 291 + } as { expiration: number } 292 + } 293 + return { 294 + expiration: expirationSeconds, 295 + } 296 + }