Urgent.News

What's breaking now, across hundreds of outlets.

AI

Make your React/Vite SPA readable by AI crawlers without rewriting it

Short answer: a React or Vite app sends an empty <div id="root"></div> and builds the page in the browser. AI crawlers don't run that JavaScript, so to ChatGPT, Claude and Perplexity your site has no text. Pre-render it: build the HTML of each route at deploy time with a 15-line script, and React takes over in the browser as usual. You don't need to rewrite anything in Next.js. When I audited 286…

React and Vite single-page applications send an empty div with id="root" to the browser, where JavaScript builds the page. AI crawlers like ChatGPT, Claude, and Perplexity don't execute the JavaScript, resulting in AI crawlers seeing a blank page with no text. To make these sites readable by AI crawlers without rewriting the code, pre-render the HTML of each route at deploy time.

Here are the four steps to pre-render a Vite and React SPA:

1. Add a placeholder in the index.html file: `<div id="root"><!--app--></div>`

2. Create a `src/entry-server.jsx` file to render a route to a string using `renderToString` from `react-dom/server` and `StaticRouter` from `react-router`. Export this function as `render`.

3. In `src/main.jsx`, replace `createRoot` with `hydrateRoot` so React can reuse the HTML that's already there instead of building it from scratch.

4. Add `prerender.js` at the project root and list your public routes. Read the HTML from `dist/index.html`, replace the placeholder with the rendered route, and save the HTML for each route in the `dist` folder.

After completing these steps, run the build script in `package.json` that includes Vite for building the app twice and the `prerender.js` script to generate the pre-rendered HTML files. Then, check your site using a command like `curl -s -A "GPTBot/1.1" https://yourdomain.com/ | grep -i "your main heading"`. If the output is blank, the pre-rendering process was successful.

Some potential issues to be aware of include using `window` or `localStorage` at the top of a component, which won't be available in the Node build. Move this code into a `useEffect` hook, which only runs in the browser. Also, be cautious of changes in `Date.now()` or `Math.random()` that could cause a hydration mismatch in React.

This pre-rendering technique works well for portfolio sites, landing pages, or small business websites where content doesn't change frequently. However, for sites with dynamic content that updates per visitor or every few minutes, server-side rendering with a framework like Next.js or React Router's framework mode would be more appropriate.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in AI

I Built Two AI Tools for Developers – Here's What I'm Learning Along the Way

Building AI-powered tools sounds exciting, but turning an idea into something developers can actually use is a completely different experience.

  • Elite Article Engine focuses on generating technical articles with research-driven content.
  • MakeYourUI generates frontend code from natural language inputs for various tech stacks.
  • Feedback sought on limitations, preferences, and improvements for AI tools.

More from Monday 23 November →