Skip to content

Commit 5b62662

Browse files
committed
fix(compiler-ssr): should not render key/ref bindings in ssr
1 parent a5d6f80 commit 5b62662

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/compiler-ssr/__tests__/ssrElement.spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ describe('ssr: element', () => {
9494
).toMatchInlineSnapshot(`"\`<div id=\\"foo\\" class=\\"bar\\"></div>\`"`)
9595
})
9696

97+
test('ignore static key/ref', () => {
98+
expect(
99+
getCompiledString(`<div key="1" ref="el"></div>`)
100+
).toMatchInlineSnapshot(`"\`<div></div>\`"`)
101+
})
102+
103+
test('ignore v-bind key/ref', () => {
104+
expect(
105+
getCompiledString(`<div :key="1" :ref="el"></div>`)
106+
).toMatchInlineSnapshot(`"\`<div></div>\`"`)
107+
})
108+
97109
test('v-bind:class', () => {
98110
expect(getCompiledString(`<div id="foo" :class="bar"></div>`))
99111
.toMatchInlineSnapshot(`
@@ -139,7 +151,7 @@ describe('ssr: element', () => {
139151
`)
140152
})
141153

142-
test('v-bind:key (boolean)', () => {
154+
test('v-bind:arg (boolean)', () => {
143155
expect(getCompiledString(`<input type="checkbox" :checked="checked">`))
144156
.toMatchInlineSnapshot(`
145157
"\`<input type=\\"checkbox\\"\${
@@ -148,7 +160,7 @@ describe('ssr: element', () => {
148160
`)
149161
})
150162

151-
test('v-bind:key (non-boolean)', () => {
163+
test('v-bind:arg (non-boolean)', () => {
152164
expect(getCompiledString(`<div :id="id" class="bar"></div>`))
153165
.toMatchInlineSnapshot(`
154166
"\`<div\${
@@ -157,7 +169,7 @@ describe('ssr: element', () => {
157169
`)
158170
})
159171

160-
test('v-bind:[key]', () => {
172+
test('v-bind:[arg]', () => {
161173
expect(getCompiledString(`<div v-bind:[key]="value"></div>`))
162174
.toMatchInlineSnapshot(`
163175
"\`<div\${

packages/compiler-ssr/src/transforms/ssrTransformElement.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
198198
if (isStaticExp(key)) {
199199
let attrName = key.content
200200
// static key attr
201+
if (attrName === 'key' || attrName === 'ref') {
202+
continue
203+
}
201204
if (attrName === 'class') {
202205
openTag.push(
203206
` class="`,
@@ -274,6 +277,9 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
274277
if (node.tag === 'textarea' && prop.name === 'value' && prop.value) {
275278
rawChildrenMap.set(node, escapeHtml(prop.value.content))
276279
} else if (!hasDynamicVBind) {
280+
if (prop.name === 'key' || prop.name === 'ref') {
281+
continue
282+
}
277283
// static prop
278284
if (prop.name === 'class' && prop.value) {
279285
staticClassBinding = JSON.stringify(prop.value.content)

0 commit comments

Comments
 (0)