Skip to content

Commit b0c85e5

Browse files
committed
feat: make ssr app id configurable
1 parent 9cf6d32 commit b0c85e5

File tree

9 files changed

+29
-48
lines changed

9 files changed

+29
-48
lines changed

docs/api/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ The name of the attribute that is added to the `html` tag to inform `vue-meta` t
2626

2727
See [How to prevent update on page load](/faq/prevent-initial)
2828

29+
### ssrAppId
30+
- type `string`
31+
- default `ssr`
32+
33+
The app id for a server side rendered app. You shouldnt have to change this normallygit s
34+
2935
### tagIDKeyName
3036
- type `string`
3137
- default `vmid`

src/server/generateServerInjector.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import { titleGenerator, attributeGenerator, tagGenerator } from './generators'
99
* @return {Object} - the new injector
1010
*/
1111

12-
export default function generateServerInjector (appId, options, type, data) {
12+
export default function generateServerInjector (options, type, data) {
1313
if (type === 'title') {
14-
return titleGenerator(appId, options, type, data)
14+
return titleGenerator(options, type, data)
1515
}
1616

1717
if (metaInfoAttributeKeys.includes(type)) {
1818
return attributeGenerator(options, type, data)
1919
}
2020

21-
return tagGenerator(appId, options, type, data)
21+
return tagGenerator(options, type, data)
2222
}

src/server/generators/tag.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { booleanHtmlAttributes, tagsWithoutEndTag, tagsWithInnerContent, tagAttr
77
* @param {(Array<Object>|Object)} tags - an array of tag objects or a single object in case of base
88
* @return {Object} - the tag generator
99
*/
10-
export default function tagGenerator (appId, { attribute, tagIDKeyName } = {}, type, tags) {
10+
export default function tagGenerator ({ ssrAppId, attribute, tagIDKeyName } = {}, type, tags) {
1111
return {
1212
text ({ body = false } = {}) {
1313
// build a string containing all tags of this type
@@ -51,7 +51,7 @@ export default function tagGenerator (appId, { attribute, tagIDKeyName } = {}, t
5151
// generate tag exactly without any other redundant attribute
5252
const observeTag = tag.once
5353
? ''
54-
: `${attribute}="${appId}"`
54+
: `${attribute}="${ssrAppId}"`
5555

5656
// these tags have no end tag
5757
const hasEndTag = !tagsWithoutEndTag.includes(type)

src/server/generators/title.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @param {String} data - the title text
66
* @return {Object} - the title generator
77
*/
8-
export default function titleGenerator (appId, { attribute } = {}, type, data) {
8+
export default function titleGenerator ({ attribute } = {}, type, data) {
99
return {
1010
text () {
1111
if (!data) {

src/server/inject.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default function _inject (options = {}) {
1818
// generate server injectors
1919
for (const key in metaInfo) {
2020
if (!metaInfoOptionKeys.includes(key) && metaInfo.hasOwnProperty(key)) {
21-
metaInfo[key] = generateServerInjector('ssr', options, key, metaInfo[key])
21+
metaInfo[key] = generateServerInjector(options, key, metaInfo[key])
2222
}
2323
}
2424

src/shared/constants.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ export const metaTemplateKeyName = 'template'
4444
// This is the key name for the content-holding property
4545
export const contentKeyName = 'content'
4646

47+
// The id used for the ssr app
48+
export const ssrAppId = 'ssr'
49+
4750
export const defaultOptions = {
4851
keyName,
4952
attribute,
5053
ssrAttribute,
5154
tagIDKeyName,
5255
contentKeyName,
53-
metaTemplateKeyName
56+
metaTemplateKeyName,
57+
ssrAppId
5458
}
5559

5660
// List of metaInfo property keys which are configuration options (and dont generate html)

src/shared/mixin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default function createMixin (Vue, options) {
8080
// if this Vue-app was server rendered, set the appId to 'ssr'
8181
// only one SSR app per page is supported
8282
if (this.$root.$el && this.$root.$el.hasAttribute && this.$root.$el.hasAttribute('data-server-rendered')) {
83-
this.$root._vueMeta.appId = 'ssr'
83+
this.$root._vueMeta.appId = options.ssrAppId
8484
}
8585
})
8686

test/unit/escaping.test.js

-31
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,4 @@ describe('escaping', () => {
9696
__dangerouslyDisableSanitizersByTagID: { noscape: ['innerHTML'] }
9797
})
9898
})
99-
100-
test.skip('special chars are escaped unless disabled by vmid', () => {
101-
const component = new Vue({
102-
metaInfo: {
103-
title: 'Hello',
104-
script: [
105-
{ vmid: 'yescape', innerHTML: ['12', 'asd'] }
106-
]
107-
}
108-
})
109-
110-
expect(getMetaInfo(component, [[/&/g, '&amp;']])).toEqual({
111-
title: 'Hello',
112-
titleChunk: 'Hello',
113-
titleTemplate: '%s',
114-
htmlAttrs: {},
115-
headAttrs: {},
116-
bodyAttrs: {},
117-
meta: [],
118-
base: [],
119-
link: [],
120-
style: [],
121-
script: [
122-
{ innerHTML: 'Hello &amp; Goodbye', vmid: 'yescape' },
123-
{ innerHTML: 'Hello & Goodbye', vmid: 'noscape' }
124-
],
125-
noscript: [],
126-
__dangerouslyDisableSanitizers: [],
127-
__dangerouslyDisableSanitizersByTagID: { noscape: ['innerHTML'] }
128-
})
129-
})
13099
})

test/unit/generators.test.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { defaultOptions } from '../../src/shared/constants'
33
import metaInfoData from '../utils/meta-info-data'
44
import { titleGenerator } from '../../src/server/generators'
55

6-
const generateServerInjector = (type, data) => _generateServerInjector('test', defaultOptions, type, data)
6+
defaultOptions.ssrAppId = 'test'
7+
8+
const generateServerInjector = (type, data) => _generateServerInjector(defaultOptions, type, data)
79

810
describe('generators', () => {
911
for (const type in metaInfoData) {
@@ -62,6 +64,13 @@ describe('generators', () => {
6264
})
6365

6466
describe('extra tests', () => {
67+
test('title generator should return an empty string when title is null', () => {
68+
const title = null
69+
const generatedTitle = titleGenerator({}, 'title', title)
70+
71+
expect(generatedTitle.text()).toEqual('')
72+
})
73+
6574
test('auto add ssrAttribute', () => {
6675
const htmlAttrs = generateServerInjector('htmlAttrs', {})
6776
expect(htmlAttrs.text(true)).toBe('data-vue-meta-server-rendered')
@@ -73,10 +82,3 @@ describe('extra tests', () => {
7382
expect(bodyAttrs.text(true)).toBe('')
7483
})
7584
})
76-
77-
describe('title generator should return an empty string when title is null', () => {
78-
const title = null
79-
const generatedTitle = titleGenerator(0, {}, 'title', title)
80-
81-
expect(generatedTitle.text()).toEqual('')
82-
})

0 commit comments

Comments
 (0)