mirror of https://github.com/sveltejs/svelte
fix: always allow `setContext` before first await in component (#17031)
The previous check was flawed because EFFECT_RAN would be set by the time it is checked, since a promise in a parent component will cause a delay of the inner component being instantiated. Instead we have a new field on the component context checking if the component was already popped (if se we are indeed too late). Don't love it to have a field just for this but I don't see another way to reliably check it. Fixes #16629pull/17029/head
parent
4eb432e941
commit
b7f39b464a
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: always allow `setContext` before first await in component
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
import { tick } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
mode: ['client'],
|
||||||
|
async test() {
|
||||||
|
// else runtime_error is checked too soon
|
||||||
|
await tick();
|
||||||
|
},
|
||||||
|
runtime_error: 'set_context_after_init'
|
||||||
|
});
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
<script>
|
||||||
|
import { setContext } from 'svelte';
|
||||||
|
|
||||||
|
await Promise.resolve('hi');
|
||||||
|
|
||||||
|
setContext('key', 'value');
|
||||||
|
</script>
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { getContext } from "svelte";
|
||||||
|
|
||||||
|
let greeting = getContext("greeting");
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<p>{greeting}</p>
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { setContext } from "svelte";
|
||||||
|
import Inner from "./Inner.svelte";
|
||||||
|
|
||||||
|
setContext("greeting", "hi");
|
||||||
|
await Promise.resolve();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Inner />
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
import { tick } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
mode: ['client', 'async-server'],
|
||||||
|
ssrHtml: `<p>hi</p>`,
|
||||||
|
async test({ assert, target }) {
|
||||||
|
await tick();
|
||||||
|
assert.htmlEqual(target.innerHTML, '<p>hi</p>');
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import Outer from "./Outer.svelte";
|
||||||
|
|
||||||
|
await Promise.resolve();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Outer />
|
||||||
Loading…
Reference in new issue