mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-22 02:20:34 +03:00
feat: auto add ssrAttribute to htmlAttrs
This commit is contained in:
@@ -8,9 +8,9 @@ import { isUndefined, isArray } from '../../utils/is-type'
|
|||||||
* @param {Object} data - the attributes to generate
|
* @param {Object} data - the attributes to generate
|
||||||
* @return {Object} - the attribute generator
|
* @return {Object} - the attribute generator
|
||||||
*/
|
*/
|
||||||
export default function attributeGenerator ({ attribute } = {}, type, data) {
|
export default function attributeGenerator ({ attribute, ssrAttribute } = {}, type, data) {
|
||||||
return {
|
return {
|
||||||
text () {
|
text (addSrrAttribute) {
|
||||||
let attributeStr = ''
|
let attributeStr = ''
|
||||||
const watchedAttrs = []
|
const watchedAttrs = []
|
||||||
|
|
||||||
@@ -26,7 +26,14 @@ export default function attributeGenerator ({ attribute } = {}, type, data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
attributeStr += `${attribute}="${(watchedAttrs.sort()).join(',')}"`
|
if (attributeStr) {
|
||||||
|
attributeStr += `${attribute}="${(watchedAttrs.sort()).join(',')}"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'htmlAttrs' && addSrrAttribute) {
|
||||||
|
return `${ssrAttribute}${attributeStr ? ' ' : ''}${attributeStr}`
|
||||||
|
}
|
||||||
|
|
||||||
return attributeStr
|
return attributeStr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { titleGenerator } from '../../src/server/generators'
|
|||||||
const generateServerInjector = (type, data) => _generateServerInjector('test', defaultOptions, type, data)
|
const generateServerInjector = (type, data) => _generateServerInjector('test', defaultOptions, type, data)
|
||||||
|
|
||||||
describe('generators', () => {
|
describe('generators', () => {
|
||||||
Object.keys(metaInfoData).forEach((type) => {
|
for (const type in metaInfoData) {
|
||||||
const typeTests = metaInfoData[type]
|
const typeTests = metaInfoData[type]
|
||||||
|
|
||||||
const testCases = {
|
const testCases = {
|
||||||
@@ -58,6 +58,19 @@ describe('generators', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('extra tests', () => {
|
||||||
|
test('auto add ssrAttribute', () => {
|
||||||
|
const htmlAttrs = generateServerInjector('htmlAttrs', {})
|
||||||
|
expect(htmlAttrs.text(true)).toBe('data-vue-meta-server-rendered')
|
||||||
|
|
||||||
|
const headAttrs = generateServerInjector('headAttrs', {})
|
||||||
|
expect(headAttrs.text(true)).toBe('')
|
||||||
|
|
||||||
|
const bodyAttrs = generateServerInjector('bodyAttrs', {})
|
||||||
|
expect(bodyAttrs.text(true)).toBe('')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user