Skip to content

Commit 9cf6d32

Browse files
committed
feat: auto add ssrAttribute to htmlAttrs
1 parent 168244d commit 9cf6d32

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/server/generators/attribute.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { isUndefined, isArray } from '../../utils/is-type'
88
* @param {Object} data - the attributes to generate
99
* @return {Object} - the attribute generator
1010
*/
11-
export default function attributeGenerator ({ attribute } = {}, type, data) {
11+
export default function attributeGenerator ({ attribute, ssrAttribute } = {}, type, data) {
1212
return {
13-
text () {
13+
text (addSrrAttribute) {
1414
let attributeStr = ''
1515
const watchedAttrs = []
1616

@@ -26,7 +26,14 @@ export default function attributeGenerator ({ attribute } = {}, type, data) {
2626
}
2727
}
2828

29-
attributeStr += `${attribute}="${(watchedAttrs.sort()).join(',')}"`
29+
if (attributeStr) {
30+
attributeStr += `${attribute}="${(watchedAttrs.sort()).join(',')}"`
31+
}
32+
33+
if (type === 'htmlAttrs' && addSrrAttribute) {
34+
return `${ssrAttribute}${attributeStr ? ' ' : ''}${attributeStr}`
35+
}
36+
3037
return attributeStr
3138
}
3239
}

test/unit/generators.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { titleGenerator } from '../../src/server/generators'
66
const generateServerInjector = (type, data) => _generateServerInjector('test', defaultOptions, type, data)
77

88
describe('generators', () => {
9-
Object.keys(metaInfoData).forEach((type) => {
9+
for (const type in metaInfoData) {
1010
const typeTests = metaInfoData[type]
1111

1212
const testCases = {
@@ -58,6 +58,19 @@ describe('generators', () => {
5858
}
5959
})
6060
})
61+
}
62+
})
63+
64+
describe('extra tests', () => {
65+
test('auto add ssrAttribute', () => {
66+
const htmlAttrs = generateServerInjector('htmlAttrs', {})
67+
expect(htmlAttrs.text(true)).toBe('data-vue-meta-server-rendered')
68+
69+
const headAttrs = generateServerInjector('headAttrs', {})
70+
expect(headAttrs.text(true)).toBe('')
71+
72+
const bodyAttrs = generateServerInjector('bodyAttrs', {})
73+
expect(bodyAttrs.text(true)).toBe('')
6174
})
6275
})
6376

0 commit comments

Comments
 (0)