Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 6 min read

Is React bad for SEO? What crawlers actually get from your app

Every few months someone on a team asks it: "Is React bad for SEO?" The short answer is no. React isn't the problem. Empty HTML is. This post shows how to check what crawlers actually get from your React app, why it mat

Every few months someone on a team asks it: "Is React bad for SEO?" The short answer is no. React isn't the problem. Empty HTML is.

This post shows how to check what crawlers actually get from your React app, why it matters beyond Google, and how to fix it with a small pre-render step on a Vite site. We also cover the trade-offs, because the fix isn't free.

Where the myth comes from

A new React app on Vite renders in the browser. The server sends one small HTML file, and a script draws the page:

<!doctype html>
<html lang="en">
  <head>
    <title>Acme</title>
    <script type="module" src="/assets/index-a1b2c3.js"></script>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

People never notice, because the browser runs the script in a blink. But anything that reads only the HTML gets a title and an empty div. No headings, no text, no links to your other pages.

So the honest version of the question is: does your page's content arrive in the HTML, or only after JavaScript runs?

What Google does with it

Google can read a client-rendered page. It just takes longer. Google's own JavaScript SEO docs describe three phases: crawl, render, index.

  • Googlebot fetches the HTML and queues the links it finds there.
  • Pages that answer 200 wait in a render queue. Google says that can be a few seconds, but it can take longer.
  • When resources allow, an up-to-date Chromium runs your JavaScript, and Google indexes the rendered page.

A few things can stop a page before it renders. A noindex in the first HTML may mean Google skips rendering, so a tag your JavaScript removes later may never be removed for Google. An error status can skip rendering too. And Google won't run JavaScript from files that robots.txt blocks.

Google is the patient reader

Google's docs also say server-side rendering or pre-rendering is still a great idea, because not all bots can run JavaScript.

That's the part people miss. Many search engine crawlers, AI tools that read pages to answer questions, and the apps that draw link previews in chats read the HTML and stop. For a client-only app, they get the empty div above. Your share tags often arrive late too, so the link preview comes out blank.

Speed is the other half. With the content in the HTML, the browser can paint the page before the JavaScript loads, which helps on slow phones.

Check your own site in one minute

You don't need any tools for the first check.

  1. Open the page and view its source (Ctrl+U on Windows and Linux, Option+Command+U in Chrome on a Mac). Search for a sentence you can see on the page. If it isn't in the source, readers that don't run JavaScript don't see it either.
  2. Or from a terminal:
curl -s https://example.com/ | head -n 40
  1. For Google's own answer, use URL Inspection in Search Console: Test live URL, then View tested page. It shows the HTML after Google rendered it, plus a screenshot.

Check the home page, one key page and one blog post. Client-only rendering usually affects every page the same way.

The fix: pre-render your routes at build time

If your pages are known when you build (a business site, a portfolio, docs, a blog), you don't need a server. Render each route to an HTML file during the build, then let React take over in the browser.

Vite's SSR guide covers the pattern. Here's a minimal version for a Vite app whose App declares its pages with React Router's <Routes> and <Route>. First, a server entry that renders one URL to a string:

// src/entry-server.jsx
import { renderToString } from "react-dom/server";
import { StaticRouter } from "react-router";
import App from "./App";

export function render(url) {
  return renderToString(
    <StaticRouter location={url}>
      <App />
    </StaticRouter>
  );
}

In index.html, add two placeholders, one in the head and one inside the root:

<head>
  <meta charset="UTF-8" />
  <!--app-head-->
  <script type="module" src="/src/main.jsx"></script>
</head>
<body>
  <div id="root"><!--app-html--></div>
</body>

Then a small script writes one HTML file per route, each with its own title and description:

// prerender.js (run with "type": "module" in package.json)
import fs from "node:fs";
import path from "node:path";
import { render } from "./dist/server/entry-server.js";

const template = fs.readFileSync("dist/client/index.html", "utf-8");
const pages = {
  "/": { title: "Handmade lamps", description: "Lamps made to order." },
  "/about": { title: "About us", description: "Who makes the lamps." },
  "/pricing": { title: "Pricing", description: "How we price a lamp." },
};

for (const [url, meta] of Object.entries(pages)) {
  const head = `<title>${meta.title}</title>
    <meta name="description" content="${meta.description}" />`;
  const html = template
    .replace("<!--app-head-->", head)
    .replace("<!--app-html-->", render(url));
  const file = url === "/" ? "index.html" : `${url.slice(1)}/index.html`;
  const out = path.join("dist/client", file);
  fs.mkdirSync(path.dirname(out), { recursive: true });
  fs.writeFileSync(out, html);
}

The build becomes three steps:

vite build --outDir dist/client
vite build --ssr src/entry-server.jsx --outDir dist/server
node prerender.js

On the client, swap createRoot for hydrateRoot and wrap App in BrowserRouter, so React picks up the HTML that's already there instead of drawing it again:

// src/main.jsx
import { hydrateRoot } from "react-dom/client";
import { BrowserRouter } from "react-router";
import App from "./App";

hydrateRoot(
  document.getElementById("root"),
  <BrowserRouter>
    <App />
  </BrowserRouter>
);

Run the curl check again. Each route's title, heading and text are in the source now, on any static host that serves about/index.html for /about.

Two things to add before you ship it:

  • Canonical and share tags. Put them in the same head string as the title. Google can read a canonical set by JavaScript, but says HTML is the best place for it, and link previews only read the HTML.
  • A real page list. A hard-coded map goes stale. Generate it from your router config or your content folder, and write sitemap.xml from the same list. If your titles or descriptions can contain quotes or <, escape them first.

If you'd rather not wire this by hand, React Router's framework mode can pre-render the paths you list, and React has a prerender API in react-dom/static for the same job.

Trade-offs

Pre-rendering gives every reader the full page from cheap static hosting. The cost is a rebuild whenever content changes. That's fine for content that changes a few times a day. It's wrong for prices or stock that change by the minute.

Server rendering builds the HTML on each request. It suits pages that differ per visitor or change constantly. The cost is a server to run, and code that has to work without window.

Hydration bugs come with both. Anything that reads the browser at render time (window, localStorage, the current time) can render differently on the server and the client. Move it into an effect.

Serving bots a separate rendered copy is called dynamic rendering. Google calls it a workaround and not a recommended solution. Pre-rendering sends the same HTML to everyone, so it isn't cloaking.

Leaving it client-only is fine for an app behind a login. Nobody needs to find your settings page in search.

And whatever you pick, the basics still apply: real <a href> links, one address per view with the History API (not # fragments), a real 404 status for missing pages, and main text in the first render, not behind a click or a scroll. Google doesn't click or scroll.

Where we landed

We build Xodia, a visual website builder for real React code on Vite, so we had to answer this for the sites it builds. Sites from our templates are pre-rendered: each page is an HTML file with its title, description, headings, text and links, on any static host. xodia.ai itself is built the same way.

We wrote the longer version, with the checks step by step, the full list of fixes and an FAQ, here: Is React bad for SEO? How to make a React site search engines can read.

How do you ship your React sites today: client-only, pre-rendered or server-rendered?

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.