mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-23 11:10:34 +03:00
feat: make ssr app id configurable
This commit is contained in:
@@ -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)
|
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
|
### tagIDKeyName
|
||||||
- type `string`
|
- type `string`
|
||||||
- default `vmid`
|
- default `vmid`
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ import { titleGenerator, attributeGenerator, tagGenerator } from './generators'
|
|||||||
* @return {Object} - the new injector
|
* @return {Object} - the new injector
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export default function generateServerInjector (appId, options, type, data) {
|
export default function generateServerInjector (options, type, data) {
|
||||||
if (type === 'title') {
|
if (type === 'title') {
|
||||||
return titleGenerator(appId, options, type, data)
|
return titleGenerator(options, type, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metaInfoAttributeKeys.includes(type)) {
|
if (metaInfoAttributeKeys.includes(type)) {
|
||||||
return attributeGenerator(options, type, data)
|
return attributeGenerator(options, type, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tagGenerator(appId, options, type, data)
|
return tagGenerator(options, type, data)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
* @param {(Array<Object>|Object)} tags - an array of tag objects or a single object in case of base
|
||||||
* @return {Object} - the tag generator
|
* @return {Object} - the tag generator
|
||||||
*/
|
*/
|
||||||
export default function tagGenerator (appId, { attribute, tagIDKeyName } = {}, type, tags) {
|
export default function tagGenerator ({ ssrAppId, attribute, tagIDKeyName } = {}, type, tags) {
|
||||||
return {
|
return {
|
||||||
text ({ body = false } = {}) {
|
text ({ body = false } = {}) {
|
||||||
// build a string containing all tags of this type
|
// 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
|
// generate tag exactly without any other redundant attribute
|
||||||
const observeTag = tag.once
|
const observeTag = tag.once
|
||||||
? ''
|
? ''
|
||||||
: `${attribute}="${appId}"`
|
: `${attribute}="${ssrAppId}"`
|
||||||
|
|
||||||
// these tags have no end tag
|
// these tags have no end tag
|
||||||
const hasEndTag = !tagsWithoutEndTag.includes(type)
|
const hasEndTag = !tagsWithoutEndTag.includes(type)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
* @param {String} data - the title text
|
* @param {String} data - the title text
|
||||||
* @return {Object} - the title generator
|
* @return {Object} - the title generator
|
||||||
*/
|
*/
|
||||||
export default function titleGenerator (appId, { attribute } = {}, type, data) {
|
export default function titleGenerator ({ attribute } = {}, type, data) {
|
||||||
return {
|
return {
|
||||||
text () {
|
text () {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export default function _inject (options = {}) {
|
|||||||
// generate server injectors
|
// generate server injectors
|
||||||
for (const key in metaInfo) {
|
for (const key in metaInfo) {
|
||||||
if (!metaInfoOptionKeys.includes(key) && metaInfo.hasOwnProperty(key)) {
|
if (!metaInfoOptionKeys.includes(key) && metaInfo.hasOwnProperty(key)) {
|
||||||
metaInfo[key] = generateServerInjector('ssr', options, key, metaInfo[key])
|
metaInfo[key] = generateServerInjector(options, key, metaInfo[key])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,13 +44,17 @@ export const metaTemplateKeyName = 'template'
|
|||||||
// This is the key name for the content-holding property
|
// This is the key name for the content-holding property
|
||||||
export const contentKeyName = 'content'
|
export const contentKeyName = 'content'
|
||||||
|
|
||||||
|
// The id used for the ssr app
|
||||||
|
export const ssrAppId = 'ssr'
|
||||||
|
|
||||||
export const defaultOptions = {
|
export const defaultOptions = {
|
||||||
keyName,
|
keyName,
|
||||||
attribute,
|
attribute,
|
||||||
ssrAttribute,
|
ssrAttribute,
|
||||||
tagIDKeyName,
|
tagIDKeyName,
|
||||||
contentKeyName,
|
contentKeyName,
|
||||||
metaTemplateKeyName
|
metaTemplateKeyName,
|
||||||
|
ssrAppId
|
||||||
}
|
}
|
||||||
|
|
||||||
// List of metaInfo property keys which are configuration options (and dont generate html)
|
// List of metaInfo property keys which are configuration options (and dont generate html)
|
||||||
|
|||||||
+1
-1
@@ -80,7 +80,7 @@ export default function createMixin (Vue, options) {
|
|||||||
// if this Vue-app was server rendered, set the appId to 'ssr'
|
// if this Vue-app was server rendered, set the appId to 'ssr'
|
||||||
// only one SSR app per page is supported
|
// only one SSR app per page is supported
|
||||||
if (this.$root.$el && this.$root.$el.hasAttribute && this.$root.$el.hasAttribute('data-server-rendered')) {
|
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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -96,35 +96,4 @@ describe('escaping', () => {
|
|||||||
__dangerouslyDisableSanitizersByTagID: { noscape: ['innerHTML'] }
|
__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, '&']])).toEqual({
|
|
||||||
title: 'Hello',
|
|
||||||
titleChunk: 'Hello',
|
|
||||||
titleTemplate: '%s',
|
|
||||||
htmlAttrs: {},
|
|
||||||
headAttrs: {},
|
|
||||||
bodyAttrs: {},
|
|
||||||
meta: [],
|
|
||||||
base: [],
|
|
||||||
link: [],
|
|
||||||
style: [],
|
|
||||||
script: [
|
|
||||||
{ innerHTML: 'Hello & Goodbye', vmid: 'yescape' },
|
|
||||||
{ innerHTML: 'Hello & Goodbye', vmid: 'noscape' }
|
|
||||||
],
|
|
||||||
noscript: [],
|
|
||||||
__dangerouslyDisableSanitizers: [],
|
|
||||||
__dangerouslyDisableSanitizersByTagID: { noscape: ['innerHTML'] }
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ import { defaultOptions } from '../../src/shared/constants'
|
|||||||
import metaInfoData from '../utils/meta-info-data'
|
import metaInfoData from '../utils/meta-info-data'
|
||||||
import { titleGenerator } from '../../src/server/generators'
|
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', () => {
|
describe('generators', () => {
|
||||||
for (const type in metaInfoData) {
|
for (const type in metaInfoData) {
|
||||||
@@ -62,6 +64,13 @@ describe('generators', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('extra tests', () => {
|
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', () => {
|
test('auto add ssrAttribute', () => {
|
||||||
const htmlAttrs = generateServerInjector('htmlAttrs', {})
|
const htmlAttrs = generateServerInjector('htmlAttrs', {})
|
||||||
expect(htmlAttrs.text(true)).toBe('data-vue-meta-server-rendered')
|
expect(htmlAttrs.text(true)).toBe('data-vue-meta-server-rendered')
|
||||||
@@ -73,10 +82,3 @@ describe('extra tests', () => {
|
|||||||
expect(bodyAttrs.text(true)).toBe('')
|
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('')
|
|
||||||
})
|
|
||||||
|
|||||||
Reference in New Issue
Block a user