SvelteKit + SEO
Meta/OG tags, canonical URL, JSON-LD, robots.txt and sitemap.xml. This page shows exactly what pickstack adds to a SvelteKit project for SEO: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.
$ npm create pickstack@latest my-app -- --framework sveltekit --modules seo --yesCustomize in the configurator
New files
import { defineEnvVars } from "@sveltejs/kit/env";
export const variables = defineEnvVars({
SITE_URL: {
public: true,
static: true,
description: "Public origin of the site, without trailing slash",
schema: (value) => value ?? "https://example.com",
},
});
import { SITE_URL } from "$app/env/public";
export const prerender = true;
export function GET() {
const body = `User-agent: *
Allow: /
Sitemap: ${SITE_URL}/sitemap.xml
`;
return new Response(body, { headers: { "Content-Type": "text/plain" } });
}
import { SITE_URL } from "$app/env/public";
export const prerender = true;
export function GET() {
const body = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>${SITE_URL}/</loc>
</url>
<url>
<loc>${SITE_URL}/about</loc>
</url>
</urlset>
`;
return new Response(body, { headers: { "Content-Type": "application/xml" } });
}
Changes to existing files
<script lang="ts">+ import { SITE_URL } from "$app/env/public"; import { resolve } from "$app/paths";+ import { page } from "$app/state"; import type { LayoutProps } from "./$types"; import "./layout.css"; let { children }: LayoutProps = $props();+ + const jsonLd = JSON.stringify({+ "@context": "https://schema.org",+ "@type": "WebSite",+ name: "my-app",+ url: SITE_URL,+ });+ // The closing tag is split so it neither ends this block nor trips svelte-eslint-parser.+ const jsonLdScript = `<script type="application/ld+json">${jsonLd}</${"script"}>`; </script> +<svelte:head>+ <meta name="description" content="my-app: a web app." />+ <link rel="canonical" href={SITE_URL + page.url.pathname} />+ <meta property="og:type" content="website" />+ <meta property="og:site_name" content="my-app" />+ <meta property="og:url" content={SITE_URL + page.url.pathname} />+ <meta name="twitter:card" content="summary" />+ {@html jsonLdScript}+</svelte:head>+ <main class="app"> <nav class="nav"> <a class="link" href={resolve("/")}>Home</a>⋯ 5 unchanged lines
⋯ 1 unchanged lines import Counter from "#lib/Counter.svelte"; </script> +<svelte:head>+ <title>Home | my-app</title>+</svelte:head>+ <section class="page"> <h1 class="title">my-app</h1> <Counter />⋯ 2 unchanged lines
+<svelte:head>+ <title>About | my-app</title>+</svelte:head>+ <section class="page"> <h1 class="title">About</h1>+ <p class="text">Built with SvelteKit, Svelte 5, TypeScript, SEO.</p>− <p class="text">Built with SvelteKit, Svelte 5, TypeScript.</p> </section>
Project files
.gitignoreREADME.mdpackage.jsonsrc/app.d.tssrc/app.htmlsrc/env.tssrc/lib/Counter.sveltesrc/lib/index.tssrc/routes/+layout.sveltesrc/routes/+layout.tssrc/routes/+page.sveltesrc/routes/about/+page.sveltesrc/routes/layout.csssrc/routes/robots.txt/+server.tssrc/routes/sitemap.xml/+server.tstsconfig.jsonvite.config.ts