This site's deploy flow used to be very simple: git pull, next build, restart the server. The problem is that next build overwrites the .next folder directly, while the live server is reading it.
During the ten-odd to few-dozen seconds of the build, the old live process holds its old manifest and reads a folder that is being rewritten; if the build fails midway, .next is left in an incomplete state, and the site stays broken until someone notices. In July the same kind of thing happened once: .next was already the new version, but the old process had not been replaced and used its old manifest to look for JS files, every chunk returned 404/500, and the whole page went white.
So on 11/03 I wanted to do a very standard thing: an atomic deploy.
First version: build beside it, then rename into place
The method is seen everywhere:
next buildoutputs to.next-build(live keeps serving from.next)- Only if the build succeeds, swap: rename
.nextto.next-prev, and.next-buildto.next - Restart the server
- If the health check fails, rename back
A failed build does not affect live at all, and the swap is just two renames, in theory instantaneous.
On the first run the build went fine, and then:
The server had been stopped before the rename; when the rename failed, it was started again with the old .next. The first run of the atomic deploy made the site go down for an extra 12 seconds.
Who is locking .next
The server was already stopped, so in principle nobody was using .next.
There is a key difference between Windows and Linux: on Linux you can rename a folder even while files inside it are open; on Windows, as long as any file or subfolder inside has a handle open, the folder above cannot be renamed.
And inside .next there is a types subfolder, the route types generated by Next.js. Next.js automatically writes .next/types/**/*.ts into the include of tsconfig.json. That means as long as VS Code has this project open, its TypeScript language service watches .next/types, and VS Code is always open on this desktop, because this is where I work on the site.
I did not want to require "close the editor first" for deploys: that would give the automation a hidden precondition that I would forget one day.
Second version: do not move folders, switch a pointer
Since the rename is the problem, do not rename.
- Prepare two build folders:
.next-aand.next-b - A small file,
data/next-dist-dir.txt, records "which one is live now" - On deploy, build into the one that is not live, and after the build, change the pointer file and restart
next.config.ts reads an environment variable to decide which folder to use:
When the server starts, it reads the pointer file and sets NEXT_DIST_DIR:
If the health check fails, the pointer is written back to the original and the server restarted once more: the old version is still intact in the other folder, so rolling back needs no new build.
One more small trap: tsconfig gets dirty
When Next.js builds, it writes <distDir>/types into the include of tsconfig.json. With distDir becoming .next-a and .next-b, every deploy adds a line to tsconfig.json, and git shows a change.
The fix is for the deploy build to use a copy: each time, copy tsconfig.json to tsconfig.deploy.json (added to .gitignore) and let Next.js modify that one.
Result
From the switch to the health check passing takes about 2 seconds. I also tested the rollback for real: the deploy script has a -TestRollback parameter that, after switching, deliberately treats the health check as failed to see whether it points back to the original folder correctly.
- Time during which the site can error during a deploy: 5 to 40 seconds (build plus restart) → 1 to 2 seconds (just the restart)
- A failed build:
.nextincomplete, broken until someone notices → live completely unaffected - A bad new version: handled by hand → automatically rolled back to the previous version
What I learned
- Most deploy recipes online assume Linux. "Rename is atomic" holds on Linux; on Windows it needs one more condition: nobody has anything inside open.
- When the dev machine is also the production machine, the editor is part of the environment. It locks files and watches folders, and automation has to account for it.
- If you can avoid moving data, avoid it, and just change a pointer. Two folders used in turn make both switching and rolling back a matter of writing one line.