Adding Markup to your SvelteKit Site

SvelteKit PWA

Adding Markup to your SvelteKit Site


πŸ˜• What use is Markup for SEO?

In this post we look at adding markup to your SvelteKit site. This is the third post in the series on SEO in SvelteKit. Firstly we had an introduction to SEO and saw how to add Twitter compatible metadata to you Svelte site. Following that we looked at the OpenGraph protocol for SEO metadata developed at Facebook and adopted on many apps for creating lovely share cards for your pages. Be sure to skim through those posts for more detailed explanations of anything which is not immediately clear in this post. Anyway let's get back to As usual before we get going, we'll look at why this topic matters. With that out of the way we see how you can add the SEO markup to your site and then how to check Google is happy with it.

How is different to OpenGraph Meta?

The meta we looked at in the previous posts is mostly about telling apps how to present your website when it is shared. That is what image, title and description to use. Although the title and meta description give search engines an idea of what the page is about, it is not easy for search engines to parse a page's content to infer what it is all about in detail. Is the page about a live performance which you can still buy tickets for? Or, for example, is it a step-by-step recipe for baking banana bread? Is there a video on the page? What are the author's Twitter and other social media pages? It is easy to let search engines know all of that information and more using As an example here is how a page with embedded How-To metadata is displayed in the Google search results page:

Adding Markup to your SvelteKit Site:  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
Adding Markup to your SvelteKit Site: Featured Snippet Screen Capture

The presentation of your page in search results pages will vary depending on the type of markup you include. Notice in the example above how the result is displayed in a larger test and with a picture. This makes it stand out from other results, increasing the chances of attracting the user to your site.

In the next section we will look at some meta you might want to include in your site. Following that we see how you can go about adding markup to your SvelteKit site. Finally we explore a few methods for checking the markup is valid. Are you ready to get going?

πŸ§‘πŸ½β€πŸŽ“ Types

There are literally 100s of types (currently 792 to be more precise ). Below is a list you might find useful for your site. If you are working on a niche or specialist site, it is worth taking ten minutes to browse through additional types on which are relevant for your field. Also, for more ideas, be sure to crack open your competitors' sites in Developer Tools to see what they include. The first group, below, contains items which will all probably be relevevant for the site you are working on. The second contains other types which are still common, but might not be appropriate for your site. Types for Most Sites

We look at code examples below focussing on including these in your site. But first, here are the types:

  • Entity : key information about your organisation. Unlike the other tags, this only needs to be included on a single page. You can then reference the meta from other pages when needed on other pages. We will see how to do this in the code below. Typically you will include this tag on your home page. This works for many sites, where your home page mostly contains information about you and your entity. If you have a lot of other information on your home page (blog posts, contact information, maps, customer testimonials, reviews etc.) Google might have a hard time working out which information is about you and your organisation. In this case place the meta on your about page. Try to keep the about page focussed if you do this.

  • WebSite : key information about your site, included on every page.

  • WebPage : this goes on every page and includes similar meta to what we included in the Twitter and OpenGraph tags.

  • SearchAction : lets search engines know how users can do an internal search on your site. Skip this if you do not have internal search. Also don't forget to adapt the meta to match your site's search parameter format.

More Types for Most Sites

  • ImageObject : use this to add your picture or company logo to the markup. It can also be used on images in general on the site (also used within some other types we look at here).

  • BreadcrumbList : structured object letting the search engine know how the current page fits into the site's structure. If you include this, Google adds the breadcrumbs to the search results. It helps bots understand the structure of your site too. Including breadcrumbs on your pages themselves (within the HTML body, not just the meta) also provides internal links, again providing hints to bots on how content is related.

  • Article : metadata on articles, includes the author, post categories and language as well as initial publish and modification dates. You can add comments and likes in here if you want to go to town.

  • Person : Has many uses. Include this on personal sites in the WebSite object to associate the site with the owner. Also include in posts to identify the author. Include links to social media profiles or other websites associated with the person.

  • Organization : information about the organisation which the site represents.

Some More Particular Types

For examples of how to implement these, follow the link and scroll to the bottom of the page that opens up. There are typically examples in several languages. Choose the JSON-LD one for an idea of the schema and use an example code below as a template for your SvelteKit implementation.

πŸ“ Some Notes on Adding Markup to your SvelteKit Site

Before we look at the code, there are a few points worth mentioning. First of all, Google have some eligibility criteria. In the main these relate to the data provided being representative of the page it appears on and not being misleading. Google guidelines detail other eligibility criteria including the content not being obscene, inappropriate or hateful .

Beyond the guidelines, Google might seem fussy about the fields included. You may need a couple of attempts to get a new type right. We will look at tools for testing shortly. These rely on publishing the data on a public site. You might need a little patience to get things right. Fortunately SvelteKit sites build very quickly so debugging isn't too onerous.

