Skip to content

Commit 8ec204c

Browse files
feat(vitrify): add loadSSRAssets to fastify-ssr-plugin
1 parent bdd3d59 commit 8ec204c

File tree

4 files changed

+87
-42
lines changed

4 files changed

+87
-42
lines changed

.changeset/three-doors-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vitrify': minor
3+
---
4+
5+
feat(vitrify): add loadSSRAssets to fastify-ssr-plugin

packages/vitrify/src/node/bin/cli.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { ResolvedConfig, ViteDevServer } from 'vite'
88
import type { Server } from 'net'
99
import type { FastifyInstance } from 'fastify'
1010
import { readdir } from 'fs/promises'
11+
import { loadSSRAssets } from '../frameworks/vue/fastify-ssr-plugin.js'
1112

1213
const cli = cac('vitrify')
1314
cli
@@ -85,17 +86,23 @@ cli
8586
...args,
8687
outDir: fileURLToPath(new URL('ssr/server/', baseOutDir))
8788
})
88-
;({ prerender, onRendered } = await import(
89+
;({ prerender } = await import(
8990
new URL('ssr/server/prerender.mjs', baseOutDir).pathname
9091
))
9192

93+
const { template, manifest, render, getRoutes, onRendered } =
94+
await loadSSRAssets({
95+
mode: 'ssg',
96+
distDir: baseOutDir
97+
})
98+
const routes = await getRoutes()
99+
92100
prerender({
93101
outDir: fileURLToPath(new URL('static/', baseOutDir)),
94-
templatePath: fileURLToPath(new URL('static/index.html', baseOutDir)),
95-
manifestPath: fileURLToPath(
96-
new URL('static/.vite/ssr-manifest.json', baseOutDir)
97-
),
98-
entryServerPath: new URL('ssr/server/entry-server.mjs', baseOutDir),
102+
template,
103+
manifest,
104+
render,
105+
routes,
99106
onRendered
100107
})
101108
break

packages/vitrify/src/node/frameworks/vue/fastify-ssr-plugin.ts

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '../../helpers/utils.js'
1010
import type { ViteDevServer } from 'vite'
1111
import type { OnRenderedHook } from '../../vitrify-config.js'
12+
import { getAppDir } from '../../app-urls.js'
1213

1314
type ProvideFn = (
1415
req: FastifyRequest,
@@ -123,31 +124,9 @@ const fastifySsrPlugin: FastifyPluginAsync<FastifySsrOptions> = async (
123124
const url = req.raw.url?.replace(options.baseUrl!, '/')
124125
const provide = options.provide ? await options.provide(req, res) : {}
125126

126-
const template = readFileSync(
127-
fileURLToPath(new URL('./dist/ssr/client/index.html', options.appDir))
128-
).toString()
129-
const manifest = JSON.parse(
130-
readFileSync(
131-
new URL('./dist/ssr/client/.vite/ssr-manifest.json', options.appDir)
132-
).toString()
133-
)
134-
const render = (
135-
await import(
136-
fileURLToPath(
137-
new URL('./dist/ssr/server/entry-server.mjs', options.appDir)
138-
)
139-
)
140-
).render
141-
const onRendered = (
142-
await import(
143-
fileURLToPath(
144-
new URL(
145-
'./dist/ssr/server/virtual_vitrify-hooks.mjs',
146-
options.appDir
147-
)
148-
)
149-
)
150-
).onRendered
127+
const { template, manifest, render, onRendered } = await loadSSRAssets({
128+
distDir: new URL('./dist/', options.appDir)
129+
})
151130

152131
const html = await renderHtml({
153132
request: req,
@@ -232,5 +211,60 @@ const renderHtml = async (options: {
232211
return html
233212
}
234213

235-
export { fastifySsrPlugin, renderHtml }
214+
const loadSSRAssets = async (
215+
{
216+
mode,
217+
distDir
218+
}: {
219+
mode?: 'ssr' | 'ssg'
220+
distDir?: URL
221+
} = {
222+
mode: 'ssr'
223+
}
224+
) => {
225+
const appDir = getAppDir()
226+
const baseOutDir = distDir || new URL('dist/', appDir)
227+
228+
let templatePath, manifestPath, entryServerPath
229+
const onRenderedPath = fileURLToPath(
230+
new URL('ssr/server/virtual_vitrify-hooks.mjs', baseOutDir)
231+
)
232+
if (mode === 'ssg') {
233+
templatePath = fileURLToPath(new URL('static/index.html', baseOutDir))
234+
manifestPath = fileURLToPath(
235+
new URL('static/.vite/ssr-manifest.json', baseOutDir)
236+
)
237+
entryServerPath = fileURLToPath(
238+
new URL('ssr/server/entry-server.mjs', baseOutDir)
239+
)
240+
} else {
241+
templatePath = fileURLToPath(new URL('ssr/client/index.html', baseOutDir))
242+
manifestPath = fileURLToPath(
243+
new URL('ssr/client/.vite/ssr-manifest.json', baseOutDir)
244+
)
245+
entryServerPath = fileURLToPath(
246+
new URL('ssr/server/entry-server.mjs', baseOutDir)
247+
)
248+
}
249+
try {
250+
const template = readFileSync(templatePath).toString()
251+
const manifest = JSON.parse(readFileSync(manifestPath).toString())
252+
const entryServer = await import(entryServerPath)
253+
const { render, getRoutes } = entryServer
254+
const onRendered = (await import(onRenderedPath)).onRendered
255+
256+
return {
257+
template,
258+
manifest,
259+
render,
260+
getRoutes,
261+
onRendered
262+
}
263+
} catch (e) {
264+
console.error(e)
265+
throw new Error('Unable to load SSR asset files')
266+
}
267+
}
268+
269+
export { fastifySsrPlugin, renderHtml, loadSSRAssets }
236270
export type FastifySsrPlugin = typeof fastifySsrPlugin

packages/vitrify/src/node/frameworks/vue/prerender.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@ import { existsSync, promises as fs, mkdirSync } from 'fs'
22
import type { OnRenderedHook } from 'src/node/vitrify-config.js'
33
import { routesToPaths } from '../../helpers/routes.js'
44
import { renderHtml } from './fastify-ssr-plugin.js'
5+
import { type RouteRecordRaw } from 'vue-router'
56

67
export const prerender = async ({
78
outDir,
8-
templatePath,
9-
manifestPath,
10-
entryServerPath,
9+
template,
10+
manifest,
11+
render,
12+
routes,
1113
onRendered
1214
}: {
1315
outDir: string
14-
templatePath: string
15-
manifestPath: string
16-
entryServerPath: string
16+
template: string
17+
manifest: Record<string, unknown>
18+
render: unknown
19+
routes: RouteRecordRaw[]
1720
onRendered: OnRenderedHook[]
1821
}) => {
1922
const promises = []
20-
const template = (await fs.readFile(templatePath)).toString()
21-
const manifest = JSON.parse((await fs.readFile(manifestPath)).toString())
22-
const { render, getRoutes } = await import(entryServerPath)
23-
const routes = await getRoutes()
2423
const paths = routesToPaths(routes).filter(
2524
(i) => !i.includes(':') && !i.includes('*')
2625
)

0 commit comments

Comments
 (0)