Skip to content

Commit

Permalink
fix: missing title and desc in 404 and custom theme.
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed May 11, 2018
1 parent e872e91 commit fcaee80
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 46 deletions.
2 changes: 2 additions & 0 deletions lib/app/root-mixins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import updateMeta from './updateMeta'
export default [updateMeta]
48 changes: 48 additions & 0 deletions lib/app/root-mixins/updateMeta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export default {
created () {
if (this.$ssrContext) {
this.$ssrContext.title = this.$title
this.$ssrContext.lang = this.$lang
this.$ssrContext.description = this.$page.description || this.$description
}
},
mounted () {
// update title / meta tags
this.currentMetaTags = []
const updateMeta = () => {
document.title = this.$title
document.documentElement.lang = this.$lang
const meta = [
{
name: 'description',
content: this.$description
},
...(this.$page.frontmatter.meta || [])
]
this.currentMetaTags = updateMetaTags(meta, this.currentMetaTags)
}
this.$watch('$page', updateMeta)
updateMeta()
},
beforeDestroy () {
updateMetaTags(null, this.currentMetaTags)
}
}

function updateMetaTags (meta, current) {
if (current) {
current.forEach(c => {
document.head.removeChild(c)
})
}
if (meta) {
return meta.map(m => {
const tag = document.createElement('meta')
Object.keys(m).forEach(key => {
tag.setAttribute(key, m[key])
})
document.head.appendChild(tag)
return tag
})
}
}
8 changes: 8 additions & 0 deletions lib/code/injectRootMixins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable */
import rootMixins from '@app/root-mixins'
function injectRootMixins (options) {
if (!options.mixins) {
options.mixins = []
}
options.mixins.push(...rootMixins)
}
45 changes: 0 additions & 45 deletions lib/default-theme/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,7 @@ export default {
}
},
created () {
if (this.$ssrContext) {
this.$ssrContext.title = this.$title
this.$ssrContext.lang = this.$lang
this.$ssrContext.description = this.$page.description || this.$description
}
},
mounted () {
// update title / meta tags
this.currentMetaTags = []
const updateMeta = () => {
document.title = this.$title
document.documentElement.lang = this.$lang
const meta = [
{
name: 'description',
content: this.$description
},
...(this.$page.frontmatter.meta || [])
]
this.currentMetaTags = updateMetaTags(meta, this.currentMetaTags)
}
this.$watch('$page', updateMeta)
updateMeta()
window.addEventListener('scroll', this.onScroll)
// configure progress bar
Expand All @@ -132,8 +107,6 @@ export default {
},
beforeDestroy () {
updateMetaTags(null, this.currentMetaTags)
window.removeEventListener('scroll', this.onScroll)
},
Expand Down Expand Up @@ -191,24 +164,6 @@ export default {
}
}
}
function updateMetaTags (meta, current) {
if (current) {
current.forEach(c => {
document.head.removeChild(c)
})
}
if (meta) {
return meta.map(m => {
const tag = document.createElement('meta')
Object.keys(m).forEach(key => {
tag.setAttribute(key, m[key])
})
document.head.appendChild(tag)
return tag
})
}
}
</script>

<style src="prismjs/themes/prism-tomorrow.css"></style>
Expand Down
7 changes: 6 additions & 1 deletion lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,14 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
component: ThemeNotFound
}`

const injectRootMixin = await fs.readFile(path.resolve(__dirname, 'code/injectRootMixins.js'), 'utf-8')

return (
`import ThemeLayout from '@themeLayout'\n` +
`import ThemeNotFound from '@themeNotFound'\n` +
`import ThemeNotFound from '@themeNotFound'\n\n` +
`${injectRootMixin}\n` +
`injectRootMixins(ThemeLayout)\n` +
`injectRootMixins(ThemeNotFound)\n\n` +
`export const routes = [${pages.map(genRoute).join(',')}${notFoundRoute}\n]`
)
}
Expand Down

0 comments on commit fcaee80

Please sign in to comment.