diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/TitleElement.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/TitleElement.js index a71deaf53953..67958ee60040 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/TitleElement.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/TitleElement.js @@ -14,11 +14,10 @@ export function TitleElement(node, context) { context.state ); - const statement = b.stmt(b.assignment('=', b.id('$.document.title'), value)); + context.state.init.push(b.stmt(b.call('$.title', value))); if (has_state) { + const statement = b.stmt(b.assignment('=', b.id('$.document.title'), value)); context.state.update.push(statement); - } else { - context.state.init.push(statement); } } diff --git a/packages/svelte/src/internal/client/dom/elements/misc.js b/packages/svelte/src/internal/client/dom/elements/misc.js index 61e513903f76..3e0207c302cb 100644 --- a/packages/svelte/src/internal/client/dom/elements/misc.js +++ b/packages/svelte/src/internal/client/dom/elements/misc.js @@ -1,3 +1,4 @@ +import { teardown } from '../../reactivity/effects.js'; import { hydrating } from '../hydration.js'; import { clear_text_content, get_first_child } from '../operations.js'; import { queue_micro_task } from '../task.js'; @@ -56,3 +57,14 @@ export function add_form_reset_listener() { ); } } + +/** + * @param {string} text + */ +export function title(text) { + const previous = document.title; + document.title = text; + teardown(() => { + document.title = previous; + }); +}