Skip to content

Commit fcaee80

Browse files
committed
fix: missing title and desc in 404 and custom theme.
1 parent e872e91 commit fcaee80

File tree

5 files changed

+64
-46
lines changed

5 files changed

+64
-46
lines changed

lib/app/root-mixins/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import updateMeta from './updateMeta'
2+
export default [updateMeta]

lib/app/root-mixins/updateMeta.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export default {
2+
created () {
3+
if (this.$ssrContext) {
4+
this.$ssrContext.title = this.$title
5+
this.$ssrContext.lang = this.$lang
6+
this.$ssrContext.description = this.$page.description || this.$description
7+
}
8+
},
9+
mounted () {
10+
// update title / meta tags
11+
this.currentMetaTags = []
12+
const updateMeta = () => {
13+
document.title = this.$title
14+
document.documentElement.lang = this.$lang
15+
const meta = [
16+
{
17+
name: 'description',
18+
content: this.$description
19+
},
20+
...(this.$page.frontmatter.meta || [])
21+
]
22+
this.currentMetaTags = updateMetaTags(meta, this.currentMetaTags)
23+
}
24+
this.$watch('$page', updateMeta)
25+
updateMeta()
26+
},
27+
beforeDestroy () {
28+
updateMetaTags(null, this.currentMetaTags)
29+
}
30+
}
31+
32+
function updateMetaTags (meta, current) {
33+
if (current) {
34+
current.forEach(c => {
35+
document.head.removeChild(c)
36+
})
37+
}
38+
if (meta) {
39+
return meta.map(m => {
40+
const tag = document.createElement('meta')
41+
Object.keys(m).forEach(key => {
42+
tag.setAttribute(key, m[key])
43+
})
44+
document.head.appendChild(tag)
45+
return tag
46+
})
47+
}
48+
}

lib/code/injectRootMixins.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* eslint-disable */
2+
import rootMixins from '@app/root-mixins'
3+
function injectRootMixins (options) {
4+
if (!options.mixins) {
5+
options.mixins = []
6+
}
7+
options.mixins.push(...rootMixins)
8+
}

lib/default-theme/Layout.vue

-45
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,7 @@ export default {
8787
}
8888
},
8989
90-
created () {
91-
if (this.$ssrContext) {
92-
this.$ssrContext.title = this.$title
93-
this.$ssrContext.lang = this.$lang
94-
this.$ssrContext.description = this.$page.description || this.$description
95-
}
96-
},
97-
9890
mounted () {
99-
// update title / meta tags
100-
this.currentMetaTags = []
101-
const updateMeta = () => {
102-
document.title = this.$title
103-
document.documentElement.lang = this.$lang
104-
const meta = [
105-
{
106-
name: 'description',
107-
content: this.$description
108-
},
109-
...(this.$page.frontmatter.meta || [])
110-
]
111-
this.currentMetaTags = updateMetaTags(meta, this.currentMetaTags)
112-
}
113-
this.$watch('$page', updateMeta)
114-
updateMeta()
115-
11691
window.addEventListener('scroll', this.onScroll)
11792
11893
// configure progress bar
@@ -132,8 +107,6 @@ export default {
132107
},
133108
134109
beforeDestroy () {
135-
updateMetaTags(null, this.currentMetaTags)
136-
137110
window.removeEventListener('scroll', this.onScroll)
138111
},
139112
@@ -191,24 +164,6 @@ export default {
191164
}
192165
}
193166
}
194-
195-
function updateMetaTags (meta, current) {
196-
if (current) {
197-
current.forEach(c => {
198-
document.head.removeChild(c)
199-
})
200-
}
201-
if (meta) {
202-
return meta.map(m => {
203-
const tag = document.createElement('meta')
204-
Object.keys(m).forEach(key => {
205-
tag.setAttribute(key, m[key])
206-
})
207-
document.head.appendChild(tag)
208-
return tag
209-
})
210-
}
211-
}
212167
</script>
213168

214169
<style src="prismjs/themes/prism-tomorrow.css"></style>

lib/prepare.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,14 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
342342
component: ThemeNotFound
343343
}`
344344

345+
const injectRootMixin = await fs.readFile(path.resolve(__dirname, 'code/injectRootMixins.js'), 'utf-8')
346+
345347
return (
346348
`import ThemeLayout from '@themeLayout'\n` +
347-
`import ThemeNotFound from '@themeNotFound'\n` +
349+
`import ThemeNotFound from '@themeNotFound'\n\n` +
350+
`${injectRootMixin}\n` +
351+
`injectRootMixins(ThemeLayout)\n` +
352+
`injectRootMixins(ThemeNotFound)\n\n` +
348353
`export const routes = [${pages.map(genRoute).join(',')}${notFoundRoute}\n]`
349354
)
350355
}

0 commit comments

Comments
 (0)