Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/compiler-ssr/__tests__/ssrElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-ssr/src/transforms/ssrInjectCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions packages/compiler-ssr/src/transforms/ssrTransformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 18 additions & 0 deletions packages/server-renderer/__tests__/renderToString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down