Skip to content

Commit 39ef287

Browse files
lmichelinpimlie
authored andcommitted
fix: don't generate <title> tag if metaInfo.title is null or false (#409)
1 parent b150d82 commit 39ef287

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

src/server/generators/title.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
export default function titleGenerator (appId, { attribute } = {}, type, data) {
99
return {
1010
text () {
11+
if (!data) {
12+
return ''
13+
}
1114
return `<${type}>${data}</${type}>`
1215
}
1316
}

src/shared/escaping.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isString, isArray, isObject } from '../utils/is-type'
1+
import { isString, isArray, isPureObject } from '../utils/is-type'
22
import { includes } from '../utils/array'
33
import { metaInfoOptionKeys, disableOptionKeys } from './constants'
44

@@ -55,11 +55,11 @@ export function escape (info, options, escapeOptions) {
5555
escaped[key] = doEscape(value)
5656
} else if (isArray(value)) {
5757
escaped[key] = value.map((v) => {
58-
return isObject(v)
58+
return isPureObject(v)
5959
? escape(v, options, escapeOptions)
6060
: doEscape(v)
6161
})
62-
} else if (isObject(value)) {
62+
} else if (isPureObject(value)) {
6363
escaped[key] = escape(value, options, escapeOptions)
6464
} else {
6565
escaped[key] = value

src/utils/is-type.js

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export function isObject (arg) {
1515
return typeof arg === 'object'
1616
}
1717

18+
export function isPureObject (arg) {
19+
return typeof arg === 'object' && arg !== null
20+
}
21+
1822
export function isFunction (arg) {
1923
return typeof arg === 'function'
2024
}

test/unit/escaping.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ describe('escaping', () => {
3939
})
4040
})
4141

42+
test('null title is left as it is', () => {
43+
const component = new Vue({
44+
metaInfo: {
45+
title: null
46+
}
47+
})
48+
49+
expect(getMetaInfo(component, [[/&/g, '&amp;']])).toEqual({
50+
title: null,
51+
titleChunk: '',
52+
titleTemplate: '%s',
53+
htmlAttrs: {},
54+
headAttrs: {},
55+
bodyAttrs: {},
56+
meta: [],
57+
base: [],
58+
link: [],
59+
style: [],
60+
script: [],
61+
noscript: [],
62+
__dangerouslyDisableSanitizers: [],
63+
__dangerouslyDisableSanitizersByTagID: {}
64+
})
65+
})
66+
4267
test('special chars are escaped unless disabled by vmid', () => {
4368
const component = new Vue({
4469
metaInfo: {

test/unit/generators.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _generateServerInjector from '../../src/server/generateServerInjector'
22
import { defaultOptions } from '../../src/shared/constants'
33
import metaInfoData from '../utils/meta-info-data'
4+
import { titleGenerator } from '../../src/server/generators'
45

56
const generateServerInjector = (type, data) => _generateServerInjector('test', defaultOptions, type, data)
67

@@ -59,3 +60,10 @@ describe('generators', () => {
5960
})
6061
})
6162
})
63+
64+
describe('title generator should return an empty string when title is null', () => {
65+
const title = null
66+
const generatedTitle = titleGenerator(0, {}, 'title', title)
67+
68+
expect(generatedTitle.text()).toEqual('')
69+
})

0 commit comments

Comments
 (0)