Next.js static export, R2, HTTP Range requests and a few lessons about syncing a very large site.
I built Trade by Innerkore, a site for exploring what 226 countries and territories buy and sell, and what moves between any two of them.
The data changes about once a month, but the number of possible views is huge. So browsing runs no server code: pages are built ahead of time, and the browser fetches small data files when you dig deeper.
The stack
-
Next.js static export (
output: "export"), React and ECharts - Python + SQLite to collect and export the data
- GitHub Actions for the monthly refresh and deploys
- Cloudflare R2 for hosting
- One small Worker + D1 for newsletter sign-ups
1. Outgrowing static hosting limits
A page for each country and product category took the export to about 94,000 files. Cloudflare Pages allows 20,000 per deploy, so I moved to an R2 bucket on a custom domain.
R2 serves objects by exact key, so /country/CHN/ doesn't find index.html by itself. Rewrite and redirect rules handle that.
2. Syncing without breaking the live site
Upload order matters:
# 1. hashed JS/CSS first, so no page points at a missing file
rclone copy out/_next "r2:$BUCKET/_next" "${common[@]}"
# 2. then everything else (simplified)
rclone copy out "r2:$BUCKET" "${common[@]}" --exclude "_next/**"
# 3. delete old files last
rclone sync out "r2:$BUCKET" "${common[@]}" --delete-after
Two flags earned their place: --checksum --no-update-modtime. Only changed content gets written. Without the second one, my first run logged 93,000 errors as rclone tried to update modification times.
And because sync deletes whatever the build lacks, the job refuses to upload a suspiciously small build:
test "$(find out -type f | wc -l)" -gt 50000
test -f out/index.html
3. Tens of thousands of country pairs, a few hundred objects
Detailed data for every exporter and importer meant tens of thousands of small files, and writing that many got throttled.
So each exporter gets one pack: every importer's JSON gzipped separately and concatenated, plus an index of offsets.
# simplified
blob, index = bytearray(), {}
for importer, data in pairs:
member = gzip.compress(data, compresslevel=9, mtime=0)
index[importer] = [len(blob), len(member)]
blob += member
The browser fetches only the bytes it needs and decompresses them natively:
const [offset, length] = index[importer];
const res = await fetch(`${BASE}/packs/${exporter}.bin`, {
headers: { Range: `bytes=${offset}-${offset + length - 1}` },
});
const stream = res.body!.pipeThrough(new DecompressionStream("gzip"));
const pair = await new Response(stream).json();
No database, no API, no decompression library. This brought it down to 679 objects.
4. Shareable views on prerendered pages
Year, filters, sorting and opened chart boxes live in the query string, so a reload or shared link shows the same view.
I didn't want useSearchParams to stop components being prerendered, so I read the URL with useSyncExternalStore:
const useParam = (key: string) =>
useSyncExternalStore(
subscribe,
() => new URLSearchParams(window.location.search).get(key),
() => null, // the prerendered page shows defaults
);
Changes use history.replaceState, so picking filters doesn't fill the back history.
5. A treemap made of buttons
The product treemap is HTML, not canvas. It's a squarified layout with absolutely positioned <button>s, so each box can take keyboard focus, hold an icon and show a card on hover or long press.
The trade-off
Build time. A full build and upload takes over an hour. For data that changes monthly, I'll accept that in exchange for very little to operate. For hourly data, I'd choose differently.
Your turn
Have you pushed static export further than this? How do you cut build times with tens of thousands of pages? And if you try the site, tell me what feels slow or confusing.
Top comments (1)
I have not pushed static export anywhere near 94,000 pages, so the number that stopped me was the sync guard: refusing to upload a build under 50,000 files is the kind of cheap check that saves you from a self-inflicted outage, and it is exactly the check most people only add after the first bad deploy. The pack-file idea is the cleverest part for me — tens of thousands of small files down to 679 objects, and the browser fetching only the byte range it needs with no database and no decompression library. That is the trade most static-site builders never make: they pay for an API instead of letting HTTP Range requests do the work.
On build times, I have never had a build over an hour to optimize, but the hashed-assets-first upload order is the detail I would steal first. What would you change first if the data started changing weekly instead of monthly — would you shard the build, or accept the hour and just run it more often?