pickstack

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 --yes
Customize in the configurator

New files

src/env.ts
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",
  },
});
src/routes/robots.txt/+server.ts
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" } });
}
src/routes/sitemap.xml/+server.ts
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

src/routes/+layout.svelte (changed)
 <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
src/routes/+page.svelte (changed)
⋯ 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
src/routes/about/+page.svelte (changed)
+<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

Related templates