Most React SEO problems come from one fact: a single-page app built with Vite sends the same near-empty index.html for every URL, and the page appears only when the JavaScript runs. Google renders it later. The crawlers behind ChatGPT, Claude and Perplexity don’t, according to Vercel’s study of AI crawlers, and neither do most link previews.
This guide turns a Vite React app into one that sends real HTML for every page, with React Router’s prerendering. Every example was built with react-router build, served as static files and crawled before and after.
The examples use one content module so they fit together. Replace it with your CMS or API:
// app/lib/guides.ts
export const site = process.env.SITE_URL ?? 'https://example.com'
export const guides = [
{
slug: 'first-guide',
title: 'First guide',
description: 'The first guide on Example.',
body: 'Some useful words about the first topic, written for people and agents.',
},
]
export const getGuide = (slug: string) => guides.find((guide) => guide.slug === slug)
What do crawlers see in a React single-page app?
An empty shell. We crawled a fresh React Router app in its single-page mode, the setup most Vite React apps have, with the seo CLI and JavaScript turned off. The crawl reached one page out of three. It had no title or H1 and no links for the crawl to follow, and a made-up URL answered 200 instead of 404.
The same crawl with --rendering on found all three pages with their titles, because a browser ran the JavaScript first. That gap is the problem. Google sees roughly the second crawl, some time later. The AI crawlers see the first.
Run both crawls on your own app before you change anything. Serve the build with any static server, then:
npx -y seo crawl http://localhost:4173 --rendering off
npx -y seo crawl http://localhost:4173 --rendering on
Which React pages should be prerendered?
Every page you want found. A prerendered page is written to an HTML file during the build, so a crawler gets the content and links on the first request with no JavaScript. React Router does it in framework mode, which runs on Vite. Set ssr to false for static hosting and list the paths:
// react-router.config.ts
import type { Config } from '@react-router/dev/config'
import { guides } from './app/lib/guides'
export default {
ssr: false,
prerender: ['/', '/sitemap.xml', '/llms.txt', ...guides.map((guide) => `/guides/${guide.slug}`)],
} satisfies Config
react-router build prints a line for each file it writes, such as Prerender (html): /guides/first-guide -> build/client/guides/first-guide/index.html. A page that isn’t in that list is still a single-page app page, so check it if you want it in search. With prerender: true React Router writes every path without a parameter, but you still list dynamic paths like /guides/:slug yourself. The pre-rendering guide covers both.
Route data moves from clientLoader to loader, which runs during the build:
// app/routes/guide.tsx
import { data } from 'react-router'
import { Seo } from '../components/Seo'
import { getGuide, site } from '../lib/guides'
import type { Route } from './+types/guide'
export function loader({ params }: Route.LoaderArgs) {
const guide = getGuide(params.slug)
if (!guide) throw data(null, { status: 404 })
return guide
}
export default function Guide({ loaderData: guide }: Route.ComponentProps) {
const path = `/guides/${guide.slug}`
const jsonLd = {
'@context': 'https://schema.org',
'@type': 'Article',
headline: guide.title,
description: guide.description,
url: `${site}${path}`,
}
return (
<main>
<Seo title={guide.title} description={guide.description} path={path} />
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd).replace(/</g, '\\u003c') }}
/>
<h1>{guide.title}</h1>
<p>{guide.body}</p>
</main>
)
}
If you’d rather render on a server for every request, TanStack Start and Next.js do that too. The Next.js SEO guide covers the App Router.
How do I set titles, descriptions and canonicals in React?
Render the tags in the page. Since React 19, a <title>, <meta> or <link> in any component goes into the document head, and prerendering writes it into the HTML file. You don’t need react-helmet or react-helmet-async for this. One small component keeps every page consistent:
// app/components/Seo.tsx
import { site } from '../lib/guides'
type Props = { title: string; description: string; path: string }
export function Seo({ title, description, path }: Props) {
const url = `${site}${path}`
return (
<>
<title>{`${title} | Example`}</title>
<meta name="description" content={description} />
<link rel="canonical" href={url} />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:url" content={url} />
<meta property="og:type" content="website" />
</>
)
}
Render exactly one Seo per page. React puts every <title> it finds into the head, and two titles leave Google to pick one (the title reference warns about this). Give <title> one string, such as the template literal above. React expects its children to be a single piece of text.
Set the canonical on every page, including the home page. A canonical copied from a layout tells Google every page is the home page.
How do I add JSON-LD to a React page?
Add a <script type="application/ld+json"> in the page, as in the route above. Escape < as \u003c so a value in your data can’t close the script tag early. Prerendering writes the script into the HTML, so crawlers that don’t run JavaScript read it too.
Describe only what the page shows. An Article for a guide, a Product for a product page, and nothing for a page that is neither. Test the output in Google’s Rich Results Test.
How do I add a sitemap to a React app?
Write it from the same data as your routes, as a resource route that is prerendered like a page. Add it to app/routes.ts with route('sitemap.xml', 'routes/sitemap.ts') and to the prerender list:
// app/routes/sitemap.ts
import { guides, site } from '../lib/guides'
export function loader() {
const urls = [`${site}/`, ...guides.map((guide) => `${site}/guides/${guide.slug}`)]
const body = urls.map((url) => ` <url><loc>${url}</loc></url>`).join('\n')
return new Response(
`<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${body}\n</urlset>\n`,
{ headers: { 'Content-Type': 'application/xml' } },
)
}
The build writes build/client/sitemap.xml. Every URL in it comes from the same list as the prerendered pages, so the sitemap can’t list a page that doesn’t exist. Leave lastmod out unless you know the real date each page changed.
llms.txt works the same way, as route('llms.txt', 'routes/llms.ts'), with a loader that returns plain text listing your main pages. Agents read it; search engines don’t use it for ranking.
What should robots.txt say in a React app?
Allow everything and point at the sitemap. Put the file in public, which Vite copies to the build as it is:
User-agent: *
Allow: /
Sitemap: https://example.com/sitemap.xml
Don’t block /assets or your JavaScript files. Google needs them to render any page that still depends on JavaScript.
How do I return real 404s from a React app?
Stop sending index.html for every URL. Most single-page app setups rewrite every path to index.html with a 200 status, so a deleted page looks like a live one and Search Console can report it as a soft 404. Once every page is prerendered, you don’t need that rewrite. Serve a 404 page with a 404 status instead.
When / is prerendered, React Router also writes __spa-fallback.html, which shows your route error boundary for any path it doesn’t know. Copy it to 404.html after the build:
{
"scripts": {
"build": "react-router build && cp build/client/__spa-fallback.html build/client/404.html"
}
}
Then tell your host to serve it. On Cloudflare Workers static assets that is "not_found_handling": "404-page", which answers with the nearest 404.html and a 404 status (Cloudflare’s 404 page docs). In our test a made-up URL then returned 404 and showed the “could not be found” page from the root error boundary.
Use normal paths, not hash URLs like /#/pricing. Google ignores everything after the #, so it treats every hash URL as the home page (Google’s JavaScript SEO basics).
How do I check a React build before it ships?
Crawl it without JavaScript. Build with your local address as the site URL, so canonicals and sitemap URLs point at the copy you are crawling, and serve the files:
SITE_URL=http://localhost:4173 npm run build
npx -y sirv-cli build/client --port 4173
In a second terminal:
npx -y seo crawl http://localhost:4173 --rendering off --fail-on medium
On our test app, the same crawl went from one page with three high issues (no title, no links, unknown URLs answering 200) to every page with no high or medium issues. Your agent can run it for you after each change; ask it in plain words, like the prompts further down. SEO for developers covers staging and CI.
Sources
Tested with React 19.3.1, React Router 8.5.3 and Vite 8.4.1: