A powerful, lightweight solution for managing meta tags in SvelteKit applications with intelligent data cascading.
You define your metadata in +page.ts
or +layout.ts
load functions, using our helper functions for simpler code - like so:
// src/routes/+layout.ts
import { metaLoad } from "@opensky/seo";
export const load = metaLoad.layout({
sitename: "My Awesome Site",
title: "Home",
titleTemplate: "My Awesome Site - {page}",
description: "A fantastic SvelteKit application",
icon: "./favicon.png",
});
Congrats! You now have seo meta tags for your entire site. Customizing meta tags from here on is easy
bun add @opensky/seo
# or
npm install @opensky/seo
// src/routes/+layout.ts
import { metaLoad } from "@opensky/seo";
export const load = metaLoad.layout({
sitename: "My SvelteKit App",
title: "Welcome",
titleTemplate: "My SvelteKit App - {page}",
description: "A fantastic SvelteKit application",
icon: "./favicon.png",
});
<!-- src/routes/+layout.svelte -->
<script lang="ts">
import { MetaTags } from '@opensky/seo';
let { children } = $props();
</script>
<MetaTags />
{@render children()}
// src/routes/blog/+layout.ts
import { metaLoad } from "@opensky/seo";
export const load = metaLoad.layout({
title: "Blog",
titleTemplate: "Svelte Blog - {page}",
description: "Read our latest articles and updates",
});
// src/routes/blog/[slug]/+page.ts
import { metaLoadWithData } from "@opensky/seo";
export const load = metaLoadWithData.page(({ data }) => ({
title: data.post.title,
description: "This is a detailed description of my blog post",
}));
And that's it! Your meta tags will be automatically generated and updated across your site.
SvelteKit Meta uses a data cascade pattern to efficiently manage metadata:
Metadata flows down through your application's routing hierarchy inheriting any data above, with lower levels being able to selectively override higher-level values.
The provided helpers for writing the load functions take care of passing parent data and data from any +page.server.ts
load functions down to the page. Other packages will lose the data from page server load functions, but using these helpers ensures that the page receives everything from the data cascade.
Titles are handled specially to allow you to set a title template and for that to work in a correct fashion.
SvelteKit Meta provides several helper functions to simplify integration. You still have to create +page.ts
and +layout.ts
files but these make it simpler to define your meta tags with minimal boilerplate code
metaLoad
- Static MetadataUse these functions when you have metadata that doesn't depend on route parameters or server data.
metaLoad.page(metaTags)
- Creates a page load function with static metadatametaLoad.layout(metaTags)
- Creates a layout load function with static metadatametaLoad.resetLayout(metaTags)
- Creates a reset layout load function with static metadatametaLoadWithData
- Dynamic MetadataUse these functions when you need to access route parameters, server data, or other context to generate metadata.
metaLoadWithData.page(callback)
- Creates a page load function with dynamic metadatametaLoadWithData.layout(callback)
- Creates a layout load function with dynamic metadatametaLoadWithData.resetLayout(callback)
- Creates a reset layout load function with dynamic metadataaddMetaTags
- Manual IntegrationUse these functions when you want full control over your load function but still want to add metadata.
addMetaTags.page(metaTags)
- Adds page metadata to load function return valueaddMetaTags.layout(metaTags)
- Adds layout metadata to load function return valueaddMetaTags.resetLayout(metaTags)
- Adds reset layout metadata to load function return valueTitle templates allow you to define a pattern for constructing page titles. You are required to specify the route where the template is located. This allows us to evaluate multiple layers of title templates and decide which should be used.
To understand this concern, consider the following: you have a /staff
route with /staff/+layout.ts
and /staff/+page.ts
files and you also have /staff/jeff
route. When you visit /staff you want to use the title from staff layout and the template from the higher layout, in this case root. But you also want a template to be defined in staff/layout which would be applied at /staff/jeff. The issue is that if you set a titleTemplate at staff/layout it will override the root layout's titleTemplate and so visiting /staff will use the titleTemplate from /staff/layout. Instead of "Site - Staff" using root template, it would be "Staff - Staff" using the staff layout template. By specifying route in the title template, we are able to determine which should be used based on the route being loaded.
// Root layout - applies to all pages under /
export const load = metaLoad.layout({
titleTemplate: { route: "/", template: "My Site - {page}" },
});
// Blog layout - applies to all pages under /blog
export const load = metaLoad.layout({
titleTemplate: { route: "/blog", template: "Blog - {page}" },
});
// Individual page - will use the most specific template
export const load = metaLoad.page({
title: "My Amazing Post",
});
// Result: "Blog - My Amazing Post" (uses /blog template)
For dynamic metadata, you can use the metaLoadWithData
helper to incorporate route parameters:
// src/routes/blog/[slug]/+page.ts
import { metaLoadWithData } from "@opensky/seo";
export const load = metaLoadWithData.page(({ params, data }) => {
const { slug } = params;
// Fetch article data based on slug
const article = await getArticleBySlug(slug);
return {
title: article.title,
description: article.excerpt,
image: article.featuredImage,
};
});
If you need full control over your load function, you can use the addMetaTags
helpers:
// +page.ts
export async function load({ data, route, params, url }) {
const post = await fetchPost(params.slug);
return {
...data,
post,
...addMetaTags.page({ title: post.title }),
};
}
// +layout.ts
export async function load({ data, route, params }) {
const section = await fetchSection();
return {
...data,
section,
...addMetaTags.layout({ title: section.title }),
};
}
The metaLoadWithData
and metaLoad
helpers automatically merge parent data, server data, and your meta data. If you are using addMetaTags, it is important that you handle this otherwise server data will not reach the page.
You must take in data
as a destructured argument in your load function and spread it on the return value
// +page.ts
export async function load({ data }) {
/* ^ include data */
return {
...data,
/* ^ include data */
...addMetaTags.page({ title: post.title }),
};
}
SvelteKit Meta supports a comprehensive range of metadata properties:
Property | Description |
---|---|
canonical |
Canonical URL for the page (og:url, link:rel:canonical) |
icon |
Favicon path as string or IconOpinionated object |
maskIcon |
Object with url (svg file) and optional color (hex) for Safari Pinned Tabs |
theme |
Theme color for browser UI elements (string or { light, dark } object) |
colorScheme |
Color scheme preference for the page (meta:color-scheme) |
sitename |
Name of the website (og:sitename) |
title |
Page title (warns if > 70 characters) (og:title, meta:title) |
titleTemplate |
Template for constructing child page titles (e.g., "Reviews - {page}") |
description |
Page description (warns if > 200 characters) (og:description, twitter:description) |
author |
Content author(s) as string or array (meta:author, og:author) |
twitterSite |
The X Account for the publishing site (twitter:site) |
twitterCreator |
The X Account for the author/creator of this page (twitter:creator) |
date |
Publication date in ISO 8601 format (og meta) |
modified |
Last modified date (og:modified-time, meta:modified, meta:last-modified) |
type |
Content type configuration (basic , article , largeImage , player or ContentTypeAdvanced) |
image |
Primary image for social sharing (string or Media object) |
images |
Multiple images for social sharing (array of Media objects) |
video |
Primary video for social sharing (string or Media object) |
videos |
Multiple videos for social sharing (array of Media objects) |
Media objects for images and videos can include:
type Media = {
url: string; // Primary URL for the media resource
secureUrl?: string; // HTTPS URL for the media resource
type?: string; // MIME type of the media
width?: number; // Width in pixels
height?: number; // Height in pixels
alt?: string; // Alt text description
};
Icons can be configured with more advanced options:
type IconOpinionated = {
svg?: string;
small?: {
url: string;
size: number;
type?: string;
};
large?: {
url: string;
size: number;
type?: string;
};
};
Content type can be configured with advanced options for different platforms:
type ContentTypeAdvanced = {
twitter: "summary" | "largeImage" | "player";
og: "website" | "article";
};
Note: Properties like
image
/images
andvideo
/videos
are mutually exclusive. The library will warn and use the plural version if both are specified.
MIT License