Skip to content

Commit 8539c0b

Browse files
authored
fix(compiler-ssr): fix SSR issue when dynamic and static class co-exist (#2354)
1 parent 6a554fe commit 8539c0b

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ describe('ssr: element', () => {
112112
`)
113113
})
114114

115+
test('v-bind:class + static class', () => {
116+
expect(getCompiledString(`<div :class="bar" class="foo"></div>`))
117+
.toMatchInlineSnapshot(`
118+
"\`<div class=\\"\${
119+
_ssrRenderClass([_ctx.bar, \\"foo\\"])
120+
}\\"></div>\`"
121+
`)
122+
})
123+
115124
test('v-bind:style', () => {
116125
expect(getCompiledString(`<div id="foo" :style="bar"></div>`))
117126
.toMatchInlineSnapshot(`

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export const ssrInjectCssVars: NodeTransform = (node, context) => {
1515
return
1616
}
1717

18-
// _cssVars is initailized once per render function
19-
// the code is injected in ssrCodegenTrasnform when creating the
18+
// _cssVars is initialized once per render function
19+
// the code is injected in ssrCodegenTransform when creating the
2020
// ssr transform context
2121
if (node.type === NodeTypes.ROOT) {
2222
context.identifiers._cssVars = 1

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,10 @@ function removeStaticBinding(
324324
tag: TemplateLiteral['elements'],
325325
binding: string
326326
) {
327-
const i = tag.findIndex(
328-
e => typeof e === 'string' && e.startsWith(` ${binding}=`)
329-
)
327+
const regExp = new RegExp(`^ ${binding}=".+"$`)
328+
329+
const i = tag.findIndex(e => typeof e === 'string' && regExp.test(e))
330+
330331
if (i > -1) {
331332
tag.splice(i, 1)
332333
}

packages/server-renderer/__tests__/renderToString.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ describe('ssr: renderToString', () => {
142142
)
143143
})
144144

145+
test('template components with dynamic class attribute after static', async () => {
146+
const app = createApp({
147+
template: `<div><div class="child" :class="'dynamic'"></div></div>`
148+
})
149+
expect(await renderToString(app)).toBe(
150+
`<div><div class="dynamic child"></div></div>`
151+
)
152+
})
153+
154+
test('template components with dynamic class attribute before static', async () => {
155+
const app = createApp({
156+
template: `<div><div :class="'dynamic'" class="child"></div></div>`
157+
})
158+
expect(await renderToString(app)).toBe(
159+
`<div><div class="dynamic child"></div></div>`
160+
)
161+
})
162+
145163
test('mixing optimized / vnode / template components', async () => {
146164
const OptimizedChild = {
147165
props: ['msg'],

0 commit comments

Comments
 (0)