fix(compiler-ssr): fix SSR issue when dynamic and static class co-exist (#2354)

This commit is contained in:
Mathieu TUDISCO 2020-10-13 22:25:15 +02:00 committed by GitHub
parent 6a554feb13
commit 8539c0bf32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 5 deletions

View File

@ -112,6 +112,15 @@ describe('ssr: element', () => {
`)
})
test('v-bind:class + static class', () => {
expect(getCompiledString(`<div :class="bar" class="foo"></div>`))
.toMatchInlineSnapshot(`
"\`<div class=\\"\${
_ssrRenderClass([_ctx.bar, \\"foo\\"])
}\\"></div>\`"
`)
})
test('v-bind:style', () => {
expect(getCompiledString(`<div id="foo" :style="bar"></div>`))
.toMatchInlineSnapshot(`

View File

@ -15,8 +15,8 @@ export const ssrInjectCssVars: NodeTransform = (node, context) => {
return
}
// _cssVars is initailized once per render function
// the code is injected in ssrCodegenTrasnform when creating the
// _cssVars is initialized once per render function
// the code is injected in ssrCodegenTransform when creating the
// ssr transform context
if (node.type === NodeTypes.ROOT) {
context.identifiers._cssVars = 1

View File

@ -324,9 +324,10 @@ function removeStaticBinding(
tag: TemplateLiteral['elements'],
binding: string
) {
const i = tag.findIndex(
e => typeof e === 'string' && e.startsWith(` ${binding}=`)
)
const regExp = new RegExp(`^ ${binding}=".+"$`)
const i = tag.findIndex(e => typeof e === 'string' && regExp.test(e))
if (i > -1) {
tag.splice(i, 1)
}

View File

@ -142,6 +142,24 @@ describe('ssr: renderToString', () => {
)
})
test('template components with dynamic class attribute after static', async () => {
const app = createApp({
template: `<div><div class="child" :class="'dynamic'"></div></div>`
})
expect(await renderToString(app)).toBe(
`<div><div class="dynamic child"></div></div>`
)
})
test('template components with dynamic class attribute before static', async () => {
const app = createApp({
template: `<div><div :class="'dynamic'" class="child"></div></div>`
})
expect(await renderToString(app)).toBe(
`<div><div class="dynamic child"></div></div>`
)
})
test('mixing optimized / vnode / template components', async () => {
const OptimizedChild = {
props: ['msg'],