Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

94,000 static pages, no app server: building a world trade explorer

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

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.

📰 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.