TanStack Start Sitemap Autogeneration and Analytics Setup

| October 30, 2024

Generate a sitemap.xml from your TanStack Start route tree at build time and wire up privacy-friendly analytics.

This post contains affiliate links for tools I use in production. If you buy through them I earn a commission at no extra cost to you. Recommendations are based on my own experience.

Table of Contents

Generating a Sitemap from the Route Tree

A sitemap plugin that reads your generated route tree can produce sitemap.xml automatically, so you are not hand-maintaining a list of URLs that drifts from your actual routes.

Step 1: Installation

Terminal window
pnpm add -D tanstack-start-sitemap

Check the package’s own README for the version compatible with your TanStack Start version before installing; sitemap plugins in this ecosystem move fast and can lag behind Start’s config format.

Step 2: Configure the Plugin

Add the plugin to your Vite config:

vite.config.ts
import { defineConfig } from "vite";
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import { sitemapPlugin } from "./plugins/vite-sitemap-plugin";
import tsConfigPaths from "vite-tsconfig-paths";
export default defineConfig({
plugins: [
tsConfigPaths({
projects: ["./tsconfig.json"],
}),
tanstackStart(),
sitemapPlugin({
hostname: "https://example.com", // Replace with your site's URL
}),
],
});

Step 3: Customize Sitemap Options

OptionTypeDefaultDescription
hostnamestringRequiredThe base URL of your site (e.g. https://example.com).
routeTreePathstring'src/routeTree.gen.ts'Path to your generated route tree. The plugin checks several common locations.
routes{ [key: string]: { changefreq?: string; priority?: number; lastmod?: string } }{}Per-route overrides.
defaultChangefreq'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never''weekly'Applied when a route has no override.
defaultPrioritynumber0.5Applied when a route has no override.

Example override for specific routes:

sitemapPlugin({
hostname: "https://example.com",
routes: {
"/about": { changefreq: "monthly", priority: 0.8 },
"/contact": { changefreq: "yearly", priority: 0.3, lastmod: "2026-01-01" },
},
});

Troubleshooting

  • Route tree file not found: check routeTreePath against where TanStack Start actually writes routeTree.gen.ts in your project. Run pnpm run build once first; the file is generated during the build, not checked into git.
  • Empty or stale sitemap: the plugin reads the route tree file from disk, so if you’re running it against a cached build output, delete .output and .vinxi (or .vite cache directories) and rebuild.

Setting Up Analytics

react-ga is unmaintained and wraps the old Universal Analytics API, which Google shut down in 2024. For GA4, use the official gtag script directly, loaded through Astro’s <script> tag or a partytown-forwarded call so it does not block the main thread.

<!-- In your root layout, loaded once -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag("js", new Date());
gtag("config", "G-XXXXXXX");
</script>

If you’d rather not send full clickstream data to Google, a privacy-first alternative such as Plausible or Umami gives you pageviews and referrers without a cookie banner, and both ship a script under 1kb.

Debugging this in production

A sitemap that silently omits new routes, or an analytics snippet that never fires, both fail quietly: no error in the console, just missing data days later when you check search console or your dashboard. A session replay and error tracker such as Sentry or LogRocket catches build-time regressions and client script failures before they cost you a week of missing traffic data, and both have a free tier that covers a side project.


If you are taking this into production, the next steps below cover auth, data and observability, and the newsletter is where the Astro SaaS boilerplate ships first.

Next steps: scaling to production

If you take this into production, these are the pieces I would add first.

  • Clerk Clerk provides drop-in authentication and user management components. Hosted auth saves the login, session and org code you would otherwise maintain.
  • Supabase Supabase is a hosted Postgres platform with authentication and storage built in. Postgres with row-level security, so the data layer is ready for multi-tenant apps.
  • Sentry Sentry captures errors and performance traces from production applications. Errors and slow transactions from real users, with source maps, before customers report them.

Production-ready Astro + TanStack architecture

Get the architecture cheat sheet and join the waitlist for the Astro SaaS boilerplate.