-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
base: main
Are you sure you want to change the base?
Conversation
commit: |
I wonder what other frameworks do here? However, it's maybe the correct behaviour if another |
|
preview: https://svelte-dev-git-preview-svelte-14116-svelte.vercel.app/ this is an automated message |
const previous = document.title; | ||
document.title = text; | ||
teardown(() => { | ||
document.title = previous; | ||
}); |
There was a problem hiding this comment.
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
const previous = document.title; | |
document.title = text; | |
teardown(() => { | |
document.title = previous; | |
}); | |
const previous = document.title; | |
effect(() => { | |
document.title = text; | |
return () => { | |
document.title = previous; | |
}; | |
}); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
- component A is removed and component B is created at the same moment
- component B sets
document.title
- component A is removed and reverts
document.title
to whatever it was beforeA
was created
That may already be impossible, hence 'superstition'
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? |
that feels like a good reason to put the assignment to |
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:
title
elements at the same time in different components, and the parent title is set toA
, then the child title is set toB
, then the parent title updates its title toA2
. Upon removal, with this PR, it would go back tooA
. Is that fine?