-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathisr-fallback.ts
More file actions
39 lines (37 loc) · 1.25 KB
/
isr-fallback.ts
File metadata and controls
39 lines (37 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { defineNuxtModule } from 'nuxt/kit'
import { provider } from 'std-env'
export default defineNuxtModule({
meta: {
name: 'isr-fallback',
},
setup(_, nuxt) {
if (provider !== 'vercel') {
return
}
nuxt.hook('nitro:init', nitro => {
const htmlFallback = 'spa.prerender-fallback.html'
const jsonFallback = 'payload-fallback.json'
nitro.hooks.hook('compiled', () => {
const spaTemplate = readFileSync(nitro.options.output.publicDir + '/200.html', 'utf-8')
for (const path of [
'package',
'package/[name]',
'package/[name]/v',
'package/[name]/v/[version]',
'package/[org]',
'package/[org]/[name]',
'package/[org]/[name]/v',
'package/[org]/[name]/v/[version]',
'',
]) {
const outputPath = resolve(nitro.options.output.serverDir, '..', path, htmlFallback)
mkdirSync(resolve(nitro.options.output.serverDir, '..', path), { recursive: true })
writeFileSync(outputPath, spaTemplate)
writeFileSync(outputPath.replace(htmlFallback, jsonFallback), '{}')
}
})
})
},
})