Browse Source

feat: reload a tab that comes back after a new deploy

A page left open on a phone kept running old code for days, so fixed
bugs kept showing up there. Each build now carries a stamp (the commit
on Vercel), /api/version serves the live one, and a tab that returns
after more than a minute away reloads if they differ, at most once per
build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QEPoSHwHCYMNpm2iKxY9hL
Claude 1 tháng trước cách đây
mục cha
commit
8cb45bb56f
4 tập tin đã thay đổi với 60 bổ sung và 0 xóa
  1. 6 0
      app/api/version/route.ts
  2. 2 0
      app/layout.tsx
  3. 46 0
      components/update-on-return.tsx
  4. 6 0
      next.config.js

+ 6 - 0
app/api/version/route.ts

@@ -0,0 +1,6 @@
+// The build that is live, for tabs left open to compare with their own.
+export const dynamic = "force-static";
+
+export function GET() {
+  return Response.json({ build: process.env.SITE_BUILD });
+}

+ 2 - 0
app/layout.tsx

@@ -8,6 +8,7 @@ import { Backdrop } from "@/components/backdrop/backdrop";
 import { CursorLens } from "@/components/glass/cursor-lens";
 import { SiteNav } from "@/components/nav/site-nav";
 import { SiteFooter } from "@/components/site-footer";
+import { UpdateOnReturn } from "@/components/update-on-return";
 import { FontLoader } from "@/components/font-loader";
 import { deferredFaces, fontDisplay, fontName, fontText } from "@/config/fonts";
 import { siteConfig } from "@/config/site";
@@ -145,6 +146,7 @@ export default function RootLayout({
           </main>
           <SiteFooter />
           <CursorLens />
+          <UpdateOnReturn />
         </ThemeProvider>
         <FontLoader faces={deferredFaces} />
         <Analytics />

+ 46 - 0
components/update-on-return.tsx

@@ -0,0 +1,46 @@
+"use client";
+
+import { useEffect } from "react";
+
+/** How long a tab has to be away before it asks whether it is out of date. */
+const AWAY = 60_000;
+
+/**
+ * Reloads a tab that comes back after a new deploy, so a page left open on a
+ * phone for days never shows bugs that have since been fixed. It reloads at
+ * most once per build.
+ */
+export function UpdateOnReturn() {
+  useEffect(() => {
+    let hiddenAt = 0;
+    const check = async () => {
+      try {
+        const res = await fetch("/api/version", { cache: "no-store" });
+        const { build } = (await res.json()) as { build?: string };
+
+        if (!build || build === process.env.SITE_BUILD) return;
+        if (sessionStorage.getItem("reloaded-for") === build) return;
+        sessionStorage.setItem("reloaded-for", build);
+        location.reload();
+      } catch {}
+    };
+    const onVisibility = () => {
+      if (document.hidden) hiddenAt = Date.now();
+      else if (hiddenAt && Date.now() - hiddenAt > AWAY) check();
+    };
+    // Restored from the back/forward cache: as good as a tab coming back.
+    const onShow = (e: PageTransitionEvent) => {
+      if (e.persisted) check();
+    };
+
+    document.addEventListener("visibilitychange", onVisibility);
+    window.addEventListener("pageshow", onShow);
+
+    return () => {
+      document.removeEventListener("visibilitychange", onVisibility);
+      window.removeEventListener("pageshow", onShow);
+    };
+  }, []);
+
+  return null;
+}

+ 6 - 0
next.config.js

@@ -1,6 +1,12 @@
+// Names this build, so a tab left open across a deploy can tell it is stale
+// (see components/update-on-return.tsx).
+const SITE_BUILD =
+  process.env.VERCEL_GIT_COMMIT_SHA || `local-${Date.now().toString(36)}`;
+
 /** @type {import('next').NextConfig} */
 const nextConfig = {
   poweredByHeader: false,
+  env: { SITE_BUILD },
   experimental: {
     // Ship the (small) stylesheet inline so first paint never waits on a CSS request.
     inlineCss: true,