Skip to content

Commit 480e07e

Browse files
committed
perf: use spa fallback on cold cache
1 parent 07ce0e4 commit 480e07e

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

app/plugins/fix.client.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default defineNuxtPlugin({
2+
enforce: 'pre',
3+
setup(nuxtApp) {
4+
// TODO: investigate why this is needed
5+
nuxtApp.payload.data ||= {}
6+
},
7+
})

modules/isr-fallback.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
const outputPath = resolve(
19+
nitro.options.output.serverDir,
20+
'..',
21+
'spa.prerender-fallback.html',
22+
)
23+
writeFileSync(outputPath, spaTemplate)
24+
})
25+
})
26+
},
27+
})

nuxt.config.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,20 @@ export default defineNuxtConfig({
8585
routeRules: {
8686
'/': { prerender: true },
8787
'/opensearch.xml': { isr: true },
88-
'/**': { isr: 60 },
89-
'/package/**': { isr: 60 },
88+
'/**': {
89+
isr: {
90+
expiration: 60,
91+
fallback: 'spa.prerender-fallback.html',
92+
},
93+
},
94+
'/api/**': { isr: 60 },
95+
'/200.html': { prerender: true },
96+
'/package/**': {
97+
isr: {
98+
expiration: 60,
99+
fallback: 'spa.prerender-fallback.html',
100+
},
101+
},
90102
'/:pkg/.well-known/skills/**': { isr: 3600 },
91103
'/:scope/:pkg/.well-known/skills/**': { isr: 3600 },
92104
// never cache
@@ -186,6 +198,7 @@ export default defineNuxtConfig({
186198

187199
htmlValidator: {
188200
failOnError: true,
201+
ignore: ['/200.html'],
189202
},
190203

191204
ogImage: {

0 commit comments

Comments
 (0)