2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-02 15:54:04 +03:00
Files
vue-meta/examples/ssr/server.js
T
pimlie 9cfde5b550 feat: make ssr work
feat: update build script

chore: use jiti instead of babel-node for examples
2021-01-25 00:50:46 +01:00

48 lines
1.2 KiB
JavaScript

import path from 'path'
import fs from 'fs-extra'
import { createSSRApp } from 'vue'
import { renderToStringWithMeta } from 'vue-meta'
import template from 'lodash/template'
import { App, createRouter, metaManager } from '../vue-router/main'
const templateFile = path.resolve(__dirname, 'app.template.html')
const templateContent = fs.readFileSync(templateFile, { encoding: 'utf8' })
// see: https://lodash.com/docs#template
const compiled = template(templateContent, { interpolate: /{{([\s\S]+?)}}/g })
process.server = true
export async function renderPage ({ url }) {
console.log('renderPage', url)
const app = createSSRApp(App)
const router = createRouter('/ssr', true)
app.use(router)
app.use(metaManager)
await router.push(url.substr(4))
await router.isReady()
const [appHtml, ctx] = await renderToStringWithMeta(app)
if (!ctx.teleports) {
ctx.teleports = {}
}
const pageHtml = compiled({
app: appHtml,
htmlAttrs: ctx.teleports.htmlAttrs || '',
headAttrs: ctx.teleports.headAttrs || '',
bodyAttrs: ctx.teleports.bodyAttrs || '',
head: ctx.teleports.head || '',
bodyPrepend: ctx.teleports['body-prepend'] || '',
bodyAppend: ctx.teleports.body || ''
})
return pageHtml
}