From a251d2f073dbf8dd86fe34adc1aa143491920203 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Tue, 26 Jan 2021 09:12:09 +0800 Subject: [PATCH 1/4] fix remove extra store subscription statement --- src/compiler/compile/render_ssr/index.ts | 1 - .../_config.js | 20 +++++++++++++++++++ .../main.svelte | 5 +++++ .../store.js | 18 +++++++++++++++++ test/server-side-rendering/index.ts | 4 ++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/_config.js create mode 100644 test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/main.svelte create mode 100644 test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/store.js diff --git a/src/compiler/compile/render_ssr/index.ts b/src/compiler/compile/render_ssr/index.ts index e00e698d6f95..6a4647546384 100644 --- a/src/compiler/compile/render_ssr/index.ts +++ b/src/compiler/compile/render_ssr/index.ts @@ -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( diff --git a/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/_config.js b/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/_config.js new file mode 100644 index 000000000000..08a3435a4b05 --- /dev/null +++ b/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/_config.js @@ -0,0 +1,20 @@ +import { store } from './store.js'; + +export default { + html: '

0

', + before_test() { + store.reset(); + }, + async test({ assert, target, component }) { + store.set(42); + + await Promise.resolve(); + + assert.htmlEqual(target.innerHTML, '

42

'); + + assert.equal(store.numberOfTimesSubscribeCalled(), 1); + }, + test_ssr({ assert }) { + assert.equal(store.numberOfTimesSubscribeCalled(), 1); + } +}; diff --git a/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/main.svelte b/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/main.svelte new file mode 100644 index 000000000000..3486dedaf1e5 --- /dev/null +++ b/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/main.svelte @@ -0,0 +1,5 @@ + + +

{$store}

diff --git a/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/store.js b/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/store.js new file mode 100644 index 000000000000..ea0f63714b71 --- /dev/null +++ b/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/store.js @@ -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; + } +}; diff --git a/test/server-side-rendering/index.ts b/test/server-side-rendering/index.ts index ef2a88ecbe72..e4655621123a 100644 --- a/test/server-side-rendering/index.ts +++ b/test/server-side-rendering/index.ts @@ -197,6 +197,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) { From 2f544cd9fb58f033adfdb30fd4f010310b9a41df Mon Sep 17 00:00:00 2001 From: Conduitry Date: Fri, 29 Jan 2021 11:02:57 -0500 Subject: [PATCH 2/4] fix indentation --- .../store.js | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/store.js b/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/store.js index ea0f63714b71..3917a96b1759 100644 --- a/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/store.js +++ b/test/runtime/samples/reactive-assignment-in-complex-declaration-with-store-3/store.js @@ -3,16 +3,16 @@ const _store = writable(0); let count = 0; export const store = { - ..._store, + ..._store, subscribe(fn) { - count++; + count++; return _store.subscribe(fn); - }, - reset() { - count = 0; - _store.set(0); - }, - numberOfTimesSubscribeCalled() { - return count; - } + }, + reset() { + count = 0; + _store.set(0); + }, + numberOfTimesSubscribeCalled() { + return count; + } }; From 755b74d6632494f723640d839a54755ed0f5fd23 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Fri, 29 Jan 2021 11:03:54 -0500 Subject: [PATCH 3/4] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fffeb964beda..87cae3206ed2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 duplicate 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)) From 19ee4df6534ac7a700fefc3d837bbc9debf7aa7e Mon Sep 17 00:00:00 2001 From: Conduitry Date: Fri, 29 Jan 2021 11:06:41 -0500 Subject: [PATCH 4/4] duplicate -> extraneous --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87cae3206ed2..a2b960d6b039 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased * Throw a parser error for `class:` directives with an empty class name ([#5858](https://github.com/sveltejs/svelte/issues/5858)) -* Fix duplicate store subscription in SSR mode ([#5883](https://github.com/sveltejs/svelte/issues/5883)) +* 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))