Skip to content

Commit

Permalink
feat: Plan Meta Quasar plugin takeover from SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoenescu committed Jul 14, 2018
1 parent dfcfef6 commit 9062194
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/plugins/meta.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { isSSR } from './platform.js'
import { isSSR, fromSSR } from './platform.js'
import extend from '../utils/extend.js'

let updateId, ssrTakeover

function normalize (meta) {
if (meta.title) {
meta.title = meta.titleTemplate
Expand Down Expand Up @@ -137,6 +139,13 @@ function parseMeta (component, meta) {
}

function updateClient () {
if (ssrTakeover) {
ssrTakeover = false
this.$root.__currentMeta = window.__Q_META__
document.querySelector('script[data-qmeta-init]').remove()
return
}

const meta = {
title: '',
titleTemplate: null,
Expand All @@ -152,20 +161,6 @@ function updateClient () {
this.$root.__currentMeta = meta
}

function beforeCreate () {
if (this.$options.meta) {
if (typeof this.$options.meta === 'function') {
if (!this.$options.computed) {
this.$options.computed = {}
}
this.$options.computed.__qMeta = this.$options.meta
}
else {
this.__qMeta = this.$options.meta
}
}
}

function getAttr (seed) {
return att => {
const val = seed[att]
Expand Down Expand Up @@ -216,11 +211,27 @@ function getServerMeta (app) {
.join(' '),
'%% Q_BODY_TAGS %%': Object.keys(meta.noscripts)
.map(name => `<noscript data-qmeta="${name}">${meta.noscripts[name]}</noscript>`)
.join('')
.join('') + `<script data-qmeta-init>window.__Q_META__=${JSON.stringify(meta)}</script>`
}
}

let updateId
function beforeCreate () {
if (this.$options.meta) {
if (typeof this.$options.meta === 'function') {
if (!this.$options.computed) {
this.$options.computed = {}
}
this.$options.computed.__qMeta = this.$options.meta
}
else {
this.__qMeta = this.$options.meta
}
}
}

function triggerMeta () {
this.$options.meta && this.__qMetaUpdate()
}

export default {
install ({ Vue }) {
Expand All @@ -229,22 +240,17 @@ export default {
Vue.mixin({ beforeCreate })
}
else {
ssrTakeover = fromSSR
Vue.mixin({
beforeCreate,
created () {
if (this.$options.meta) {
this.__qMetaUnwatch = this.$watch('__qMeta', this.__qMetaUpdate)
}
},
activated () {
this.$options.meta && this.__qMetaUpdate()
},
deactivated () {
this.$options.meta && this.__qMetaUpdate()
},
beforeMount () {
this.$options.meta && this.__qMetaUpdate()
},
activated: triggerMeta,
deactivated: triggerMeta,
beforeMount: triggerMeta,
destroyed () {
if (this.$options.meta) {
this.__qMetaUnwatch()
Expand Down

0 comments on commit 9062194

Please sign in to comment.