From b06e4356848ab3375797e634fd6c6114a9643252 Mon Sep 17 00:00:00 2001 From: Emil Tholin Date: Tue, 10 Jan 2023 09:39:50 +0100 Subject: [PATCH] [fix] Spread component props immutably during SSR (#8176) By passing an empty object literal as first argument to Object.assign we can avoid having objects spread as props on a component being mutated during SSR. Fixes #8171 --- .../render_ssr/handlers/InlineComponent.ts | 2 +- .../spread-component-immutable/Widget.svelte | 0 .../samples/spread-component-immutable/_config.js | 15 +++++++++++++++ .../spread-component-immutable/main.svelte | 7 +++++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/spread-component-immutable/Widget.svelte create mode 100644 test/runtime/samples/spread-component-immutable/_config.js create mode 100644 test/runtime/samples/spread-component-immutable/main.svelte diff --git a/src/compiler/compile/render_ssr/handlers/InlineComponent.ts b/src/compiler/compile/render_ssr/handlers/InlineComponent.ts index 8287b46268..73d31940e2 100644 --- a/src/compiler/compile/render_ssr/handlers/InlineComponent.ts +++ b/src/compiler/compile/render_ssr/handlers/InlineComponent.ts @@ -36,7 +36,7 @@ export default function(node: InlineComponent, renderer: Renderer, options: Rend let props; if (uses_spread) { - props = x`@_Object.assign(${ + props = x`@_Object.assign({}, ${ node.attributes .map(attribute => { if (attribute.is_spread) { diff --git a/test/runtime/samples/spread-component-immutable/Widget.svelte b/test/runtime/samples/spread-component-immutable/Widget.svelte new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/runtime/samples/spread-component-immutable/_config.js b/test/runtime/samples/spread-component-immutable/_config.js new file mode 100644 index 0000000000..634fac24c1 --- /dev/null +++ b/test/runtime/samples/spread-component-immutable/_config.js @@ -0,0 +1,15 @@ +const obj = { + x: 1, + y: 2, + z: 3 +}; + +export default { + props: { + obj + }, + + test({ assert }) { + assert.deepEqual(obj, { x: 1, y: 2, z: 3 }); + } +}; diff --git a/test/runtime/samples/spread-component-immutable/main.svelte b/test/runtime/samples/spread-component-immutable/main.svelte new file mode 100644 index 0000000000..44e0954856 --- /dev/null +++ b/test/runtime/samples/spread-component-immutable/main.svelte @@ -0,0 +1,7 @@ + + +