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.

Define default meta tags, title etc. and have the option to override it

See original GitHub issue

I want to define default meta tags, title etc. and have the option to override it per page. For example: I have 10 generic pages where I just want to use the default ones and then I have 5 pages where I want to set individual meta keywords, description, title etc.

In the app.html I can define those default tags, but if I set new tags via svelte:head it just appends those new tags and doesn’t replace the existing ones.

I also tried playing around with __layout.svelte (since this is somehow a root component at the moment) and Context API etc. but this didn’t work for me.

At the moment, the only solution I can find is creating a root component (e. g. App.svelte) and every page will import and call it. This root component will then export those meta tags and title and will have default values. Then I remove those tags from the app.html and pass it 1x from the root component with svelte:head

<App keywords="test1, test2, test3" description="this is a test" title="test title">
    The actual content of the site
</App>

But this seems to be cumbersome and would mean that in my case, I have to update more than 100 pages.

Is there a better way of doing that?

the easiest way of doing it would be to have an “override” mode for svelte:head:

<svelte:head|override>
    // override existing content
</svelte:head>

edit: there is also a realted Svelte issue: https://github.com/sveltejs/svelte/issues/4990

It would also be a nice solution to have some sort of named slots here (in the app.html, the __layout.svelte or somewhere else).

Using a store (https://github.com/svelte-society/sveltesociety-2021/blob/main/src/lib/stores/metatags.ts) can also be a solution, but the “override” solution seems to be easier to read and write.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:14
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
benmccanncommented, Feb 18, 2022

Yes, I think we can for now and see how well that solves the problem for people. We’ve also documented it here: https://kit.svelte.dev/docs/seo

5reactions
e0commented, Mar 15, 2022

With the recent addition of stuff, should this be closed now?

Following is a TypeScript example.

In src/app.d.ts:


declare namespace App {
  interface Locals {}

  interface Platform {}

  interface Session {}

  interface Stuff {
    description: string;
  }
}

In src/routes/my-page.svelte:

<script context="module" lang="ts">
  export const load = () => ({
    stuff: {
      description: 'Description from page'
    }
  });
</script>

In src/routes/__layout.svelte:

<script context="module" lang="ts">
  export const load = () => ({
    stuff: {
      description: 'Default description from layout'
    }
  });
</script>

<script lang="ts">
  import { page } from '$app/stores';
</script>

<svelte:head>
  <meta name="description" content={$page.stuff.description} />
</svelte:head>

Edit: Updated code based on https://github.com/sveltejs/kit/issues/1540#issuecomment-1068232079

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration | Metatag | Drupal Wiki guide on Drupal.org
Any values that are not overridden per object will automatically update should the defaults be updated.
Read more >
Meta Tags - KU CMS Guide
You have the option to override the default meta tags on each webpage of your site to control the metadata search engines and...
Read more >
override meta tags in masterpage at the page level - MSDN
I have a meta tag in master page and would like to override that meta tag in one of the pages. Any help...
Read more >
SEO Features - Support
You can do this in the Meta Tags Override field, also found under Default Values. Besides overriding all other default value meta tag...
Read more >
Open Graph Meta Tags: Everything You Need to Know - Ahrefs
If you don't set up an OG image and the post has a featured image, Yoast will use that by default. It will...
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