question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSONLD elements preserved between page change with SvelteKit

See original GitHub issue

Describe the bug I have multiple pages with FAQs and products. When I navigate from page A with FAQ-A and products-A to page B, I get both FAQ-A, FAQ-B and products-A and products-B in my head markup.

The setup is the follwing:

I’ve created a helper function to generate the JSONLD content


export function generateProductJSONLD(service: Service, image: string): Partial<JsonLdProps> {
  const priceValidUntil = new Date()
  priceValidUntil.setFullYear(priceValidUntil.getFullYear() + 1)
  const jsonLD: Partial<JsonLdProps> = {
    schema: {
      "@context": "https://schema.org",
      "@type": "Product",
      name: service.name,
      description: service.description,
      image,
      url: '<redacted_base_url>' + service.selfLink,
      brand: {
        "@type": "Organization",
        "name": "<redacted>"
      },
      sku: service.name,
      offers: {
        "@type": "Offer",
        price: service.price,
        priceValidUntil,
        url: '<redacted_base_url>' + service.selfLink,
        priceCurrency: "NZD",
        availability: "https://schema.org/InStock",
      }
    }
  }

  return jsonLD
}


export function generateFAQJSONLD(faqs: FAQ[]): Partial<JsonLdProps> {
  const jsonLD: Partial<JsonLdProps> = {
    schema: {
      "@context": "https://schema.org",
      "@type": "FAQPage",
      mainEntity: faqs.map(faq => ({
        "@type": "Question",
        name: faq.question,
        acceptedAnswer: {
          "@type": "Answer",
          text: faq.answer
        }
      }))
    }
  }
  return jsonLD
}

This is then begin used in page A and page B

src/routes/services/pageA && src/routes/services/pageB:

<svelte:head>
	<title>Title</title>
</svelte:head>
<JsonLd {...generateFAQJSONLD(faqs)} />
<MetaTags {...generateMetaTags({ title: 'Title', description: "Description", url: '<redacted>' })} />

...HTML code...

{#each services.massage as service, idx}
<JsonLd {...generateProductJSONLD(service, serviceBanners[service.id])} />
....HTML code...

First time I navigate to page A, everything looks OK, but if I navigate to page B (using a simple html element, no fancy navigation), I then get all JSONLDs from page A and page B. Interestingly enough, the MetaTags are updated properly.

Reproduction You can see it live at https://amelies.co.nz navigate to services/massage, then using the menu, navigate to services/skin-and-makeup

Please let me know if I’m doing something zrong, or if there is a bug somewhere.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Pixcellcommented, May 8, 2022

Seems it has fixed it! Thank you!

Although now google search console is giving me the error that the element has no name. Probably nothing to do with you guys, but if you have an idea, I’d be keen to hear it.

Anywya, thanks for the quick response and fix!

1reaction
Pixcellcommented, May 7, 2022

Thanks for letting me know, I’ll give it a try next week and let you know

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hydration issue when rendering dynamic head element #1035
Describe the bug When using the @sveltejs/adapter-static and appending a script tag with application/ld+json type to svelte:head, ...
Read more >
SvelteKit FAQ Page SEO: add JSON-LD Schema | Rodney Lab
We can treat this just like any other array in JavaScript. In lines 23 – 27 we iterate over the elements of the...
Read more >
Including json+ld structured data in Svelte
Easily add json+ld structured data to pages build in Svelte, with TypeScript type checking!
Read more >
How to persist _layout state between pages in Sveltekit
Best practice is to create a file containing your stored variables. In this case i called the file store.js inside the lib folder....
Read more >
SEO • Docs • SvelteKit
Every page should have well-written and unique <title> and <meta ... you will need to explicitly preserve ld+json data (this may change in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found