2017-08-18 16:05:01 +00:00
|
|
|
|
2017-08-21 09:38:21 +00:00
|
|
|
import Vue from 'vue'
|
|
|
|
import VueMeta from 'vue-meta'
|
|
|
|
import VueServerRenderer from 'vue-server-renderer'
|
2017-08-18 16:05:01 +00:00
|
|
|
import LRU from 'lru-cache'
|
|
|
|
|
|
|
|
export default class MetaRenderer {
|
2017-08-19 09:50:45 +00:00
|
|
|
constructor (nuxt, renderer) {
|
2017-08-18 16:05:01 +00:00
|
|
|
this.nuxt = nuxt
|
2017-08-19 09:50:45 +00:00
|
|
|
this.renderer = renderer
|
2017-08-18 16:05:01 +00:00
|
|
|
this.options = nuxt.options
|
2017-08-21 09:38:21 +00:00
|
|
|
this.vueRenderer = VueServerRenderer.createRenderer()
|
2017-08-18 16:05:01 +00:00
|
|
|
this.cache = LRU({})
|
|
|
|
|
2017-08-21 09:38:21 +00:00
|
|
|
// Add VueMeta to Vue (this is only for SPA mode)
|
|
|
|
// See lib/app/index.js
|
|
|
|
Vue.use(VueMeta, {
|
|
|
|
keyName: 'head',
|
|
|
|
attribute: 'data-n-head',
|
|
|
|
ssrAttribute: 'data-n-head-ssr',
|
|
|
|
tagIDKeyName: 'hid'
|
|
|
|
})
|
|
|
|
}
|
2017-08-18 16:05:01 +00:00
|
|
|
|
2017-08-21 09:38:21 +00:00
|
|
|
getMeta(url) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const vm = new Vue({
|
|
|
|
render: (h) => h(), // Render empty html tag
|
|
|
|
head: this.options.head || {}
|
2017-08-18 16:05:01 +00:00
|
|
|
})
|
2017-08-21 09:38:21 +00:00
|
|
|
this.vueRenderer.renderToString(vm, (err) => {
|
|
|
|
if (err) return reject(err)
|
|
|
|
resolve(vm.$meta().inject())
|
2017-08-18 16:05:01 +00:00
|
|
|
})
|
2017-08-21 09:38:21 +00:00
|
|
|
})
|
|
|
|
}
|
2017-08-18 16:05:01 +00:00
|
|
|
|
2017-08-21 09:38:21 +00:00
|
|
|
async render ({ url = '/' }) {
|
|
|
|
let meta = this.cache.get(url)
|
2017-08-18 16:05:01 +00:00
|
|
|
|
2017-08-21 09:38:21 +00:00
|
|
|
if (meta) {
|
|
|
|
return meta
|
2017-08-18 16:05:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 09:38:21 +00:00
|
|
|
meta = {
|
|
|
|
HTML_ATTRS: '',
|
|
|
|
BODY_ATTRS: '',
|
|
|
|
HEAD: ''
|
|
|
|
}
|
|
|
|
// Get vue-meta context
|
|
|
|
const m = await this.getMeta(url)
|
|
|
|
// HTML_ATTRS
|
|
|
|
meta.HTML_ATTRS = 'data-n-head-ssr ' + m.htmlAttrs.text()
|
|
|
|
// BODY_ATTRS
|
|
|
|
meta.BODY_ATTRS = m.bodyAttrs.text()
|
|
|
|
// HEAD tags
|
|
|
|
meta.HEAD = m.meta.text() + m.title.text() + m.link.text() + m.style.text() + m.script.text() + m.noscript.text()
|
2017-08-19 09:50:45 +00:00
|
|
|
// Resource Hints
|
|
|
|
const clientManifest = this.renderer.resources.clientManifest
|
|
|
|
if (this.options.render.resourceHints && clientManifest) {
|
|
|
|
const publicPath = clientManifest.publicPath || '/_nuxt/'
|
|
|
|
// Pre-Load initial resources
|
|
|
|
if (Array.isArray(clientManifest.initial)) {
|
2017-08-21 09:38:21 +00:00
|
|
|
meta.HEAD += clientManifest.initial.map(r => `<link rel="preload" href="${publicPath}${r}" as="script" />`).join('')
|
2017-08-19 09:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pre-Fetch async resources
|
|
|
|
if (Array.isArray(clientManifest.async)) {
|
2017-08-21 09:38:21 +00:00
|
|
|
meta.HEAD += clientManifest.async.map(r => `<link rel="prefetch" href="${publicPath}${r}" />`).join('')
|
2017-08-19 09:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 09:38:21 +00:00
|
|
|
// Set meta tags inside cache
|
|
|
|
this.cache.set(url, meta)
|
2017-08-18 16:05:01 +00:00
|
|
|
|
2017-08-21 09:38:21 +00:00
|
|
|
return meta
|
2017-08-18 16:05:01 +00:00
|
|
|
}
|
|
|
|
}
|