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.
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
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).
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
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.
2
u/meteor_punch Jun 18 '25
or just use Astro with React 🤷
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
u/launchfastshop May 21 '26
it did not work, says could not load chrome. we use encited now
19
u/[deleted] Jun 19 '25
[removed] — view removed comment