πŸ—³οΈ Poll

How much markup are you already adding to your sites?
Voting reveals latest results.

πŸ–₯ Adding Markup to your SvelteKit Site: Code

There is a little data which feeds into the SchemaOrg component. The mechanism is similar to the one we used for the Twitter and OpenGraph components though. As there is a bit to get through here, we won't go into details on how to plumb the data in. That should stop the post getting too long! You can see the full code on the Rodney Lab GitHub repo which is a complete and tested version . The demo site is up at . We will focus on the SchemaOrg component which is in the file src/lib/components/SEO/SchemaOrg.svelte.

SchemaOrg component

Let's start at the end! There are a few data format options for including on your site. I would say the easiest is using JSON-LD in a script tag. You have to be a little careful with how you include the tag in your Svelte file firstly, for it to be parsed as intended and secondly so prettier doesn't mangle it! I found this works, strange as it looks:

185const schemaOrgArray = [
186 schemaOrgEntity,
187 schemaOrgWebsite,
188 schemaOrgImageObject,
189 schemaOrgWebPage,
190 schemaOrgBreadcrumbList,
191 schemaOrgPublisher,
192 ];
193 let jsonLdString = JSON.stringify(schemaOrgObject);
194 let jsonLdScript = `
195 <script type="application/ld+json">
196 ${jsonLdString}
197 ${'<'}/script>
198 `;
201 {@html jsonLdScript}

We will build up the elements of schemaOrgArray one by one. If you are using this as a guide for work on other frameworks, the most important feature is to include the script tag in each page's HTML head section. That's basically all the code above does:

<script type="application/ld+json">


Okay let's look at the schemaOrgEntity first. This is the first element in the array in lines 185–192. Essentially the array combines several type objects into a single element which we can include in the script tag just mentioned.

27const schemaOrgEntity =
28 entityMeta !== null
29 ? {
30 '@type': ['Person', 'Organization'],
31 '@id': `${siteUrl}/#/schema/person/${entityHash}`,
32 name: author,
33 image: {
34 '@type': 'ImageObject',
35 '@id': `${siteUrl}/#personlogo`,
36 inLanguage: siteLanguage,
37 url: entityMeta.url,
38 width: entityMeta.faviconWidth,
39 height: entityMeta.faviconHeight,
40 caption: author,
41 },
42 logo: {
43 '@id': `${siteUrl}/#personlogo`,
44 },
45 sameAs: [
46 `${twitterUsername}`,
47 `${githubPage}`,
48 `${tiktokUsername}`,
49 `${telegramUsername}`,
50 `${linkedinProfile}`,
51 facebookPage,
52 ],
53 }
54 : null;

We saw earlier that we only need to include this element on a single page. We include it on the home page in this example. This is done by adding the entityMeta object in the props passed to the SEO component on the home page. The @id field in line 31 allows us to reference this object in other objects. We will see that field used in other objects. The social media profiles are included so Google can add those profiles to your Knowledge graph in search results. The knowledge graph appears towards the right on the Google desktop search result page. Here is an example:

Adding Markup to your SvelteKit Site:  example of a knowledge graph shows GitHub metadata with social media icons towards the bottom
Adding Markup to your SvelteKit Site: Knowledge Graph example

Web Site

Next up is the schemaOrgWebsite object. This includes the SearchAction type.

56const schemaOrgWebsite = {
57 '@type': 'WebSite',
58 '@id': `${siteUrl}/#website`,
59 url: siteUrl,
60 name: siteTitle,
61 description: siteTitleAlt,
62 publisher: {
63 '@id': `${siteUrl}/#/schema/person/${entityHash}`,
64 },
65 potentialAction: [
66 {
67 '@type': 'SearchAction',
68 target: `${siteUrl}/?s={search_term_string}`,
69 'query-input': 'required name=search_term_string',
70 },
71 ],
72 inLanguage: siteLanguage,
73 };

No need to include this if internal search is not implemented in your site. The search parameter in this code (line 68) works if, to search for β€œcheese” you would enter the url Tweak as needed for your own use case.

Image Object

Next up we have the ImageObject. This is the featured image for the page we are adding the meta to. The data included is not too different to the data we used for the Twitter and OpenGraph meta:

75const schemaOrgImageObject = {
76 '@type': 'ImageObject',
77 '@id': `${url}#primaryimage`,
78 inLanguage: siteLanguage,
79 url: featuredImage.url,
80 contentUrl: featuredImage.url,
81 width: featuredImage.width,
82 height: featuredImage.height,
83 caption: featuredImage.caption,
84 };

I don't think there is anything that needs clarification here, but let me know if I am wrong.

Breadcrumb List

Moving swiftly on, we have BreadcrumbList up next. Breadcrumbs just provide a hierarchy. The code included within the SchemaOrg component relies on us defining a breadcrumb object for each page or template. Here is an example of the code for defining breadcrumbs on a page, used in the blog post template:

