Skip to content

Commit

Permalink
feat: Finalize Meta Quasar plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoenescu committed Jul 15, 2018
1 parent 7fe5afe commit e1f3f9d
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions src/plugins/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function normalize (meta) {
delete meta.titleTemplate
}

[['tags', 'content'], ['links', 'href']].forEach(type => {
;[['meta', 'content'], ['link', 'href']].forEach(type => {
const
metaType = meta[type[0]],
metaProp = type[1]
Expand All @@ -32,7 +32,9 @@ function normalize (meta) {
}

function changed (old, def) {
if (Object.keys(old).length !== Object.keys(def).length) { return true }
if (Object.keys(old).length !== Object.keys(def).length) {
return true
}
for (let key in old) {
if (old[key] !== def[key]) { return true }
}
Expand All @@ -49,7 +51,7 @@ function diff (meta, other) {
add.title = other.title
}

;['tags', 'links', 'htmlAttrs', 'bodyAttrs'].forEach(type => {
;['meta', 'link', 'htmlAttr', 'bodyAttr'].forEach(type => {
const old = meta[type], cur = other[type]
remove[type] = []

Expand Down Expand Up @@ -81,39 +83,37 @@ function apply ({ add, remove }) {
}

if (Object.keys(remove).length > 0) {
remove.tags.forEach(name => {
remove.meta.forEach(name => {
document.head.querySelector(`meta[data-qmeta="${name}"]`).remove()
})
remove.links.forEach(name => {
remove.link.forEach(name => {
document.head.querySelector(`link[data-qmeta="${name}"]`).remove()
})
remove.htmlAttrs.forEach(name => {
remove.htmlAttr.forEach(name => {
document.documentElement.removeAttribute(name)
})
remove.bodyAttrs.forEach(name => {
remove.bodyAttr.forEach(name => {
document.body.removeAttribute(name)
})
}

;[['tags', 'meta'], ['links', 'link']].forEach(type => {
const
metaType = add[type[0]],
tagName = type[1]
;['meta', 'link'].forEach(type => {
const metaType = add[type]

for (let name in metaType) {
const tag = document.createElement(tagName)
const tag = document.createElement(type)
for (let att in metaType[name]) {
tag.setAttribute(att, metaType[name][att])
}
tag.setAttribute('data-qmeta', name)
document.head.appendChild(tag)
}
})
Object.keys(add.htmlAttrs).forEach(name => {
document.documentElement.setAttribute(name, add.htmlAttrs[name] || '')
Object.keys(add.htmlAttr).forEach(name => {
document.documentElement.setAttribute(name, add.htmlAttr[name] || '')
})
Object.keys(add.bodyAttrs).forEach(name => {
document.body.setAttribute(name, add.bodyAttrs[name] || '')
Object.keys(add.bodyAttr).forEach(name => {
document.body.setAttribute(name, add.bodyAttr[name] || '')
})
}

Expand Down Expand Up @@ -149,10 +149,10 @@ function updateClient () {
const meta = {
title: '',
titleTemplate: null,
tags: {},
links: {},
htmlAttrs: {},
bodyAttrs: {}
meta: {},
link: {},
htmlAttr: {},
bodyAttr: {}
}
parseMeta(this.$root, meta)
normalize(meta)
Expand All @@ -173,14 +173,12 @@ function getHead (meta) {
if (meta.title) {
output += `<title>${meta.title}</title>`
}
;[['tags', 'meta'], ['links', 'link']].forEach(type => {
const
metaType = meta[type[0]],
tag = type[1]
;['meta', 'link'].forEach(type => {
const metaType = meta[type]

for (let att in metaType) {
const attrs = Object.keys(metaType[att]).map(getAttr(metaType[att]))
output += `<${tag} ${attrs.join(' ')} data-qmeta="${att}">`
output += `<${type} ${attrs.join(' ')} data-qmeta="${att}">`
}
})
return output
Expand All @@ -190,30 +188,30 @@ function getServerMeta (app, html) {
const meta = {
title: '',
titleTemplate: null,
tags: {},
links: {},
htmlAttrs: {},
bodyAttrs: {},
noscripts: {}
meta: {},
link: {},
htmlAttr: {},
bodyAttr: {},
noscript: {}
}

parseMeta(app, meta)
normalize(meta)

const tokens = {
'%%Q_HTML_ATTRS%%': Object.keys(meta.htmlAttrs)
'%%Q_HTML_ATTRS%%': Object.keys(meta.htmlAttr)
.filter(name => !['lang', 'dir'].includes(name))
.map(getAttr(meta.htmlAttrs))
.map(getAttr(meta.htmlAttr))
.join(' '),
'%%Q_HEAD_TAGS%%': getHead(meta),
'%%Q_BODY_ATTRS%%': Object.keys(meta.bodyAttrs)
'%%Q_BODY_ATTRS%%': Object.keys(meta.bodyAttr)
.filter(name => name !== 'class')
.map(getAttr(meta.bodyAttrs))
.map(getAttr(meta.bodyAttr))
.join(' '),
'%%Q_BODY_TAGS%%': Object.keys(meta.noscripts)
.map(name => `<noscript data-qmeta="${name}">${meta.noscripts[name]}</noscript>`)
'%%Q_BODY_TAGS%%': Object.keys(meta.noscript)
.map(name => `<noscript data-qmeta="${name}">${meta.noscript[name]}</noscript>`)
.join('') +
`<script data-qmeta-init>window.__Q_META__=${delete meta.noscripts && JSON.stringify(meta)}</script>`
`<script data-qmeta-init>window.__Q_META__=${delete meta.noscript && JSON.stringify(meta)}</script>`
}

Object.keys(tokens).forEach(key => {
Expand Down Expand Up @@ -254,7 +252,6 @@ export default {
Q_BODY_ATTRS: '%%Q_BODY_ATTRS%%',
Q_BODY_TAGS: '%%Q_BODY_TAGS%%'
})
// ssr.Q_BODY_CLASSES
})
}
else {
Expand Down

0 comments on commit e1f3f9d

Please sign in to comment.