2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-07 20:42:25 +03:00

feat: make ssr app id configurable

This commit is contained in:
pimlie
2019-07-16 10:04:17 +02:00
parent 9cf6d323db
commit b0c85e5301
9 changed files with 29 additions and 48 deletions
+6
View File
@@ -26,6 +26,12 @@ The name of the attribute that is added to the `html` tag to inform `vue-meta` t
See [How to prevent update on page load](/faq/prevent-initial)
### ssrAppId
- type `string`
- default `ssr`
The app id for a server side rendered app. You shouldnt have to change this normallygit s
### tagIDKeyName
- type `string`
- default `vmid`
+3 -3
View File
@@ -9,14 +9,14 @@ import { titleGenerator, attributeGenerator, tagGenerator } from './generators'
* @return {Object} - the new injector
*/
export default function generateServerInjector (appId, options, type, data) {
export default function generateServerInjector (options, type, data) {
if (type === 'title') {
return titleGenerator(appId, options, type, data)
return titleGenerator(options, type, data)
}
if (metaInfoAttributeKeys.includes(type)) {
return attributeGenerator(options, type, data)
}
return tagGenerator(appId, options, type, data)
return tagGenerator(options, type, data)
}
+2 -2
View File
@@ -7,7 +7,7 @@ import { booleanHtmlAttributes, tagsWithoutEndTag, tagsWithInnerContent, tagAttr
* @param {(Array<Object>|Object)} tags - an array of tag objects or a single object in case of base
* @return {Object} - the tag generator
*/
export default function tagGenerator (appId, { attribute, tagIDKeyName } = {}, type, tags) {
export default function tagGenerator ({ ssrAppId, attribute, tagIDKeyName } = {}, type, tags) {
return {
text ({ body = false } = {}) {
// build a string containing all tags of this type
@@ -51,7 +51,7 @@ export default function tagGenerator (appId, { attribute, tagIDKeyName } = {}, t
// generate tag exactly without any other redundant attribute
const observeTag = tag.once
? ''
: `${attribute}="${appId}"`
: `${attribute}="${ssrAppId}"`
// these tags have no end tag
const hasEndTag = !tagsWithoutEndTag.includes(type)
+1 -1
View File
@@ -5,7 +5,7 @@
* @param {String} data - the title text
* @return {Object} - the title generator
*/
export default function titleGenerator (appId, { attribute } = {}, type, data) {
export default function titleGenerator ({ attribute } = {}, type, data) {
return {
text () {
if (!data) {
+1 -1
View File
@@ -18,7 +18,7 @@ export default function _inject (options = {}) {
// generate server injectors
for (const key in metaInfo) {
if (!metaInfoOptionKeys.includes(key) && metaInfo.hasOwnProperty(key)) {
metaInfo[key] = generateServerInjector('ssr', options, key, metaInfo[key])
metaInfo[key] = generateServerInjector(options, key, metaInfo[key])
}
}
+5 -1
View File
@@ -44,13 +44,17 @@ export const metaTemplateKeyName = 'template'
// This is the key name for the content-holding property
export const contentKeyName = 'content'
// The id used for the ssr app
export const ssrAppId = 'ssr'
export const defaultOptions = {
keyName,
attribute,
ssrAttribute,
tagIDKeyName,
contentKeyName,
metaTemplateKeyName
metaTemplateKeyName,
ssrAppId
}
// List of metaInfo property keys which are configuration options (and dont generate html)
+1 -1
View File
@@ -80,7 +80,7 @@ export default function createMixin (Vue, options) {
// if this Vue-app was server rendered, set the appId to 'ssr'
// only one SSR app per page is supported
if (this.$root.$el && this.$root.$el.hasAttribute && this.$root.$el.hasAttribute('data-server-rendered')) {
this.$root._vueMeta.appId = 'ssr'
this.$root._vueMeta.appId = options.ssrAppId
}
})
-31
View File
@@ -96,35 +96,4 @@ describe('escaping', () => {
__dangerouslyDisableSanitizersByTagID: { noscape: ['innerHTML'] }
})
})
test.skip('special chars are escaped unless disabled by vmid', () => {
const component = new Vue({
metaInfo: {
title: 'Hello',
script: [
{ vmid: 'yescape', innerHTML: ['12', 'asd'] }
]
}
})
expect(getMetaInfo(component, [[/&/g, '&amp;']])).toEqual({
title: 'Hello',
titleChunk: 'Hello',
titleTemplate: '%s',
htmlAttrs: {},
headAttrs: {},
bodyAttrs: {},
meta: [],
base: [],
link: [],
style: [],
script: [
{ innerHTML: 'Hello &amp; Goodbye', vmid: 'yescape' },
{ innerHTML: 'Hello & Goodbye', vmid: 'noscape' }
],
noscript: [],
__dangerouslyDisableSanitizers: [],
__dangerouslyDisableSanitizersByTagID: { noscape: ['innerHTML'] }
})
})
})
+10 -8
View File
@@ -3,7 +3,9 @@ import { defaultOptions } from '../../src/shared/constants'
import metaInfoData from '../utils/meta-info-data'
import { titleGenerator } from '../../src/server/generators'
const generateServerInjector = (type, data) => _generateServerInjector('test', defaultOptions, type, data)
defaultOptions.ssrAppId = 'test'
const generateServerInjector = (type, data) => _generateServerInjector(defaultOptions, type, data)
describe('generators', () => {
for (const type in metaInfoData) {
@@ -62,6 +64,13 @@ describe('generators', () => {
})
describe('extra tests', () => {
test('title generator should return an empty string when title is null', () => {
const title = null
const generatedTitle = titleGenerator({}, 'title', title)
expect(generatedTitle.text()).toEqual('')
})
test('auto add ssrAttribute', () => {
const htmlAttrs = generateServerInjector('htmlAttrs', {})
expect(htmlAttrs.text(true)).toBe('data-vue-meta-server-rendered')
@@ -73,10 +82,3 @@ describe('extra tests', () => {
expect(bodyAttrs.text(true)).toBe('')
})
})
describe('title generator should return an empty string when title is null', () => {
const title = null
const generatedTitle = titleGenerator(0, {}, 'title', title)
expect(generatedTitle.text()).toEqual('')
})