24const breadcrumbs = [
25 {
26 name: 'Home',
27 slug: '',
28 },
29 {
30 name: title,
31 slug,
32 },
33 ];

This works fine for small blog sites, but for larger sites (with many non-blog post pages) it might not scale well. I heard Elder.js has a smart way of handling Breadcrumbs, but I haven't yet had a chance to investigate. Anyway here is the actual code breadcrumb code in the SchemaOrg component which ingests data supplied in the format above:

86const schemaOrgBreadcrumbList = {
87 '@type': 'BreadcrumbList',
88 '@id': `${url}#breadcrumb`,
89 itemListElement:, index) => ({
90 '@type': 'ListItem',
91 position: index + 1,
92 item: {
93 '@type': 'WebPage',
94 '@id': `${siteUrl}/${element.slug}`,
95 url: `${siteUrl}/${element.slug}`,
96 name:,
97 },
98 })),
99 };

Web Page, Article and Publisher

We just have three more objects to investigate, so let's prepare for the sprint finish! The final three objects are not substantially different to the preceding ones so we will tackle them in a single leap:

src/lib/components/SEO/SchemaOrg.svelte β€” click to expand code.
101const schemaOrgWebPage = {
102 '@type': 'WebPage',
103 '@id': `${url}#webpage`,
104 url,
105 name: title,
106 isPartOf: {
107 '@id': `${siteUrl}/#website`,
108 },
109 primaryImageOfPage: {
110 '@id': `${url}#primaryimage`,
111 },
112 datePublished,
113 dateModified: lastUpdated,
114 author: {
115 '@id': `${siteUrl}/#/schema/person/${entityHash}`,
116 },
117 description: metadescription,
118 breadcrumb: {
119 '@id': `${url}#breadcrumb`,
120 },
121 inLanguage: siteLanguage,
122 potentialAction: [
123 {
124 '@type': 'ReadAction',
125 target: [url],
126 },
127 ],
128 };
130 let schemaOrgArticle = null;
131 if (article) {
132 schemaOrgArticle = {
133 '@type': 'Article',
134 '@id': `${url}#article`,
135 isPartOf: {
136 '@id': `${url}#webpage`,
137 },
138 author: {
139 '@id': `${siteUrl}/#/schema/person/${entityHash}`,
140 },
141 headline: title,
142 datePublished,
143 dateModified: lastUpdated,
144 mainEntityOfPage: {
145 '@id': `${url}#webpage`,
146 },
147 publisher: {
148 '@id': `${siteUrl}/#/schema/person/${entityHash}`,
149 },
150 image: {
151 '@id': `${url}#primaryimage`,
152 },
153 articleSection: ['blog'],
154 inLanguage: siteLanguage,
155 };
156 }
158 const schemaOrgPublisher = {
159 '@type': ['Person', 'Organization'],
160 '@id': `${siteUrl}/#/schema/person/${entityHash}`,
161 name: entity,
162 image: {
163 '@type': 'ImageObject',
164 '@id': `${siteUrl}/#personlogo`,
165 inLanguage: siteLanguage,
166 url: `${siteUrl}/assets/rodneylab-logo.png`,
167 contentUrl: `${siteUrl}/assets/rodneylab-logo.png`,
168 width: 512,
169 height: 512,
170 caption: entity,
171 },
172 logo: {
173 '@id': `${siteUrl}/#personlogo`,
174 },
175 sameAs: [
176 `${twitterUsername}`,
177 `${githubPage}`,
178 `${tiktokUsername}`,
179 `${telegramUsername}`,
180 `${linkedinProfile}`,
181 facebookPage,
182 ],
183 };

As always if there is anything in here which needs further explanation, don't hesitate to drop a comment below.

For reference here is the complete set of output JSON for a blog post:

