All writing
3 min

Deploys should add files, not swap them

Why a visitor's open tab breaks right after you ship a Next.js app you host yourself, what frontend client skew is, and the one-folder fix.


Someone has your site open. You ship an update. They click a button, and the page turns into an error.

The old version was fine. The new one is fine. It broke because their tab was open while you deployed. You'll probably never see it: after a deploy, you refresh. Your visitors don't.

Loading...

What the visitor sees

The page says "This page couldn't load". In DevTools, the console shows:

layout
ChunkLoadError: Failed to load chunk /_next/static/chunks/04leuz213l84f.js

and the Network tab shows a 404 for that file.

It has a name: frontend client skew

Version skew is when two parts that depend on each other run different versions for a while. Malte Ubl explains it best. Here the two parts are the open tab (still on the old version) and your server (already on the new one).

Loading...
The kinds of version skew
The kinds of version skew

Why the file is gone

Two good Next.js features meet one bad deploy.

  • •
    Code loads when it's needed. Code behind a button can be its own file, downloaded when you click. The open tab remembers that file's name.
  • •
    Files are named after their contents. Change one word and the file gets a new name. In my test, changing "v1" to "v2" replaced 2 of 11 JavaScript files.
  • Now the deploy:

    bash· 3 lines
    1git pull
    2npm run build      # deletes the old .next folder first
    3pm2 restart app

    The old file names are gone. The open tab asks for one, the server answers 404, and the page breaks. Nothing in Next.js is wrong. The deploy swaps the files instead of adding to them.

    What other people do

  • Reload on error. Hides the crash, but the visitor loses what they were doing.
  • Next.js deploymentId. Notices the mismatch and does a full reload. State is lost.
  • Vercel Skew Protection. Sends old tabs to the old deployment, for a limited time (12 hours by default on Pro).
  • Kamal asset bridging. Old and new assets share one folder during a deploy.
  • Just keep the files. The Next.js self-hosting guide: "if you just keep adding them to your existing static file folder (or CDN bucket), there will be no version skew."
  • Loading...
    What other people do
    What other people do

    How NextDeploy does it

    Each app has one folder, shared_static. Before a new release gets traffic, its static files are copied in. Old files stay. Same name means same contents, so nothing collides.

    Caddy serves every /_next/static/* request from that folder:

    layout· 5 lines
    handle_path /_next/static/* {
    	root * /path/to/app/shared_static
    	header Cache-Control "public, max-age=31536000, immutable"
    	file_server
    }

    The old tab asks for its old file, and it's still there: 200, not 404.

    Loading...
    fig-06: a swap deploy compared with an add deploy
    fig-06: a swap deploy compared with an add deploy

    So the folder doesn't grow forever, each deploy keeps the files the five newest releases use and deletes the rest.

    Loading...
    Cleanup keeps what the newest five releases use
    Cleanup keeps what the newest five releases use

    What this doesn't fix

    It fixes missing files only. An old tab can still call a Server Function the new build doesn't have, or use page data it prefetched from the old version. Those need the deploymentId reload or Vercel-style version locking.

    Loading...
    What shared_static fixes, and what it doesn't
    What shared_static fixes, and what it doesn't

    The rule

    Deploys should add files, not swap them.

    A two-minute check: open your site in a private window, deploy, then click something that loads code lazily. If it breaks, look at what your deploy does to .next/static.

    NextDeploy is open source: github.com/aynaash/NextDeploy.


    Sources

  • Malte Ubl, Version Skew, 21 Jun 2023
  • Next.js self-hosting guide, Version Skew and Immutable Assets
  • Next.js deploymentId
  • —views

    Comments

    Loading...