Skip to content

Commit f6ca397

Browse files
author
ludanxer
committed
fix: render meta tags during ssr
1 parent 4f50deb commit f6ca397

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

Diff for: packages/@vuepress/core/lib/client/index.ssr.html

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width,initial-scale=1">
66
<title>{{ title }}</title>
7-
<meta name="description" content="{{ description }}">
87
<meta name="generator" content="VuePress {{ version }}">
98
{{{ userHeadTags }}}
109
{{{ pageMeta }}}

Diff for: packages/@vuepress/core/lib/client/root-mixins/updateMeta.js

+35-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import unionBy from 'lodash/unionBy'
22

33
export default {
4+
// created will be called on both client and ssr
45
created () {
56
if (this.$ssrContext) {
7+
const siteMetaTags = this.$site.headTags
8+
.filter(item => item[0] === 'meta')
9+
.map(item => item[1])
10+
11+
const meta = this.getMergedMetaTags(siteMetaTags)
12+
613
this.$ssrContext.title = this.$title
714
this.$ssrContext.lang = this.$lang
8-
this.$ssrContext.description = this.$page.description || this.$description
15+
this.$ssrContext.pageMeta = renderPageMeta(meta)
916
}
1017
},
11-
18+
// Other life cycles will only be called at client
1219
mounted () {
1320
// init currentMetaTags from DOM
1421
this.currentMetaTags = [...document.querySelectorAll('meta')]
@@ -31,13 +38,16 @@ export default {
3138
document.title = this.$title
3239
document.documentElement.lang = this.$lang
3340

34-
const pageMetaTags = (this.$page.frontmatter.meta || []).slice(0)
41+
const newMetaTags = this.getMergedMetaTags(this.siteMetaTags)
42+
this.currentMetaTags = updateMetaTags(newMetaTags, this.currentMetaTags)
43+
},
44+
45+
getMergedMetaTags (siteMeta) {
46+
const pageMeta = (this.$page.frontmatter.meta || []).slice(0)
3547
// pageMetaTags have higher priority than siteMetaTags
3648
// description needs special attention for it has too many entries
37-
const newMetaTags = unionBy([{ name: 'description', content: this.$description }],
38-
pageMetaTags, this.siteMetaTags, metaIdentifier)
39-
40-
this.currentMetaTags = updateMetaTags(newMetaTags, this.currentMetaTags)
49+
return unionBy([{ name: 'description', content: this.$description }],
50+
pageMeta, siteMeta, metaIdentifier)
4151
}
4252
},
4353

@@ -89,3 +99,21 @@ function metaIdentifier (tag) {
8999
}
90100
return JSON.stringify(tag)
91101
}
102+
103+
/**
104+
* Render meta tags
105+
*
106+
* @param {Array} meta
107+
* @returns {Array<string>}
108+
*/
109+
110+
function renderPageMeta (meta) {
111+
if (!meta) return ''
112+
return meta.map(m => {
113+
let res = `<meta`
114+
Object.keys(m).forEach(key => {
115+
res += ` ${key}="${m[key]}"`
116+
})
117+
return res + `>`
118+
}).join('\n ')
119+
}

Diff for: packages/@vuepress/core/lib/node/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ module.exports = class App {
438438
title: this.siteConfig.title || '',
439439
description: this.siteConfig.description || '',
440440
base: this.base,
441+
headTags: this.siteConfig.head || [],
441442
pages: this.pages.map(page => page.toJson()),
442443
themeConfig: this.siteConfig.themeConfig || {},
443444
locales
@@ -499,4 +500,3 @@ module.exports = class App {
499500
return this
500501
}
501502
}
502-

Diff for: packages/@vuepress/core/lib/node/build/index.js

+4-24
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ module.exports = class Build extends EventEmitter {
7777
})
7878

7979
// pre-render head tags from user config
80+
// filter out meta tags for they will be injected in updateMeta.js
8081
this.userHeadTags = (this.context.siteConfig.head || [])
82+
.filter(item => item[0] !== 'meta')
8183
.map(renderHeadTag)
82-
.join('\n ')
84+
.join('\n ')
8385

8486
// if the user does not have a custom 404.md, generate the theme's default
8587
if (!this.context.pages.some(p => p.path === '/404.html')) {
@@ -138,14 +140,10 @@ module.exports = class Build extends EventEmitter {
138140
readline.cursorTo(process.stdout, 0)
139141
process.stdout.write(`Rendering page: ${pagePath}`)
140142

141-
// #565 Avoid duplicate description meta at SSR.
142-
const meta = (page.frontmatter && page.frontmatter.meta || []).filter(item => item.name !== 'description')
143-
const pageMeta = renderPageMeta(meta)
144-
145143
const context = {
146144
url: page.path,
147145
userHeadTags: this.userHeadTags,
148-
pageMeta,
146+
pageMeta: null,
149147
title: 'VuePress',
150148
lang: 'en',
151149
description: '',
@@ -225,24 +223,6 @@ function renderAttrs (attrs = {}) {
225223
}
226224
}
227225

228-
/**
229-
* Render meta tags
230-
*
231-
* @param {Array} meta
232-
* @returns {Array<string>}
233-
*/
234-
235-
function renderPageMeta (meta) {
236-
if (!meta) return ''
237-
return meta.map(m => {
238-
let res = `<meta`
239-
Object.keys(m).forEach(key => {
240-
res += ` ${key}="${escape(m[key])}"`
241-
})
242-
return res + `>`
243-
}).join('')
244-
}
245-
246226
/**
247227
* find and remove empty style chunk caused by
248228
* https://github.com/webpack-contrib/mini-css-extract-plugin/issues/85

0 commit comments

Comments
 (0)