mirror of
https://github.com/aelve/guide.git
synced 2024-12-23 12:52:31 +03:00
37 lines
1006 B
TypeScript
37 lines
1006 B
TypeScript
import path from 'path'
|
|
import fs from 'fs'
|
|
import { createBundleRenderer } from 'vue-server-renderer'
|
|
// TODO move 'src' prefix to config
|
|
// TODO find a way to get rid of ts ignore and still build without errors
|
|
// @ts-ignore
|
|
import bundle from '../src/vue-ssr-server-bundle.json'
|
|
// @ts-ignore
|
|
import clientManifest from '../src/vue-ssr-client-manifest.json'
|
|
import serve from 'koa-static'
|
|
import koaMount from 'koa-mount'
|
|
|
|
const fsAsync = fs.promises
|
|
export default async function setupProdServer (app) {
|
|
|
|
const template = await fsAsync.readFile(path.resolve(__dirname, '../src/index.html'), 'utf-8') as string
|
|
|
|
const renderer = createBundleRenderer(bundle, {
|
|
template,
|
|
clientManifest,
|
|
runInNewContext: false
|
|
})
|
|
|
|
async function handler (ctx) {
|
|
const { url } = ctx
|
|
|
|
try {
|
|
ctx.body = await renderer.renderToString({ url })
|
|
} catch (error) {
|
|
ctx.body = error
|
|
}
|
|
}
|
|
|
|
app.use(koaMount('/src', serve(path.resolve(__dirname, '../src/'))))
|
|
app.use(handler)
|
|
}
|