Skip to content

fix: move ssrContext and updateMeta to core (#349, #370) #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions lib/app/ThemeWrapper.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<template>
<ThemeLayout v-if="$page.path"/>

<ThemeNotFound v-else/>
</template>

<script>
import ThemeLayout from '@themeLayout'
import ThemeNotFound from '@themeNotFound'
export default {
components: {
ThemeLayout,
ThemeNotFound
},
created () {
if (this.$ssrContext) {
this.$ssrContext.title = this.$title
this.$ssrContext.lang = this.$lang
this.$ssrContext.description = this.$page.description || this.$description
}
},
mounted () {
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
})
}
}
</script>

4 changes: 4 additions & 0 deletions lib/app/util.js
Original file line number Diff line number Diff line change
@@ -13,4 +13,8 @@ export function findPageForPath (pages, path) {
return page
}
}
return {
path: '',
frontmatter: {}
}
}
45 changes: 0 additions & 45 deletions lib/default-theme/Layout.vue
Original file line number Diff line number Diff line change
@@ -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
@@ -132,8 +107,6 @@ export default {
},
beforeDestroy () {
updateMetaTags(null, this.currentMetaTags)
window.removeEventListener('scroll', this.onScroll)
},
@@ -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>
7 changes: 3 additions & 4 deletions lib/prepare.js
Original file line number Diff line number Diff line change
@@ -311,7 +311,7 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
let code = `
{
path: ${JSON.stringify(pagePath)},
component: ThemeLayout,
component: ThemeWrapper,
beforeEnter: (to, from, next) => {
import(${JSON.stringify(filePath)}).then(comp => {
Vue.component(${JSON.stringify(fileToComponentName(file))}, comp.default)
@@ -334,12 +334,11 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
const notFoundRoute = `,
{
path: '*',
component: ThemeNotFound
component: ThemeWrapper
}`

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