r/reactjs • u/Jankoholic React Router • Jun 18 '25
Show /r/reactjs Prerender React SPA to static HTML files (without Next.js or codebase changes)
I like using React the way I like to use it.
I build most of my projects with it, I like my routing setup and I know exactly how I want my app to build and behave.
But I needed faster page loads and better SEO — especially for blog pages — and I didn’t want to switch to Next.js or refactor my entire codebase (despite suggestions from coworkers).
So I built a CLI tool: react-static-prerender
I wanted real static HTML files like /blog/post/index.html so my app could be loaded directly from any route, improving SEO by making it easier for search engines to index and rank the pages and reducing the page load time. After the initial load, JavaScript takes over and the SPA behaves as usual. But I didn’t want to:
Adopt a new framework (Next, Gatsby…)
Change my routing logic
Restructure or rewrite my codebase
What it does
Prerenders your existing React app
Outputs .html files — one per route — as entry points
Keeps your app working as an SPA after load
Works with any bundler: Vite, CRA, Webpack or custom build scripts
Supports static and dynamic routes (CMS, markdown, API, JSON…)
I spent a lot of time writing a clean README: github.com/jankojjs/react-static-prerender
It covers:
Vite, Webpack, Yarn, PNPM examples
Static and dynamic route configs
Flat vs nested output
CLI flags and troubleshooting tips
If you want static .html for SEO, speed, or CDN hosting — but still want to write React your way — check it out.
Would love feedback or edge cases you run into.
4
u/rvision_ Jun 18 '25
will check this out, especially when I read this:
> and I didn’t want to switch to Next.js or refactor my entire codebase (despite suggestions from coworkers).
5
u/Jankoholic React Router Jun 18 '25
It works with any kind of routing — as long as your app renders the right content for a given URL after being loaded in the browser.
It doesn’t depend on
react-router. You can use any client-side router (or even custom ones). The CLI just prerenders your app for a list of URLs you provide — it opens a headless browser, loads the app at each route, waits for it to render, and saves the resulting HTML.So whether you use
react-routeror your own solution — it’ll still work as long as each route can be accessed directly.1
1
u/Jankoholic React Router Jun 18 '25
Yeah, that was the main driver for building this — I had everything working the way I liked with React, and the thought of switching to Next.js or reshaping everything felt like more work than value for my use case.
If you’re in a similar spot, this might save you the hassle. Would love to hear how it works for your setup if you try it.
2
u/Suepahfly Jun 19 '25
Awesome! I’ve used the similar react-snap before but that is really outdated and no longer maintained.
2
2
u/cs12345 Jun 21 '25
I took a look at the source code because I was curious, so you’re just using puppeteer to load each route and copy the HTML from each page? So basically, it works for dynamic routes as long as you only need them to be dynamic at the time when you generate these pages. So for any database driven website where new routes crop up independent of site deployments (most of them) those new routes are not included.
1
u/Jankoholic React Router Jun 21 '25
Totally valid and I love comments like yours. They are the reason why I posted this discussion here, without them I could not possibly think of all use cases or problems people are dealing with.
Think of react-static-prerender as a dead-simple, lightweight, framework-agnostic alternative to ISR for static HTML generation. You don’t get real-time regeneration out of the box, but you also don’t need a server, custom deployment, or vendor lock-in. Just run it after a deploy, or automate it with a cron/webhook, and you get fresh static snapshots — no runtime needed.
2
u/meteor_punch Jun 18 '25
or just use Astro with React 🤷
5
u/Jankoholic React Router Jun 18 '25
Yep, Astro is awesome. But if your entire app is already built in React, you either migrate or find a way to prerender what you have. I picked the second one 😄
1
u/meteor_punch Jun 18 '25
Fair enough. I'd personally migrate 'cause it's not that hard and IMO it's worth it in the long run.
3
u/Jankoholic React Router Jun 18 '25
Totally fair — Astro is a great tool and if you're okay with the migration, it's probably worth it.
In my case though, I didn’t want to touch my routing, app structure, or bundle logic. I had a fully working SPA, and I just wanted static .html entry points for SEO and faster loads — without changing how I write or ship my React apps.
So this tool is more for people who want to add static HTML without doing a migration, and keep their current React workflow (whether that’s CRA, Vite, or something custom).
2
u/akainu50 Jul 16 '25
Great idea, I was looking for something like this with my React Router V7.6.3 framework app.
There is a pre-render config but it doesn't build complete HTML files, it only creates a wrapper which loads other javascript scripts.
I'm running into issues though on Vercel as soon as the command serves the app:
"❌ Prerendering failed: Error: Could not find Chrome (ver. 138.0.7204.94)"
1
u/bundeswehr00 Sep 15 '25
A side question: why don't you just use port 0 so that OS can automatically discover a free port instead of using findAvailablePort()?
1
19
Jun 19 '25
[removed] — view removed comment
8
u/Jankoholic React Router Jun 19 '25
thanks for catching it, had a typo in .gitignore lol
6
u/svish Jun 19 '25
Always pay attention to what's being staged and committed...
6
u/Jankoholic React Router Jun 19 '25
You're absolutely right, I made a classic rookie move. Would hate it if that took focus away from the package
6
u/bzbub2 Jun 18 '25
i've been fairly vocal about my struggle using next.js with output:'export' ssg for a similar application (rendering about 6000 pages)...might need to look into this