fix: make template store subscriptions wait for the promise that assigns the store

pull/18582/head
Nic 2 weeks ago
parent 3dde011d3a
commit 17db18f708

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: block template store subscriptions on the promise that assigns the store

@ -1265,6 +1265,13 @@ function calculate_blockers(instance, analysis) {
binding.blocker = /** @type {typeof binding['blocker']} */ (blocker);
}
// a store subscription must wait on whatever blocks the store itself
for (const [name, binding] of instance.scope.declarations) {
if (binding.kind === 'store_sub') {
binding.blocker ??= instance.scope.get(name.slice(1))?.blocker ?? null;
}
}
}
/**

@ -0,0 +1,13 @@
import { tick } from 'svelte';
import { test } from '../../test';
// Tests that a store subscription only present in the template waits for the
// promise that assigns the store instead of subscribing to `undefined`.
export default test({
mode: ['client', 'hydrate', 'async-server'],
ssrHtml: '<p>hello</p>',
async test({ assert, target }) {
await tick();
assert.htmlEqual(target.innerHTML, '<p>hello</p>');
}
});

@ -0,0 +1,11 @@
<script>
import { writable } from 'svelte/store';
async function get_store() {
return writable('hello');
}
const store = await get_store();
</script>
<p>{$store}</p>
Loading…
Cancel
Save