KenMatsuo
KenMatsuo

Reputation: 11

Svelte SEO - Dynamic page title

First off, I have already checked this question, but it does not really answer my own question.

Here is my situation:

What I want to do now is tweak the "title" tag to make it dynamic, so that each page would be something like "Example | Page title".

What I have tried:

However, that's not working and I am not sure what I can do. Spoiler: I am still a bit of a noob and I am mostly tweaking a website that was designed by a developer, so I still have a lot to learn!

Thanks!

Upvotes: 0

Views: 588

Answers (1)

Corrl
Corrl

Reputation: 7699

One way would be to set the title dynamically inside +layout.svelte based on the $page.url.pathname and a 'lookup' function or object

<script>
    import {page} from '$app/stores'
    import SvelteSeo from "svelte-seo";

    function getPageTitle(pathname) {
        switch(pathname) {
            case '/' :
                return 'Home'
            case '/about':
                return 'About'
            default:
                return 'Default title'
        }
    }
</script>

<SvelteSeo
    title={getPageTitle($page.url.pathname)}
    ...
    />
<script>
    import {page} from '$app/stores'
    import SvelteSeo from "svelte-seo";

    const pageTitle = {
        '/' :   'Home',
        '/about': 'About',
    }
</script>

<SvelteSeo
    title={pageTitle[$page.url.pathname] ?? 'Default title'}
    ...
    />

Upvotes: 0

Related Questions