I want to add a head title suffix like - mywebsite
on each Svelte page.
I'm struggling finding how to do it with ease and simplicity.
On the svelte website source, we can see they do it manually: https://github.com/sveltejs/svelte/blob/1273f978084aaf4d7c697b2fb456314839c3c90d/site/src/routes/docs/index.svelte#L15
I started creating a component like this:
<script>
export let title = false;
</script>
<svelte:head>
<title>
{#if title}
{title} • WebSite
{:else}
Website • Home suffix
{/if}
</title>
</svelte:head>
But:
<title> can only contain text and {tags}svelte(illegal-structure)
errorHow can an achieve what I want to do?
Since <title>
only can contain text or {tags}
you could compute the document title with the ternary operator.
Example
<script>
export let title = false;
</script>
<svelte:head>
<title>{title ? `${title} • WebSite` : 'Website • Home suffix'}</title>
</svelte:head>
While using a ternary inside of the markup works just fine, reactivity statements may help clean it up:
<script>
export let title = false;
$: fullTitle = title
? `${title} • WebSite`
: 'Website • Home suffix';
</script>
<svelte:head>
<title>{fullTitle}</title>
</svelte:head>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With