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

<svelte:head> creates duplicate elements on hydration #1607

Closed
morleytatro opened this issue Jul 21, 2018 · 32 comments · Fixed by #4082
Closed

<svelte:head> creates duplicate elements on hydration #1607

morleytatro opened this issue Jul 21, 2018 · 32 comments · Fixed by #4082
Labels

Comments

@morleytatro
Copy link

When hydrating SSR-generated Svelte components, the svelte:head component creates duplicate elements. This seems to be true for all elements other than title. Please see the following repo:
https://github.com/morleytatro/svelte-head-test

Steps to repro:

  1. npm install
  2. npm run start
  3. Visit localhost:3333 in your browser.
  4. Check the two meta tags and the canonical link element.
@morleytatro
Copy link
Author

I just checked, and this issue is plaguing Sapper as well. I've noticed that other frameworks add a data attribute to specify which elements are tracked. Would it help to add a data-svelte-head attribute to head elements to properly hydrate them on initial page load?

@jackyef
Copy link
Contributor

jackyef commented Apr 15, 2019

Hi, is there any fix for this yet? We are using version 2.16.0 and are having this issue as well.

@evdama
Copy link

evdama commented Jun 29, 2019

I've been doing Sapper lighthouse tests because I want perfect SEO. Lighthouse told me to add <meta name="Description" content="Put your description here."> to each page. So I did, using <svelte:head> on each page e.g.

<svelte:head>
	<title>About</title>
	<meta name="description" content="foo bar baz">
</svelte:head>

I just added the meta line, all else was there before, on each page. After adding the meta line I'm seeing two issues:

  • doesn't hot reload anymore in dev mode, I have to manually click reload
  • it only puts the first meta inside head (currently index.svelte), but then on every other page i.e. no matter what page I visit, it's always the same meta tag incl. name and content; in fact, it seems head doesn't change at all or at least not the meta with name and content.
  • title works fine though, renders in dev mode and is also different on every page

@evdama
Copy link

evdama commented Jul 1, 2019

PR for this issue sveltejs/sapper-template#158

@kuechlerm
Copy link

I cannot reproduce the problem with the current REPL (3.16.4).
Can this issue be closed?

@Conduitry
Copy link
Member

What were you doing to test? The REPL doesn't perform any hydration.

@kuechlerm
Copy link

@Conduitry Sorry, I overlooked that point.

@Conduitry
Copy link
Member

Presumably the way to address this would be to have the SSR code mark or delimit its <head> elements in some way so that the client-side hydration code can see what needs to be replaced.

One fairly obvious way to do this would be to a unique hash-based string for each component that's added as an attribute to each element in the component's SSR'd contribution to the <head>. This won't work with {@html} though, where we can't see the elements at compile time. I don't think we want to have the SSR code parsing through the HTML and adding the attributes at runtime.

Another option is some sort of wrapping element, or separating elements before and after this component's <head> portion. We need to now run afoul of HTML rules though, and I think there's a pretty limited number of tags that are allowed in the head. Perhaps there's some innocuous tag (maybe a <meta> tag with no name or content?) that we can stick in there, and then the hydration code is responsible for looking for these.

@nsivertsen
Copy link

react-helmet uses dataset attributes for this:

<meta data-react-helmet="true" property="og:type" content="website">

@Conduitry
Copy link
Member

Fixed (finally!) in 3.17.0.

@jhwheeler
Copy link

jhwheeler commented Sep 29, 2020

@Conduitry Unfortunately this is happening again. We're using Sapper 0.28.9 and Svelte 3.28.0 (it was happening with 0.28.8 and 3.27.0 as well; I upgraded to see if that would fix it, but no luck).

I'm having trouble pinpointing exactly what the pattern is, since not every route's <svelte:head> is duplicated on the client side. In our main _layout file the entire contents of <svelte:head> are duplicated, and in some individual routes; but not every single one.

I'll try to create a repro repo and post it tomorrow.

@rawoke083
Copy link

