MAIS AMOR POR FAVOR

SvelteKit SEO: Search Engine Optimisation Metadata

SvelteKit SEO:  Search Engine Optimisation Metadata

SvelteKit SEO: Search Engine Optimisation Metadata

SHARE:

πŸ§‘πŸ½β€πŸŽ“ Why is SvelteKit SEO Important?

Why write a post on SvelteKit SEO? We'll start by answering that question, before looking at some different types of SEO and then see how you can add Twitter SEO metadata as well as other metadata to your blog posts and other website pages. The aim will be to have our SvelteKit page shares on Twitter appear something like this:

SvelteKit SEO:  Search Engine Optimisation Metadata: screen capture of twitter post generated from page share
SvelteKit SEO: Twitter Post Share Screen Capture

We specify to Twitter which image we want to use as well as the title and description which appear below it.

Search Engine Optimisation (SEO) is all about getting your website pages to appear at the top of search engine results pages. Would you still write content if no-one would ever read it? Probably not. If your page does not appear in the top few search results, there is a risk that very few people will visit your page. This is because up to 90% of search engine users will not go beyond the first page of search results . Around a third of users will click the first result and 17%, the second. In short, Google needs to rank your post well for users to click on it.

We will look at some optimisations you can easily work into you SvelteKit site you get your posts and pages ranking higher. As well as that we look at how you can ensure your posts look good whenever visitors share them on social media and messaging apps.

πŸ€– Which Aspects of SvelteKit SEO are we focussingΒ on?

Although it is a new discipline, SEO is already has many aspects. All branches, however, boil down to providing a good user experience. Google want to connect their users to what they are looking for. A bad user experience translates into users leaving your page without interacting with it (bouncing). If you have a high bounce rate, why would Google want to put your site at the top of a search result list?

Some factors important for good user experience and SEO are:

  • good content: writing about what people are interested in (good keyword research can help here ),

  • good site structure: essentially this is about sufficient and relevant internal links, be they in breadcrumbs, navigation menus or post tags and categories. You need to optimise your site's structure so users can get to what they want in few clicks. On top more contextual links (links in the main text of a blog post, for example), help search engines determine what the post is all about. Knowing what a post is about gives the search engine more confidence in placing it higher in the results. Linking to (internal and external) related posts is important here.

  • technical SEO: fast loading pages (SvelteKit makes fast sites so you already have a head start here), security, accessibility are important for user experience and so improve search ranking. Technical SEO also encompasses getting rich results β€” we'll look at these in a bit more detail.

It is worth stressing that all of these aspects are important for ranking well. You should take a holistic approach for successful SEO. We will focus on the the more technical aspects though in this post as this is what is more specific to SvelteKit. In particular we'll look at what metadata you should include on your website's pages.

Rich Results

On Google, rich results will make your page stand out from others on a search results page. This might be through a HowTo or a Rich Snippet (including a thumbnail for a video as well as the usual result data). For these the most important metadata to include is SchemaOrg. Google and other search engines developed SchemaOrg. By including JSON objects matching the official schema, the search engine has a better idea of what is on the page and can be more confident in producing a rich snippet. Below is an example of a featured snippet , it appears above all other search results and Google give it prominence, with larger text:

SvelteKit SEO:  Example of a featured snippet from Google search results. Shows a Rodney Lab page in results given prominence over other search results in a featured snippet
SvelteKit SEO: Rich Results: Featured Snippet Screen Capture

We will return to SchemaOrg in more detail. To start though, let's take a look at more general SEO metadata and then Twitter metadata. If you are starting a new site clone the SvelteKit MDsveX starter and follow along. Alternatively you can follow along and paste the code snippets into your own project.

🧱 General Metadata

As a minimum, you should aim to include these four pieces of SEO metadata on your sites pages. Lighthouse will give you a warning if some of them are missing.

  1. Title: If your post has a short on-page title, augment the SEO title to include extra search terms. Although there is no character limit, Google sets a maximum display width of 600Β px .

  2. Meta Description: This is a longer description of the post. Google might not show it in search results (they might instead decide to show an extract from the page). When they do show it, they typically cut it to around 155 characters, though Google do not officially confirm this . Try to keep it within that limit.

  3. Robots Tag: The robots tag tells search engines you are happy for them to index your page. They need to index the page to include it in their search results. A follow directive tells search engines they can follow links on the page to discover new content. The max-snippet directive specifies the limit on the meta description length (in number of characters), a value of -1 indicates no limit.

  4. Site Language: This is important for site visitors using screen readers. It helps with pronunciation, especially when there are dialects or regional variations (Brazilian Portuguese vs. European Portuguese for example).

SvelteKit SEO Component

The SvelteKit blog starter includes those four pieces of metadata, just mentioned, in the SEO component. You can either use the SvelteKit MDsveX blog starter or just copy the component from below and adapt it to your own project. Here is the code for the component:

