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

Using components in <svelte:head> creates duplicate elements on hydration #4533

Closed
domingues opened this issue Mar 10, 2020 · 20 comments
Closed

Comments

@domingues
Copy link

domingues commented Mar 10, 2020

If we include a component inside <svelte:head>:

<svelte:head>
<A/>
</svelte:head>

with A.svelte:

<meta property="x" content="y"/>

this meta is duplicated, not fixed by #4082.

If this is discouraged we should throw a warning.


P.S.:

Note that this is only a problem in the reported example. There is no problem when done this way:

<!-- Main.svelte -->
<A/>
<!-- A.svelte -->
<svelte:head>
<meta property="x" content="y"/>
</svelte:head>
@jhwheeler
Copy link

Same is occurring for me, this quite an issue for using Sapper for SEO-focused sites.

@rawoke083
Copy link

I feel this issue has been raised, fixed and re-appear a few times ? These days, whenever my SERPS or Rankings drop I have to go look at Sapper/Svelte first.

@domingues
Copy link
Author

Note that this is only a problem in the reported example. There is no problem when done this way:

<!-- Main.svelte -->
<A/>
<!-- A.svelte -->
<svelte:head>
<meta property="x" content="y"/>
</svelte:head>

@rawoke083
Copy link

rawoke083 commented Oct 1, 2020 via email

@jhwheeler
Copy link

Seems to be the same issue as #1607, since it also happens even if it's not in a component. But it doesn't happen for all elements, I'm having trouble figure out what the difference is.

@stephenhutchings
Copy link

If it helps, I'm currently working around this issue with the following.

import { afterUpdate } from "svelte"

afterUpdate(() => {
  Array.from(document.head.querySelectorAll("meta[data-svelte]"))
    .forEach(e => e.remove())
})

It's not a perfect solution, but works for me as all the pre-hydrated meta tags have data attributes, probably from an earlier solution to this issue.

@mattpilott
Copy link
Contributor

Im seeing this too when i do:

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

@Maggi64
Copy link

Maggi64 commented Dec 16, 2020

It indeed makes working with meta tags quite hard. They are essential for production news sites. Would love to see a fix.
Happy holidays guys 🎄
Appreciate the work.

@Shackless
Copy link

Shackless commented Mar 18, 2021

I have the same issue on my sapper page as well. If I use a Svelte component to render the <meta> and <title> tags within <svelte:head>, old entries stay and new ones get appended to the <head> section of the page on navigation.
However, if I just add the tags directly to <svelte:head> without wrapping them in a Svelte component, they get replaced correctly when navigating to another page.

@Annieloves
Copy link

I have the same issue on my sapper page as well. If I use a Svelte component to render the <meta> and <title> tags within <svelte:head>, old entries stay and new ones get appended to the <head> section of the page on navigation.
However, if I just add the tags directly to <svelte:head> without wrapping them in a Svelte component, they get replaced correctly when navigating to another page.

@jdgaravito
Copy link

jdgaravito commented Mar 28, 2021

Same on Sapper ^0.28.0 , i have a Meta component, to save me to write many times the metatags.

`<script>
export let metadata = {};
</script>

{#if metadata.title}
  <title>{metadata.title}</title>
  <meta name="title" content={metadata.title} />
  <meta property="og:title" content={metadata.title} />
  <meta property="twitter:title" content={metadata.title} />
{/if}

{#if metadata.description}
  <meta name="description" content="Page Description" />
  <meta property="og:description" content="Page Description" />
  <meta property="twitter:description" content="Page Description" />
{/if} 

{#if metadata.image}
  <meta property="og:image" content="/thumbnail.jpg" />
  <meta
    property="twitter:image"
    content="/thumbnail.jpg"
  />
{/if} 

{#if metadata.imageAlt}
  <meta property="og:image:alt" content={metadata.imageAlt} />
  <meta property="twitter:image:alt" content={metadata.imageAlt} />
{/if}

{#if metadata.url}
  <meta property="og:url" content={metadata.url} />
  <meta property="twitter:url" content={metadata.url} />
{/if}

   {#if metadata.type}
    <meta property="og:type" content={metadata.type} />
{/if}

<meta property="twitter:card" content={metadata.twitterCard || "summary_large_image"} />

when i use it for each page i got the metatag even 3 times
svelte:head
<Meta {metadata}/>

<title>{metadata.title}</title> `

This is hell :(

image

@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@endigma
Copy link

endigma commented Aug 31, 2022

This also affects ld+json tags, {@html} appears to count as a component.

@Conduitry
Copy link
Member

This should be fixed now in 3.51.0.

brightowltutoring added a commit to brightowltutoring/sveltekit that referenced this issue Dec 21, 2022
…as per conduitry on 'sveltejs/svelte#4533') ...  the meta tag duplication SSR issue is not fixed. With this update I have commented out the custom hook function 'metaTagFixWhenSSR' as well
@dongle-the-gadget
Copy link

I was able to reproduce it in 3.55 though.

@0x366
Copy link

0x366 commented Jan 19, 2023

Finally solved it! It wasn't working for me even in 3.55 simply because of Cloudflare. This is the explanation #8112

All you have to do is turnoff autominify and brotli in Cloudflare Speed -> Optimization
Screenshot 2023-01-19 at 13 24 31

@Shekhar-Zealous
Copy link

Shekhar-Zealous commented Sep 5, 2023

@0x366 I tried the same as you did, but the issue still persists on my website :(
I have disabled the Auto minify, Brotli option from Content optimization.

There are some options that are still enabled:
image
image

Do I also have to disable the above 👀? Or is there any other way to sort this out.

Any help would be appreciated.
Thanks.

@0x366
Copy link

0x366 commented Sep 5, 2023

I have both disabled, not sure if it's the case tho. I almost have all features disabled. Maybe it's worth a try to disable everything and see if it helps. And then turn on features one by one.

@Shekhar-Zealous
Copy link

@0x366 Thanks for the reply.

I checked disabling each configurations 1 by 1 still that didn't worked out.
At the end, I found out that the Auto minify configuration was used at another place in the Cloudflare dashboard from where I disabled the options and the issue was fixed right on :)

image

@sunxyw
Copy link

sunxyw commented Oct 31, 2023

Finally solved it! It wasn't working for me even in 3.55 simply because of Cloudflare. This is the explanation #8112

All you have to do is turnoff autominify and brotli in Cloudflare Speed -> Optimization Screenshot 2023-01-19 at 13 24 31

Thanks a lot! This bothered me all day.

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

No branches or pull requests