mirror of https://github.com/sveltejs/svelte
fix: properly defer document title until async work is complete (#17158)
#17061 didn't properly handle the case where the title is sync but reactive and async work outside is pending. Handle this by creating a proper effect for the document title, and make sure to wait on it and flush it once ready. Fixes #17114pull/17159/head
parent
e238e6611e
commit
a0c7c3b327
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: properly defer document title until async work is complete
|
||||
@ -0,0 +1,15 @@
|
||||
<script>
|
||||
let { deferred, title } = $props();
|
||||
|
||||
function push() {
|
||||
const d = Promise.withResolvers();
|
||||
deferred.push(() => d.resolve());
|
||||
return d.promise;
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>{title}</title>
|
||||
</svelte:head>
|
||||
|
||||
<p>{await push()}</p>
|
||||
@ -0,0 +1,24 @@
|
||||
import { tick } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
async test({ assert, target }) {
|
||||
const [toggle, resolve] = target.querySelectorAll('button');
|
||||
toggle.click();
|
||||
await tick();
|
||||
assert.equal(window.document.title, '');
|
||||
|
||||
toggle.click();
|
||||
await tick();
|
||||
assert.equal(window.document.title, '');
|
||||
|
||||
toggle.click();
|
||||
await tick();
|
||||
assert.equal(window.document.title, '');
|
||||
|
||||
resolve.click();
|
||||
await tick();
|
||||
await tick();
|
||||
assert.equal(window.document.title, 'title');
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,12 @@
|
||||
<script>
|
||||
import Inner from './Inner.svelte';
|
||||
|
||||
let deferred = [];
|
||||
let show = $state(false);
|
||||
</script>
|
||||
|
||||
<button onclick={() => show = !show}>toggle</button>
|
||||
<button onclick={() => deferred.pop()()}>resolve</button>
|
||||
{#if show}
|
||||
<Inner {deferred} title="title" />
|
||||
{/if}
|
||||
Loading…
Reference in new issue