forked from
npmx.dev/npmx.dev
[READ-ONLY]
a fast, modern browser for the npm registry
1import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
2import { resolve } from 'node:path'
3import { defineNuxtModule } from 'nuxt/kit'
4import { provider } from 'std-env'
5
6export 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 const htmlFallback = 'spa.prerender-fallback.html'
17 const jsonFallback = 'payload-fallback.json'
18 nitro.hooks.hook('compiled', () => {
19 const spaTemplate = readFileSync(nitro.options.output.publicDir + '/200.html', 'utf-8')
20 for (const path of [
21 'package',
22 'package/[name]',
23 'package/[name]/v',
24 'package/[name]/v/[version]',
25 'package/[org]',
26 'package/[org]/[name]',
27 'package/[org]/[name]/v',
28 'package/[org]/[name]/v/[version]',
29 '',
30 ]) {
31 const outputPath = resolve(nitro.options.output.serverDir, '..', path, htmlFallback)
32 mkdirSync(resolve(nitro.options.output.serverDir, '..', path), { recursive: true })
33 writeFileSync(outputPath, spaTemplate)
34 writeFileSync(outputPath.replace(htmlFallback, jsonFallback), '{}')
35 }
36 })
37 })
38 },
39})