Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Keep previous build assets for X days

Blue Mouse0cd34bc09252
diff --git a/docs/.gitignore b/docs/.gitignore
index ca8600674..8b47155a6 100644
--- a/docs/.gitignore
+++ b/docs/.gitignore
@@ -1,4 +1,5 @@
node_modules/
.next/
+.static-archive/
.source/
out/
diff --git a/docs/package.json b/docs/package.json
index 2b5eae750..56e443224 100644
--- a/docs/package.json
+++ b/docs/package.json
@@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev -p 5007",
- "build": "next build",
+ "build": "next build && node scripts/retain-static.mjs",
"start": "next start -p 5007 -H 0.0.0.0",
"format": "oxfmt",
"format:check": "oxfmt --check"
diff --git a/docs/scripts/retain-static.mjs b/docs/scripts/retain-static.mjs
new file mode 100644
index 000000000..78cb9dc65
--- /dev/null
+++ b/docs/scripts/retain-static.mjs
@@ -0,0 +1,68 @@
+// Keeps static assets from previous builds available after a redeploy.
+//
+// `next build` regenerates `.next/static` from scratch, so any visitor holding
+// HTML from an earlier deploy (restored tab, browser cache) hits 404s on every
+// content-hashed chunk. The page then renders as inert prerendered HTML: no
+// hydration, no click handlers, no ⌘K search. This script archives each
+// build's static assets and merges past builds back into `.next/static`, so
+// stale HTML keeps working. Assets unseen for RETENTION_DAYS are pruned.
+import fs from "node:fs";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+
+const RETENTION_DAYS = 60;
+
+const docsRoot = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
+const staticDir = path.join(docsRoot, ".next", "static");
+const archiveDir = path.join(docsRoot, ".static-archive");
+
+function mergeMissing(from, to) {
+ if (!fs.existsSync(from)) return 0;
+ let copied = 0;
+ for (const entry of fs.readdirSync(from, { withFileTypes: true })) {
+ const src = path.join(from, entry.name);
+ const dest = path.join(to, entry.name);
+ if (entry.isDirectory()) {
+ copied += mergeMissing(src, dest);
+ } else if (!fs.existsSync(dest)) {
+ fs.mkdirSync(to, { recursive: true });
+ fs.copyFileSync(src, dest);
+ copied += 1;
+ }
+ }
+ return copied;
+}
+
+function prune(dir, cutoffMs) {
+ if (!fs.existsSync(dir)) return 0;
+ let removed = 0;
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
+ const p = path.join(dir, entry.name);
+ if (entry.isDirectory()) {
+ removed += prune(p, cutoffMs);
+ if (fs.readdirSync(p).length === 0) fs.rmdirSync(p);
+ } else if (fs.statSync(p).mtimeMs < cutoffMs) {
+ fs.unlinkSync(p);
+ removed += 1;
+ }
+ }
+ return removed;
+}
+
+try {
+ if (!fs.existsSync(staticDir)) {
+ console.warn("retain-static: no .next/static found, skipping");
+ process.exit(0);
+ }
+
+ const restored = mergeMissing(archiveDir, staticDir);
+ const archived = mergeMissing(staticDir, archiveDir);
+ const pruned = prune(archiveDir, Date.now() - RETENTION_DAYS * 24 * 60 * 60 * 1000);
+
+ console.log(
+ `retain-static: restored ${restored} asset(s) from previous builds, archived ${archived} new, pruned ${pruned} older than ${RETENTION_DAYS}d`,
+ );
+} catch (err) {
+ // Never fail the build over asset retention.
+ console.warn("retain-static: failed, continuing without retention:", err);
+}