Skip to content

Commit

Permalink
do not declare variable for member assignment in reactive declaration…
Browse files Browse the repository at this point in the history
… in SSR (sveltejs#5251)
  • Loading branch information
tanhauhau authored and taylorzane committed Dec 17, 2020
1 parent 29288c8 commit 32ca324
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Svelte changelog

## Unreleased

* In SSR mode, do not automatically declare variables for reactive assignments to member expressions ([#5247](https://github.com/sveltejs/svelte/issues/5247))

## 3.24.1

* Prevent duplicate invalidation with certain two-way component bindings ([#3180](https://github.com/sveltejs/svelte/issues/3180), [#5117](https://github.com/sveltejs/svelte/issues/5117), [#5144](https://github.com/sveltejs/svelte/issues/5144))
Expand Down
39 changes: 11 additions & 28 deletions src/compiler/compile/render_ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { string_literal } from '../utils/stringify';
import Renderer from './Renderer';
import { INode as TemplateNode } from '../nodes/interfaces'; // TODO
import Text from '../nodes/Text';
import { extract_names } from '../utils/scope';
import { LabeledStatement, Statement, ExpressionStatement, AssignmentExpression, Node } from 'estree';
import { LabeledStatement, Statement, Node } from 'estree';

export default function ssr(
component: Component,
Expand Down Expand Up @@ -72,37 +71,17 @@ export default function ssr(
})
: [];

const injected = Array.from(component.injected_reactive_declaration_vars).filter(name => {
const variable = component.var_lookup.get(name);
return variable.injected;
});

const reactive_declarations = component.reactive_declarations.map(d => {
const body: Statement = (d.node as LabeledStatement).body;

let statement = b`${body}`;

if (d.declaration) {
const declared = extract_names(d.declaration);
const injected = declared.filter(name => {
return name[0] !== '$' && component.var_lookup.get(name).injected;
});

const self_dependencies = injected.filter(name => d.dependencies.has(name));

if (injected.length) {
// in some cases we need to do `let foo; [expression]`, in
// others we can do `let [expression]`
const separate = (
self_dependencies.length > 0 ||
declared.length > injected.length
);

const { left, right } = (body as ExpressionStatement).expression as AssignmentExpression;

statement = separate
? b`
${injected.map(name => b`let ${name};`)}
${statement}`
: b`
let ${left} = ${right}`;
}
} else { // TODO do not add label if it's not referenced
if (!d.declaration) { // TODO do not add label if it's not referenced
statement = b`$: { ${statement} }`;
}

Expand All @@ -119,6 +98,8 @@ export default function ssr(
${reactive_store_values}
${injected.map(name => b`let ${name};`)}
${reactive_declarations}
$$rendered = ${literal};
Expand All @@ -129,6 +110,8 @@ export default function ssr(
: b`
${reactive_store_values}
${injected.map(name => b`let ${name};`)}
${reactive_declarations}
return ${literal};`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
html: `
<h1>Hello world!</h1>
`,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
$: user = {};
$: user.name = 'world';
</script>

<h1>Hello {user.name}!</h1>

0 comments on commit 32ca324

Please sign in to comment.