src/lib/components/SEO/index.svelte
svelte
1<script>
2 import { VERTICAL_LINE_ENTITY } from '$lib/constants/entities';
3 import website from '$lib/config/website';
4
5 const { siteLanguage, siteTitle } = website;
6
7 export let metadescription;
8 export let title;
9
10 const pageTitle = `${siteTitle} ${VERTICAL_LINE_ENTITY} ${title}`;
11</script>
12
13<svelte:head>
14 <title>{pageTitle}</title>
15 <meta name="description" content={metadescription} />
16 <meta
17 name="robots"
18 content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1"
19 />
20 <html lang={siteLanguage} />
21</svelte:head>

There are two more robots directives included here (not mentioned previously): max-image-preview and max-video-preview. We set image preview to large and put no limit on video preview, which is the number of seconds of video to play as a preview in search results.

You will see we need to pass in a metadescription and title as props when we use the component. Here is an example (from the BlogPost component):

src/lib/components/BlogPost.svelte
svelte
24<script>
25 const {
26 postTitle: title,
27 seoMetaDescription: metadescription,
28 slug,
29 } = post;
30</script>
31
32<SEO article={true} {slug} {title} {metadescription} {timeToRead} />
33<BannerImage {...bannerImageProps} />

In this case when we create a new post and add postTitle and seoMetaDescription to the frontmatter, these will automatically feed through to the html head section and be included in the post's metadata. We can check SvelteKit has included the metadata by cracking open the post in a browser and opening Developer Tools Inspector if you use Firefox or Elements in Chrome:

SvelteKit SEO:  Search Engine Optimisation Metadata: General Metadata Inspector
SvelteKit SEO: General Metadata: Inspector

In this screenshot we can see SvelteKit included the title and meta description as well as robots and language. General metadata is important but it will only get you so far. Next we take a look at Twitter metadata.

πŸ§‘πŸ½β€πŸŽ“ Twitter Metadata

You might not know that Slack uses some Twitter meta tags when users share your pages on the app. It's not just Twitter that uses it. If you didn't know that keep reading to learn even more about Twitter metadata!

We mentioned before it is important to tell social networks which image to use for each page. Not only will you avoid random, unrelated images from your page being picked, but also by providing an image with the right dimensions, you can avoid a poor crop, which can reflect badly on your brand.

I should point out because we include metadata in the pages, it won't matter whether the post is shared from someone clicking a link on your site or just by pasting the link into one of their tweets. Either way, Twitter will use your chosen image and description.

Twitter Cards

When someone shares your page, Twitter generates a Card to show it. There are different types, but the Summary Card with Large Image serves many purposes so we will use that. To discover the other types of Twitter sharing card, see the Twitter Developer Docs .

For our chosen type of card we should provide an image which is 800Β px Γ— 418Β px. The specification changes with time, so if you are reading this some point well into my future, check the latest specs!

Anyway, here is a component which we can use to add the Twitter metadata to our page:

