Skip to content

Commit 6207596

Browse files
authored
fix extraneous store subscription in SSR (#5929)
1 parent 0f3264e commit 6207596

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* Throw a parser error for `class:` directives with an empty class name ([#5858](https://github.com/sveltejs/svelte/issues/5858))
6+
* Fix extraneous store subscription in SSR mode ([#5883](https://github.com/sveltejs/svelte/issues/5883))
67
* Fix type inference for derived stores ([#5935](https://github.com/sveltejs/svelte/pull/5935))
78
* Make parameters of built-in animations and transitions optional ([#5936](https://github.com/sveltejs/svelte/pull/5936))
89
* Make `SvelteComponentDev` typings more forgiving ([#5937](https://github.com/sveltejs/svelte/pull/5937))

src/compiler/compile/render_ssr/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export default function ssr(
5151
return b`
5252
${component.compile_options.dev && b`@validate_store(${store_name}, '${store_name}');`}
5353
${`$$unsubscribe_${store_name}`} = @subscribe(${store_name}, #value => ${name} = #value)
54-
${store_name}.subscribe($$value => ${name} = $$value);
5554
`;
5655
});
5756
const reactive_store_unsubscriptions = reactive_stores.map(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { store } from './store.js';
2+
3+
export default {
4+
html: '<h1>0</h1>',
5+
before_test() {
6+
store.reset();
7+
},
8+
async test({ assert, target, component }) {
9+
store.set(42);
10+
11+
await Promise.resolve();
12+
13+
assert.htmlEqual(target.innerHTML, '<h1>42</h1>');
14+
15+
assert.equal(store.numberOfTimesSubscribeCalled(), 1);
16+
},
17+
test_ssr({ assert }) {
18+
assert.equal(store.numberOfTimesSubscribeCalled(), 1);
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { store } from './store';
3+
</script>
4+
5+
<h1>{$store}</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { writable } from '../../../../store';
2+
const _store = writable(0);
3+
let count = 0;
4+
5+
export const store = {
6+
..._store,
7+
subscribe(fn) {
8+
count++;
9+
return _store.subscribe(fn);
10+
},
11+
reset() {
12+
count = 0;
13+
_store.set(0);
14+
},
15+
numberOfTimesSubscribeCalled() {
16+
return count;
17+
}
18+
};

test/server-side-rendering/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ describe('ssr', () => {
201201
assert.htmlEqual(html, config.html);
202202
}
203203

204+
if (config.test_ssr) {
205+
config.test_ssr({ assert });
206+
}
207+
204208
if (config.after_test) config.after_test();
205209

206210
if (config.show) {

0 commit comments

Comments
 (0)