Skip to content

Commit

Permalink
fix extraneous store subscription in SSR (#5929)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored Jan 29, 2021
1 parent 0f3264e commit 6207596
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Throw a parser error for `class:` directives with an empty class name ([#5858](https://github.com/sveltejs/svelte/issues/5858))
* Fix extraneous store subscription in SSR mode ([#5883](https://github.com/sveltejs/svelte/issues/5883))
* Fix type inference for derived stores ([#5935](https://github.com/sveltejs/svelte/pull/5935))
* Make parameters of built-in animations and transitions optional ([#5936](https://github.com/sveltejs/svelte/pull/5936))
* Make `SvelteComponentDev` typings more forgiving ([#5937](https://github.com/sveltejs/svelte/pull/5937))
Expand Down
1 change: 0 additions & 1 deletion src/compiler/compile/render_ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export default function ssr(
return b`
${component.compile_options.dev && b`@validate_store(${store_name}, '${store_name}');`}
${`$$unsubscribe_${store_name}`} = @subscribe(${store_name}, #value => ${name} = #value)
${store_name}.subscribe($$value => ${name} = $$value);
`;
});
const reactive_store_unsubscriptions = reactive_stores.map(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { store } from './store.js';

export default {
html: '<h1>0</h1>',
before_test() {
store.reset();
},
async test({ assert, target, component }) {
store.set(42);

await Promise.resolve();

assert.htmlEqual(target.innerHTML, '<h1>42</h1>');

assert.equal(store.numberOfTimesSubscribeCalled(), 1);
},
test_ssr({ assert }) {
assert.equal(store.numberOfTimesSubscribeCalled(), 1);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { store } from './store';
</script>

<h1>{$store}</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { writable } from '../../../../store';
const _store = writable(0);
let count = 0;

export const store = {
..._store,
subscribe(fn) {
count++;
return _store.subscribe(fn);
},
reset() {
count = 0;
_store.set(0);
},
numberOfTimesSubscribeCalled() {
return count;
}
};
4 changes: 4 additions & 0 deletions test/server-side-rendering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ describe('ssr', () => {
assert.htmlEqual(html, config.html);
}

if (config.test_ssr) {
config.test_ssr({ assert });
}

if (config.after_test) config.after_test();

if (config.show) {
Expand Down

0 comments on commit 6207596

Please sign in to comment.