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

Child inside the svelte:head is not removed from DOM when the parent is unmounted #8202

Closed
tontonsb opened this issue Jan 17, 2023 · 4 comments

Comments

@tontonsb
Copy link

Describe the bug

If the following component is included and subsequently removed, the document head still contains whatever Child is.

<!-- MyComponent.svelte -->
<script>
    import Child from './Child.svelte'
</script>

<svelte:head>
    <Child />
</svelte:head>

I see a lot of similar bugs in this and Kit's issue history, but this still seems present in the latest release.

Reproduction

https://svelte.dev/repl/54395e0587fc49cebf931d11f57f22dc?version=3.55.1

Click the button in the REPL to repeatedly add and remove a Head element, which has the following HTML:

<svelte:head>
	<Title {title} />  <!-- this contains <title>{title}</title> -->
	<meta name="test">
</svelte:head>

You'll see that when Head is added, the title and meta are both added to DOM (inspect it or just look at the counters). When Head is removed the title still remains (but it shouldn't) while meta is correctly cleaned up.

Logs

No response

System Info

Svelte 3.55.1

Severity

annoyance

@tontonsb
Copy link
Author

tontonsb commented Jan 17, 2023

Btw here's a workaround for the repl case. Remove the Title component manually before the Head is removed.

<!-- Head.svelte -->
<script>
	import Title from './Title.svelte'
	import {onDestroy} from 'svelte'
	
	export let title = ''
	
	let enabled = true
	onDestroy(() => enabled = false)
</script>

<svelte:head>
	{#if enabled}
	<Title {title} />
	{/if}
	<meta name="test">
</svelte:head>

Trying the same cleanup inside Title doesn't seem to help, even though onDestroy is called.

@dummdidumm
Copy link
Member

Closing as duplicate of #7656

@dummdidumm dummdidumm closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2024
@tontonsb
Copy link
Author

tontonsb commented Nov 2, 2024

@dummdidumm it's not a duplicate, it was another issue.

This issue was about child components inside head. They are never removed, reinserting the component duplicates the contents of the component (and makes additional copy of contents on every reinsert) and it doesn't matter if you put a title tag or anything else in the Title.svelte component here: https://svelte.dev/repl/54395e0587fc49cebf931d11f57f22dc?version=3.55.1

However, the issue has been fixed in Svelte 5: https://svelte.dev/playground/54395e0587fc49cebf931d11f57f22dc?version=5.1.3

So it is still probably fine to close this.

The #7656 issue on the other hand is some quirk related to title behaving differently than meta. But it doesn't make additional copies of title, it just leaves it in when it should be removed. And that problem appears to persist in Svelte 5: https://svelte.dev/playground/2ecf81b43d0d4b2eaa02d80adf803929?version=5.1.3

@dummdidumm
Copy link
Member

Thanks for the explanation, and sorry for the confusion. Keeping this closed because as you say it's fixed in Svelte 5

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

No branches or pull requests

2 participants