Skip to content

Reset document.title on component unmount #14789

@ottomated

Description

@ottomated

Describe the problem

I expect this example to work. That is, when a component is unmounted that contains the following code:

<svelte:head>
  <title>Title</title>
</svelte:head>

it should reset the document.title to the previous value.

This is particularly relevant when using shallow routing, where rendering a page component manually is recommended.

Describe the proposed solution

Something like

$.head(($$anchor) => {
  const previous_title = $.document.title;
  $.teardown(() => {
    $.document.title = previous_title;
  });
  $.document.title = `title`;
});

I'm not sure whether there would need to be some global queue of titles, i.e.

const title_queue = [];
function set_title(title) {
  render_effect(() => {
    title_queue.push(document.title);
    document.title = title;
    return () => {
      if (title_queue.length === 0) return;
      document.title = title_queue.pop();
    };
  });
}

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions