Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reset title element to previous value on removal #14116

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Nov 2, 2024

This started as an attempt to fix #7656, but I now ran into some questions where I'm not sure what's the best answer, to I'm pushing this up as a WIP

The questions:

  • is resetting the title to the previous value the correct move? or should we reset it to the empty string?
  • assuming we reset it to the previous value, what does happen if you have multiple title elements at the same time in different components, and the parent title is set to A, then the child title is set to B, then the parent title updates its title to A2. Upon removal, with this PR, it would go back too A. Is that fine?

Copy link

pkg-pr-new bot commented Nov 2, 2024

pnpm add https://pkg.pr.new/svelte@14116

commit: 3265021

@trueadm
Copy link
Contributor

trueadm commented Nov 3, 2024

I wonder what other frameworks do here? However, it's maybe the correct behaviour if another <title> set the value.

Copy link

changeset-bot bot commented Nov 4, 2024

⚠️ No Changeset found

Latest commit: 3265021

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-svelte-14116-svelte.vercel.app/

this is an automated message

Comment on lines +65 to +69
const previous = document.title;
document.title = text;
teardown(() => {
document.title = previous;
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't been able to break this, but it feels like we should put the setup in an effect so that there's no possibility of another component setting the title to a new value immediately before the teardown nukes it. might just be superstition

Suggested change
const previous = document.title;
document.title = text;
teardown(() => {
document.title = previous;
});
const previous = document.title;
effect(() => {
document.title = text;
return () => {
document.title = previous;
};
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The effect does nothing, since text is a static value; I kept the "add document.title = .. to the shared effect" part within the compiler which is why it's updating still. The assumption is that it's rare that people have a reactive title. But we can move it into here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not about reactivity, it's about preventing this sequence of events from being a possibility:

  1. component A is removed and component B is created at the same moment
  2. component B sets document.title
  3. component A is removed and reverts document.title to whatever it was before A was created

That may already be impossible, hence 'superstition'

@dummdidumm
Copy link
Member Author

What we could also do: Last one wins, always, i.e. it doesn't matter whether or not A is first and updates after B comes in, if B is after A then B always takes precedence. In DEV mode we could also warn then "hey there are multiple competing, and the last one which is currently X wins".

@trueadm
Copy link
Contributor

trueadm commented Nov 4, 2024

What we could also do: Last one wins, always, i.e. it doesn't matter whether or not A is first and updates after B comes in, if B is after A then B always takes precedence. In DEV mode we could also warn then "hey there are multiple competing, and the last one which is currently X wins".

That gets complicated with error boundaries though, no? What if you trigger an update and the title changes, but then an error occurs? Is this warning then useful here?

@Rich-Harris
Copy link
Member

That gets complicated with error boundaries though, no? What if you trigger an update and the title changes, but then an error occurs?

that feels like a good reason to put the assignment to document.title in an effect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

svelte:head does not remove title tag
3 participants