Browse Source

fix(compiler-ssr) ensure that ssrHelpers are unique after merge from parent context (#3269)

fix #3268
lidlanca 5 years ago
parent
commit
012dc5a303

+ 21 - 0
packages/compiler-ssr/__tests__/ssrElement.spec.ts

@@ -71,6 +71,27 @@ describe('ssr: element', () => {
       `)
     })
 
+    test("multiple _ssrInterpolate at parent and child import dependency once", () => {
+      expect( compile(`<div>{{ hello }}<textarea v-bind="a"></textarea></div>`).code)
+      .toMatchInlineSnapshot(`
+        "const { ssrRenderAttrs: _ssrRenderAttrs, ssrInterpolate: _ssrInterpolate } = require(\\"@vue/server-renderer\\")
+
+        return function ssrRender(_ctx, _push, _parent, _attrs) {
+          let _temp0
+
+          _push(\`<div\${
+            _ssrRenderAttrs(_attrs)
+          }>\${
+            _ssrInterpolate(_ctx.hello)
+          }<textarea\${
+            _ssrRenderAttrs(_temp0 = _ctx.a, \\"textarea\\")
+          }>\${
+            _ssrInterpolate((\\"value\\" in _temp0) ? _temp0.value : \\"\\")
+          }</textarea></div>\`)
+        }"
+      `);
+    });
+
     test('should pass tag to custom elements w/ dynamic v-bind', () => {
       expect(
         compile(`<my-foo v-bind="obj"></my-foo>`, {

+ 3 - 2
packages/compiler-ssr/src/ssrCodegenTransform.ts

@@ -56,10 +56,11 @@ export function ssrCodegenTransform(ast: RootNode, options: CompilerOptions) {
 
   // Finalize helpers.
   // We need to separate helpers imported from 'vue' vs. '@vue/server-renderer'
-  ast.ssrHelpers = [
+  ast.ssrHelpers = Array.from(new Set([  
     ...ast.helpers.filter(h => h in ssrHelpers),
     ...context.helpers
-  ]
+  ]))
+  
   ast.helpers = ast.helpers.filter(h => !(h in ssrHelpers))
 }