SvelteKit SEO for AI crawlers: ssr, prerender and SPA mode
SvelteKit SEO for AI crawlers: how ssr, csr and prerender change the HTML, why the adapter-static SPA fallback hurts, and how to serve robots.txt and a sitemap.
By Crawlito · 6 min read
In short
- SvelteKit server-renders pages by default, so crawlers that read raw HTML get the full content unless ssr is turned off.
- Setting ssr = false renders an empty shell page, and setting it in the root +layout.js turns the whole app into a single-page app.
- The adapter-static fallback option creates an SPA for any route that was not prerendered; SvelteKit's docs say this has large negative performance and SEO impacts.
- Put robots.txt in the static folder, or serve it and sitemap.xml from +server.js endpoints, and give every page its own title and description in svelte:head.
- Leave trailingSlash at never or always: SvelteKit's docs warn that ignore makes /x and /x/ separate URLs.
On this page
- What SvelteKit sends crawlers by default
- The page options that change the HTML
- The adapter-static fallback pitfall
- How to serve robots.txt in SvelteKit
- How to generate a sitemap
- Titles, descriptions and canonical tags with svelte:head
- A checklist before you deploy
- Frequently asked questions
- Check a SvelteKit page as a crawler sees it
SvelteKit is a good fit for AI crawlers out of the box, because it server-renders every page by default. The HTML a crawler receives contains your content, title and meta tags without running JavaScript. Problems start when a project turns off server rendering with ssr = false, or deploys with adapter-static and a fallback page, which turns unprerendered routes into a single-page app that sends crawlers an empty shell.
This guide covers the page options that change what crawlers see, the static adapter pitfall, and how to serve robots.txt, a sitemap and per-page metadata.
What SvelteKit sends crawlers by default
SvelteKit's SEO documentation says it uses server-side rendering by default and recommends leaving it on, because server-rendered content is indexed more reliably. Each request returns HTML with the page content already in it, then the client-side code hydrates the page so it becomes interactive.
That matters because most AI crawlers do not document running JavaScript. Public measurements, summarised in do AI crawlers run JavaScript, found the major ones read only the server's HTML. With SSR on, that HTML is complete.
Check it on your own site:
curl -s https://www.example.com/pricing | grep -o "<title>.*</title>"
curl -s https://www.example.com/pricing | grep -c "Starter plan"If the title is right and the count is above zero, crawlers get the content.
The page options that change the HTML
SvelteKit's page options are exports in +page.js, +page.server.js, +layout.js or +layout.server.js. A layout's option applies to every page below it, which is why one line in the root layout can change a whole site.
| Option | Default behaviour | What crawlers get when changed |
|---|---|---|
ssr = false | Pages render on the server | An empty shell; content appears only after JavaScript runs |
csr = false | Pages hydrate in the browser | Full HTML and no JavaScript shipped at all |
prerender = true | Pages render per request | Full HTML generated at build time |
prerender = 'auto' | Not set | Prerendered, but the route can still be server-rendered dynamically |
trailingSlash | 'never' | 'always' adds slashes; 'ignore' creates duplicate URLs |
ssr = false
The page options docs say that with ssr = false SvelteKit renders an empty "shell" page instead, and that setting it in the root +layout.js turns the entire app into an SPA. The docs say this is not recommended in most situations.
// src/routes/+layout.js
export const ssr = false; // every page now sends crawlers an empty shellIt is sometimes added to silence errors from a library that touches window or document during server rendering. A better fix is to import that library only in the browser, for example inside onMount, or to turn off SSR only for the one route that needs it.
csr = false
The opposite option is useful for content pages. With csr = false, SvelteKit ships no JavaScript to the client. The docs list the consequences: <script> tags are removed, forms cannot be progressively enhanced, and links cause full-page navigation. For a blog post, documentation page or policy page that has no interactivity, that is often fine, and the HTML is what every crawler and visitor gets.
prerender
Prerendering generates HTML files at build time. The docs say pages with form actions cannot be prerendered, because a server must handle the POST requests. For dynamic routes such as /blog/[slug], SvelteKit discovers pages by following links from your entry points, or you can list them with an entries export. A post that nothing links to is not prerendered unless you list it.
// src/routes/blog/[slug]/+page.js
export const prerender = true;
export async function entries() {
const posts = await getAllPosts();
return posts.map((post) => ({ slug: post.slug }));
}The adapter-static fallback pitfall
@sveltejs/adapter-static builds a fully static site. Its documentation says to add export const prerender = true to the root layout so every page is prerendered. It also has a fallback option:
// svelte.config.js
import adapter from '@sveltejs/adapter-static';
export default {
kit: {
adapter: adapter({
fallback: '200.html'
})
}
};The fallback page is the entry point for any URL that was not prerendered. The adapter docs say plainly that this option has large negative performance and SEO impacts, and the single-page apps guide says SPA mode forces multiple network round trips and can harm SEO.
What happens in practice:
- A route that was not prerendered, such as a dynamic page nothing linked to at build time, gets the fallback file.
- The fallback file contains no page content. The browser downloads the JavaScript and renders the page.
- A crawler that does not run JavaScript sees an empty document.
- Depending on how your host serves the fallback, unknown URLs may also return
200with that same empty shell, so missing pages look like real ones. See SPA 404 status codes.
The adapter has a strict option, on by default, that fails the build if some pages were not prerendered and no fallback is set. Setting a fallback to make that error go away hides exactly the pages crawlers cannot read. Prefer to fix the prerendering: add entries, link the pages, or switch to an adapter that supports server rendering.
If you really need SPA mode for an app section, the SPA docs show how to re-enable ssr and prerender for the routes that should be crawlable, such as marketing pages and docs.
How to serve robots.txt in SvelteKit
There are two ways.
A static file. Files in the static folder are served as-is from the site root, so static/robots.txt becomes /robots.txt:
User-agent: *
Disallow: /account/
Sitemap: https://www.example.com/sitemap.xmlAn endpoint. If the rules depend on the environment, for example blocking everything on a preview deployment, use a +server.js route:
// src/routes/robots.txt/+server.js
import { PUBLIC_SITE_ENV } from '$env/static/public';
export const prerender = true;
export function GET() {
const body = PUBLIC_SITE_ENV === 'production'
? 'User-agent: *\nDisallow: /account/\n\nSitemap: https://www.example.com/sitemap.xml\n'
: 'User-agent: *\nDisallow: /\n';
return new Response(body, { headers: { 'Content-Type': 'text/plain' } });
}Do not have both a static file and an endpoint at the same path. Pick one. Then make sure your rules for AI crawlers say what you intend; robots.txt for AI crawlers has complete examples.
How to generate a sitemap
The SEO docs show a sitemap generated by a +server.js endpoint at src/routes/sitemap.xml/ that returns XML with an application/xml content type:
// src/routes/sitemap.xml/+server.js
export const prerender = true;
export async function GET() {
const posts = await getAllPosts();
const urls = posts
.map((p) => `<url><loc>https://www.example.com/blog/${p.slug}</loc><lastmod>${p.updated}</lastmod></url>`)
.join('');
return new Response(
`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls}</urlset>`,
{ headers: { 'Content-Type': 'application/xml' } }
);
}Use the same URL format as your trailingSlash setting, so the sitemap lists the canonical form of each page.
Titles, descriptions and canonical tags with svelte:head
SvelteKit's SEO docs say every page should have a unique <title> and <meta name="description"> inside <svelte:head>. A common pattern is to return the values from a load function and render them in the page or root layout:
<!-- src/routes/blog/[slug]/+page.svelte -->
<script>
let { data } = $props();
</script>
<svelte:head>
<title>{data.post.title}</title>
<meta name="description" content={data.post.description} />
<link rel="canonical" href={`https://www.example.com/blog/${data.post.slug}`} />
</svelte:head>Because SSR is on, these tags are in the server's HTML. With ssr = false they would only be added by JavaScript, and crawlers that do not render would miss them. The same applies to JSON-LD: render it in <svelte:head> on the server.
Keep one URL per page
SvelteKit removes trailing slashes by default and redirects to match your setting. The page options docs advise against trailingSlash = 'ignore', because /x and /x/ then become separate URLs. On static hosts that do not serve /a.html for /a, the adapter-static docs suggest trailingSlash = 'always'.
A checklist before you deploy
- Search the codebase for
ssr = falseand confirm each one is on a route that does not need to be crawled. - If you use adapter-static, check whether
fallbackis set and list which routes rely on it. - Fetch three page types with curl and confirm the content, title and description are in the HTML.
- Request a URL that does not exist and confirm it returns
404, not200. - Fetch
/robots.txtand/sitemap.xmlfrom production and check the status code and content.
Frequently asked questions
Is SvelteKit good for SEO?
Its defaults are. SvelteKit server-renders pages unless you turn it off, so crawlers that read raw HTML receive the full content and metadata.
Does ssr = false hurt AI crawlers?
Yes, for the routes it applies to. SvelteKit sends an empty shell, and crawlers that do not run JavaScript see no content. Keep SSR on for every page you want read.
Should I use adapter-static with a fallback page?
Only for app sections that do not need to be crawled. SvelteKit's adapter docs say the fallback option has large negative performance and SEO impacts. Prerender public pages instead.
Where does robots.txt go in a SvelteKit project?
Put it in the static folder to serve it from the root, or create a src/routes/robots.txt/+server.js endpoint if the rules need to change per environment.
Check a SvelteKit page as a crawler sees it
The fastest way to catch an SSR or fallback problem is to look at the HTML a crawler receives. Run the free check on a page from each route type, or compare the output with what you see in the browser using raw vs rendered HTML.
Filed under Platform guides. Topics: javascript, ssr, rendering, robots-txt, sitemaps