Example JSON for a blog post β€” click to expand code.
Example JSON for a blog post
2 "@context": "",
3 "@graph": [
4 {
5 "@type": ["Person", "Organization"],
6 "@id": "",
7 "name": "Rodney Johnson",
8 "image": {
9 "@type": "ImageObject",
10 "@id": "",
11 "inLanguage": "en-GB",
12 "url": "",
13 "width": 512,
14 "height": 512,
15 "caption": "Rodney Johnson"
16 },
17 "logo": { "@id": "" },
18 "sameAs": [
19 "",
20 "",
21 "",
22 "",
23 "",
24 ""
25 ]
26 },
27 {
28 "@type": "WebSite",
29 "@id": "",
30 "url": "",
31 "name": "SvelteKit SEO Demo Site",
32 "description": "SvelteKit SEO",
33 "publisher": {
34 "@id": ""
35 },
36 "potentialAction": [
37 {
38 "@type": "SearchAction",
39 "target": "{query}",
40 "query": "required"
41 }
42 ],
43 "inLanguage": "en-GB"
44 },
45 {
46 "@type": "ImageObject",
47 "@id": "",
48 "inLanguage": "en-GB",
49 "url": "",
50 "contentUrl": "",
51 "width": 672,
52 "height": 448,
53 "caption": "Home page"
54 },
55 {
56 "@type": "WebPage",
57 "@id": "",
58 "url": "",
59 "name": "SvelteKit SEO Demo Site | Home",
60 "isPartOf": { "@id": "" },
61 "primaryImageOfPage": { "@id": "" },
62 "datePublished": "2021-07-07T14:19:33.000+0100",
63 "dateModified": "2021-07-07T14:19:33.000+0100",
64 "author": {
65 "@id": ""
66 },
67 "description": "SvelteKit MDsvex Blog Starter - starter code by Rodney Lab to help you get going on your next blog site",
68 "breadcrumb": { "@id": "" },
69 "inLanguage": "en-GB",
70 "potentialAction": [
71 { "@type": "ReadAction", "target": [""] }
72 ]
73 },
74 {
75 "@type": "BreadcrumbList",
76 "@id": "",
77 "itemListElement": [
78 {
79 "@type": "ListItem",
80 "position": 1,
81 "item": {
82 "@type": "WebPage",
83 "@id": "",
84 "url": "",
85 "name": "Home"
86 }
87 }
88 ]
89 },
90 {
91 "@type": ["Person", "Organization"],
92 "@id": "",
93 "name": "Rodney Lab",
94 "image": {
95 "@type": "ImageObject",
96 "@id": "",
97 "inLanguage": "en-GB",
98 "url": "",
99 "contentUrl": "",
100 "width": 512,
101 "height": 512,
102 "caption": "Rodney Lab"
103 },
104 "logo": { "@id": "" },
105 "sameAs": [
106 "",
107 "",
108 "",
109 "",
110 "",
111 ""
112 ]
113 }
114 ]

Putting it all together

Don't forget to include the new SchemaOrg component in the SEO component (as mentioned earlier we won't go into details on this, but let me know if anything is unclear):

108 <title>{pageTitle}</title>
109 <meta name="description" content={metadescription} />
110 <meta
111 name="robots"
112 content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1"
113 />
114 <html lang={siteLanguage} />
116<Twitter {...twitterProps} />
117<OpenGraph {...openGraphProps} />
118<SchemaOrg {...schemaOrgProps} />

You can see the full code on the Rodney Lab GitHub repo which is a complete and tested version . If it's OK with you let's move on to testing.

πŸ’― Adding Markup to your SvelteKit Site: Testing

As usual I can't go without us first running through how to test our work. There are currently three steps I use. The first is a sanity check using the browser on the dev server. You can check, the markup contains all the expected fields in the browser developer tools using Inspector on Firefox or Elements on Chrome. I will run through the process using Firefox, though it is similar on Chrome. First search for the script tag in the Inspector search tool. When you find the element it will be tricky to make out as the code is minified. I like to copy the JSON to Visual Code and use Prettier to format it before taking a look:

Adding Markup to your SvelteKit Site: Sanity Check

Here we are just looking for anything which looks out of place or missing. It makes sense to run this sanity check before pushing the code to our server and building the site.

Google Structured Data Testing Tools

For the next steps we need to run tests using a publicly accessible URL. This means you will need to publish the site to your testing server. There are two Google tools for testing Structured Data. The first is marked for retirement, but it still works and I find it more helpful for debugging. You go to and paste in you test site's URL . It will give warnings or errors if it is not happy with something. Be sure to fix the errors, using the site for help . It is worth repairing warnings where you can to improve your ranking.

Google's replacement tool works in a similar way:

Adding Markup to your SvelteKit Site: Rich Results Testing Tool

Google Search Console

That's it in terms of testing. However the schema do change from time to time and it is also possible you inadvertently break some functionality without knowing. For that reason regularly check your structured data in the Google Search Console. To do this go to and log in . From the left side menu look at each of the items under Enhancements in turn. If Google found errors in the structured data when crawling the site, they will show up here in red. Also, typically Google will send you an email when the bot does encounter an error while crawling.

Adding Markup to your SvelteKit Site:  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
Adding Markup to your SvelteKit Site: Featured Snippet Screen Capture

πŸ™ŒπŸ½ Adding Markup to your SvelteKit Site: Wrapup

That's it for this post. We have seen:

  • why is so useful,
  • what types you might want to include on your site,
  • adding markup to your SvelteKit site and testing it.

As always suggestions for improvements, together with requests for explanations and feedback are more than welcome. Also let me know what other features you would like implemented on the starter.

πŸ™πŸ½ Adding Markup to your SvelteKit Site: 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 other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.