Skip to content

Commit

Permalink
fix(compiler-ssr): should not render key/ref bindings in ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 27, 2020
1 parent a5d6f80 commit 5b62662
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/compiler-ssr/__tests__/ssrElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ describe('ssr: element', () => {
).toMatchInlineSnapshot(`"\`<div id=\\"foo\\" class=\\"bar\\"></div>\`"`)
})

test('ignore static key/ref', () => {
expect(
getCompiledString(`<div key="1" ref="el"></div>`)
).toMatchInlineSnapshot(`"\`<div></div>\`"`)
})

test('ignore v-bind key/ref', () => {
expect(
getCompiledString(`<div :key="1" :ref="el"></div>`)
).toMatchInlineSnapshot(`"\`<div></div>\`"`)
})

test('v-bind:class', () => {
expect(getCompiledString(`<div id="foo" :class="bar"></div>`))
.toMatchInlineSnapshot(`
Expand Down Expand Up @@ -139,7 +151,7 @@ describe('ssr: element', () => {
`)
})

test('v-bind:key (boolean)', () => {
test('v-bind:arg (boolean)', () => {
expect(getCompiledString(`<input type="checkbox" :checked="checked">`))
.toMatchInlineSnapshot(`
"\`<input type=\\"checkbox\\"\${
Expand All @@ -148,7 +160,7 @@ describe('ssr: element', () => {
`)
})

test('v-bind:key (non-boolean)', () => {
test('v-bind:arg (non-boolean)', () => {
expect(getCompiledString(`<div :id="id" class="bar"></div>`))
.toMatchInlineSnapshot(`
"\`<div\${
Expand All @@ -157,7 +169,7 @@ describe('ssr: element', () => {
`)
})

test('v-bind:[key]', () => {
test('v-bind:[arg]', () => {
expect(getCompiledString(`<div v-bind:[key]="value"></div>`))
.toMatchInlineSnapshot(`
"\`<div\${
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler-ssr/src/transforms/ssrTransformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
if (isStaticExp(key)) {
let attrName = key.content
// static key attr
if (attrName === 'key' || attrName === 'ref') {
continue
}
if (attrName === 'class') {
openTag.push(
` class="`,
Expand Down Expand Up @@ -274,6 +277,9 @@ export const ssrTransformElement: NodeTransform = (node, context) => {
if (node.tag === 'textarea' && prop.name === 'value' && prop.value) {
rawChildrenMap.set(node, escapeHtml(prop.value.content))
} else if (!hasDynamicVBind) {
if (prop.name === 'key' || prop.name === 'ref') {
continue
}
// static prop
if (prop.name === 'class' && prop.value) {
staticClassBinding = JSON.stringify(prop.value.content)
Expand Down

0 comments on commit 5b62662

Please sign in to comment.