src/lib/components/SEO/Twitter.svelte
svelte
1<script>
2 export let article = false;
3 export let author;
4 export let twitterUsername;
5 export let image;
6 export let metadescription;
7 export let pageTitle;
8 export let timeToRead = 0;
9 export let url;
10</script>
11
12<svelte:head>
13 <meta name="twitter:card" content="summary_large_image" />
14 <meta name="twitter:title" content={pageTitle} />
15 <meta name="twitter:description" content={metadescription} />
16 <meta name="twitter:url" content={url} />
17 {#if image}
18 <meta name="twitter:image" content={image.url} />
19 <meta name="twitter:image:alt" content={image.alt} />
20 {/if}
21 {#if twitterUsername}
22 <meta name="twitter:creator" content={`@${twitterUsername}`} />
23 <meta name="twitter:site" content={`@${twitterUsername}`} />
24 {/if}
25 <meta name="twitter:label1" content="Written by" />
26 <meta name="twitter:data1" content={author} />
27 {#if article && timeToRead > 0}
28 <meta name="twitter:label2" content="Est. reading time" />
29 <meta name="twitter:data2" content={timeToRead !== 1 ? `${timeToRead} minutes` : '1 minute'} />
30 {/if}
31</svelte:head>

Twitter metadata is based on the OpenGraph standard though they don't follow the standard to the letter. While in OpenGraph we would use:

<meta property="og:title" content={pageTitle} />

for Twitter we use:

<meta name="twitter:title" content={pageTitle} />

Twitter meta falls back to OpenGraph meta where the equivalent OpenGraph tag is present. So this means if we want to use the same title for sharing on Twitter as other apps, we can drop the twitter:title meta and just include the OpenGraph equivalent (og:title), which Twitter will pick up.

It's the last two tags (lines 25–30) which generate metadata that appears when your post is shared on Slack:

SvelteKit SEO: Slack: Webpage Bot:  Slack conversation screenshot.  User has received a share of a web page.  The share shows author and reading time details in the social sharing card.
SvelteKit SEO: Slack: Author and Reading Time sourced from meta we provide

So now you know how automatically to include a reading time whenever your post is shared on Slack. The other tags as used mostly for Twitter.

πŸ—³οΈ Poll

Should I include the Twitter SEO component in the starter, or is it something you'd prefer to add yourself when you need it?
Voting reveals latest results.

We need to update the SEO component to include Twitter metadata now:

src/lib/components/SEO/index.svelte
svelte
1<script>
2 import { VERTICAL_LINE_ENTITY } from '$lib/constants/entities';
3 import website from '$lib/config/website';
4 import Twitter from './Twitter.svelte';
5
6 const { author, siteLanguage, siteTitle, siteUrl } = website;
7
8 export let article = false;
9 export let metadescription;
10 export let slug;
11 export let timeToRead = 0;
12 export let title;
13 export let twitterImage = {
14 url:
15 'https://rodneylab-climate-starter.imgix.net/home-twitter.jpg?ixlib=js-3.2.0&w=800&h=418&s=1b08b7276d34486234a4e2c1ccb49a74',
16 alt:
17 'picture of a person with long, curly hair, wearing a red had taking a picture with an analogue camera',
18 };
19
20 const pageTitle = `${siteTitle} ${VERTICAL_LINE_ENTITY} ${title}`;
21 const twitterProps = {
22 article,
23 author,
24 twitterUsername: import.meta.env.VITE_TWITTER_USERNAME,
25 image: twitterImage,
26 metadescription,
27 pageTitle,
28 timeToRead,
29 url: `${siteUrl}/${slug}`,
30 };
31</script>
32
33<svelte:head>
34 <title>{pageTitle}</title>
35 <meta name="description" content={metadescription} />
36 <meta
37 name="robots"
38 content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1"
39 />
40 <html lang={siteLanguage} />
41</svelte:head>
42<Twitter {...twitterProps} />

We specify a fallback image, with alt text in lines 13–18. This is used if we don't specify the twitterImage prop when the component is consumed. The next piece in the chain is making sure metadata needed for Twitter SEO is passed through from the blog posts:

src/lib/components/BlogPost.svelte
svelte
1<script>
2 import readingTime from 'reading-time';
3 import BannerImage from '$lib/components/BannerImage.svelte';
4 import SEO from '$lib/components/SEO/index.svelte';
5
6 export let post;
7
8 const timeToRead = Math.ceil(readingTime(post.body).minutes);
9 const {
10 featuredImage,
11 featuredImageAlt,
12 featuredImageSrc,
13 featuredImageSrcset,
14 postTitle: title,
15 seoMetaDescription: metadescription,
16 slug,
17 twitterImage = null,
18 } = post;
19
20 const twitterImageObject = twitterImage
21 ? {
22 url: twitterImage,
23 alt: featuredImageAlt,
24 }
25 : null;
26 const bannerImageProps = {
27 featuredImage,
28 featuredImageAlt,
29 featuredImageSrc,
30 featuredImageSrcset,
31 };
32</script>
33
34<SEO
35 article={true}
36 {slug}
37 {title}
38 {metadescription}
39 {timeToRead}
40 twitterImage={twitterImageObject}
41/>
42<BannerImage {...bannerImageProps} />

twitterImage in line 17 will be a url for the image we want to be shared on Twitter (remember this should be the dimensions we mentioned above). This value feeds from the individual post frontmatter. We give it a default value here, for the case it is undefined. We assume the Twitter shared image has similar appearance to the main featured image of the post, so we can recycle the alt text. Remember that alt text should describe the image for the benefit of visually impaired users.

Finally we need to install the reading-time package:

pnpm i -D reading-time

πŸ’― Checking Twitter Metadata

You can test Twitter metadata using the Twitter Card Validator :

SvelteKit SEO: Twitter Card Validator

The Twitter logo watermark is something I added to the image file itself, just so you could see Twitter is picking up the specified image (which doesn't even appear on the shared page). The Twitter meta is used by the Telegram messaging app too. To request the Telegram server to cache the image for your page, just start a new conversation in Telegram with @webpagebot and paste, then send, up to 10 URLs at a time as a message:

SvelteKit SEO: Telegram: Webpage Bot:  Telegram conversation screenshot for converation with the web page bot.  User has typed a U R L as a message and the message window displays social sharing card featuring images and text set by the site's meta
SvelteKit SEO: Telegram: Webpage Bot

πŸ™ŒπŸ½ OpenGraph and SchemaOrg Metadata

Is this post, we have looked at:

  • why SEO is important and an introduction to the broader topic,
  • general SEO metadata,
  • Twitter metadata.

You might have guessed this is not the end of the story. As mentioned earlier, SEO is a detailed topic and there is too much detail to be able to summarise in a single post. In an upcoming post we will look at SchemaOrg metadata. This can be used to help your page appear, for example, in HowTo in Google search results.

In this followup post, we look at Open Graph SEO in SvelteKit. Although this is used by Facebook, it is also used in other apps, especially messaging apps (like Telegram and Signal). We will also see how the order in which the meta tags are arranged affects whether WhatsApp shows your Card whenever a link for your site is shared.

You can see the code for the story so far on the Rodney Lab Git Hub repo .

πŸ™πŸ½ SvelteKit SEO: Feedback

Please send me feedback! Have you found the post useful? Would you like to see posts on another topic instead? Get in touch with ideas for new posts. Also if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a couple of dollars, rupees, euros or pounds, please consider supporting me through Buy me a Coffee.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on SvelteKit as well as Gatsby JS among other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.