@Conduitry Unfortunately this is happening again. We're using Sapper 0.28.9 and Svelte 3.28.0 (it was happening with 0.28.8 and 3.27.0 as well; I upgraded to see if that would fix it, but no luck).

Confirmed me as well.
Using Svelte 3.280 and Sapper 0.28.9

@mattpilott
Copy link
Contributor

Im seeing this too when i do:

<svelte:head>
  {@html page.yoast_head}
</svelte:head>

@benmccann
Copy link
Member

@matt3224 no need to post the same comment on multiple issues. you can simply give a thumbs up on the issue if you'd like to indicate it's one you care about

@jdgaravito
Copy link

any new on this? got all my meta tags duplicated

@jhwheeler
Copy link

This issue should be re-opened 🙏

@benmccann
Copy link
Member

I have a SvelteKit app deployed to production at https://c3.ventures and am not seeing this there. Maybe it's just an issue with Sapper?

@Maggi64
Copy link

Maggi64 commented Apr 11, 2021

Sveltekit also solved this for me

@benmccann
Copy link
Member

SvelteKit uses vite-plugin-svelte which automatically sets the hydratable flag to true. If you're hitting this you may want to check that you've set that flag appropriately

@angelmeraz
Copy link

Still experiencing this with sapper v0.29.2 & svelte v3.42.1

@keeganpoppen
Copy link

i am also having this problem in most recent svelte version, unfortunately. only with <script> tags, not with <style>. will keep digging to see if i can find out what is going on...

@abhinandan-chakraborty
Copy link

Tested on SvelteKit Project: Svelte v3.44.0 + SvelteKit v1.0.0-next.260
Solution:

  1. [optional step] Created a separate Meta component with all fields (including <svelte:head> tag inside this component)
  2. [optional step] Added this Meta component in all relevant routes
  3. Now go to "App.html" in src directory -> Replace all the tags in <head> tag except for %svelte.head%
    That's it. now only the tags from your Meta component will appear

@winston0410
Copy link

SvelteKit uses vite-plugin-svelte which automatically sets the hydratable flag to true. If you're hitting this you may want to check that you've set that flag appropriately

@benmccann Do you mean that we should disable hydration in that case to prevent duplication?

@winston0410
Copy link

I am seeing duplication for the generated json-ld, and I cannot find a solution right now. Any viable solution for this?

I generate the json as string:

export const mkSchema = (thing: Schema) => {
	return `<script type="application/ld+json">${JSON.stringify(thing, null, 2)}</script>`;
};

And I am using it in __layout.svelte like this:

<svelte:head>
    {@html mkSchema(orgSchema)}
    {@html mkSchema(webpageSchema)}
    {@html mkSchema(websiteSchema)}
</svelte:head>

@benmccann
Copy link
Member

@benmccann Do you mean that we should disable hydration in that case to prevent duplication?

No. I meant that if you're using Svelte without SvelteKit you may want to make sure your project is configured appropriately

@avoonix
Copy link

avoonix commented May 7, 2022

I'm using svelte 3.48.0 and sveltekit [email protected] and this issue still occurred when the meta elements were not direct children of svelte:head:

<svelte:head>
	<Meta title={variables.siteName} {url} {description} {image} /> <!-- contains multiple meta elements; this causes duplication --> 
</svelte:head>
<svelte:head>
	<meta itemprop="name" content={title} /> <!-- this works as expected -->
	<meta itemprop="description" content={description} />
</svelte:head>

@rawoke083
Copy link

rawoke083 commented May 7, 2022 via email

@benmccann
Copy link
Member

@avoonix can you file a new issue? I'm afraid your comment would get lost here given how large this thread is

@monarchwadia
Copy link

Could we please reopen this? This is still an issue in Oct 2022.

@dummdidumm
Copy link
Member

Please open a new issue with a proper reproduction, it's maybe the same outcome but likely not the same root cause.

@monarchwadia
Copy link

@dummdidumm #7973

@0x366
Copy link

0x366 commented Jan 17, 2023

related svelte-meta-tags#556

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

Successfully merging a pull request may close this issue.