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 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function TitleElement(node, context) {
context.state
);

const statement = b.stmt(b.assignment('=', b.id('$.document.title'), value));
const statement = b.stmt(b.call('$.title', value));

if (has_state) {
context.state.update.push(statement);
Expand Down
15 changes: 15 additions & 0 deletions packages/svelte/src/internal/client/dom/elements/misc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { render_effect } 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';
Expand Down Expand Up @@ -56,3 +57,17 @@ export function add_form_reset_listener() {
);
}
}

/**
* @param {string} text
*/
export function title(text) {
render_effect(() => {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
const previous = document.title;
document.title = text;

return () => {
document.title = previous;
Copy link
Member Author

@dummdidumm dummdidumm Dec 6, 2024

Choose a reason for hiding this comment

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

This should handle another edge case - thoughts?

Suggested change
document.title = previous;
// Preserve later updates to title from other sources
if (document.title === text) {
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.

Which case would this prevent? It actually seems to me like this might make bugs more likely, since if a component had a <title> and also contained a component that set document.title in a user effect, it wouldn't revert either when the parent was removed

Copy link
Member Author

Choose a reason for hiding this comment

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

This handles the case where A sets the title, then B is mounted and sets the title, then A updates the title (in other words it contains something like <title>{foo}</title>), then B is removed - in that case we wouldn't want the title to be reverted back to what A had initially, we want to preserve the title.

Setting document.title in a user effect manually feels a bit like "working against the framework", you should just do that declaratively.

They are both edge cases either way, so I'm ok with not adding this suggestion.

Comment on lines +66 to +70
Copy link
Member Author

@dummdidumm dummdidumm Dec 10, 2024

Choose a reason for hiding this comment

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

We can also do this, would handle the case described in https://github.com/sveltejs/svelte/pull/14116/files#r1876874053

Suggested change
const previous = document.title;
document.title = text;
return () => {
document.title = previous;
const previous = document.title;
const own = {};
// @ts-expect-error
document._last_title_setter = own;
document.title = text;
return () => {
// @ts-expect-error
if (document._last_title_setter === own) {
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.

Looking at how the code is generated, we should switch it up anyway - right now if your title would contain dynamic content, you would set it two times on an update - once to revert to the previous value, then right away again for the updated value. Wasteful.

};
});
}
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export {
} from './dom/elements/attributes.js';
export { set_class, set_svg_class, set_mathml_class, toggle_class } from './dom/elements/class.js';
export { apply, event, delegate, replay_events } from './dom/elements/events.js';
export { autofocus, remove_textarea_child } from './dom/elements/misc.js';
export { autofocus, remove_textarea_child, title } from './dom/elements/misc.js';
export { set_style } from './dom/elements/style.js';
export { animation, transition } from './dom/elements/transitions.js';
export { bind_active_element } from './dom/elements/bindings/document.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default test({
async test({ assert, target, window }) {
const [btn1] = target.querySelectorAll('button');

assert.htmlEqual(window.document.head.innerHTML, ``);
assert.htmlEqual(window.document.head.innerHTML, `<title>initial</title>`);

flushSync(() => {
btn1.click();
Expand All @@ -17,6 +17,6 @@ export default test({
btn1.click();
});

assert.htmlEqual(window.document.head.innerHTML, `<title>hello world</title>`);
assert.htmlEqual(window.document.head.innerHTML, `<title>initial</title>`);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
}
</script>

<svelte:head>
<title>initial</title>
</svelte:head>

<button onclick={toggle}>
toggle
</button>
Expand Down
Loading