The commit is the version
I often leave eLNotes open for days. My notes live in the browser, so there is no reason to close the tab. Meanwhile I keep pushing code, and Netlify keeps publishing it.
By Wednesday, that tab is still running Monday’s JavaScript. And because eLNotes works offline, a service worker sits between the tab and the network holding a cached copy of the app. Even a browser refresh can hand back Monday’s build.
So an open tab needs to answer one question: am I running what is currently deployed?
In eLNotes, the version is the git commit. The v.4f2a9c1 in the corner of the app is the short hash of the commit Netlify built.
Why a commit hash
In most projects, a version like 1.4.0 comes from a release step. You decide a batch of changes is ready, bump the number, and ship.
Here, pushing is the release step. I let a few commits stack up locally, and when the work feels ready to go out, I push HEAD. Netlify builds the top commit of that push. Not every commit becomes a deployment, only the one at the tip of a push.
Since every push to the production branch triggers a deploy, each deploy already has a unique name: the commit hash. Generating a separate build number in CI would just be inventing an alias for it. The hash is also more useful when something breaks. If you tell me you are on v.4f2a9c1, I can check out that exact commit and see what you are running.
This works because eLNotes is small and I am the only person shipping it. On a larger team, where multiple commits get bundled into a release and promoted across environments, a commit hash is the wrong abstraction and I would use release tags like v1.2.3 instead.
Different, not newer
I often get asked how the app knows which version is newer. Commit hashes have no order: 4f2a9c1 is not bigger or smaller than a03b7e2.
It turns out the tab never needs to ask whether it is behind. It only needs to know whether it matches production. If the hashes are the same, do nothing. If they differ, production has moved.
Checking for inequality also handles rollbacks. If I revert a bad deploy, production goes back to an older commit. A “newer than” check would see an older version and leave the tab stuck on the broken build. Comparing hashes just sees that production changed and pulls the tab back in line.
Stamping the build
For the comparison to work, the hash has to live in two places: baked into the JavaScript the tab is running, and published in a file on the server that the tab can fetch later.
Both happen in vite.config.ts. Netlify builds inside a checkout of the deployed commit, so the build reads the hash straight from git:
const getCommitHash = () => {
try {
return execSync("git rev-parse --short HEAD").toString().trim();
} catch (error) {
console.warn("Could not get git commit hash:", error);
return "unknown";
}
};
const COMMIT_HASH = getCommitHash();
Vite’s define config replaces __COMMIT_HASH__ across the codebase at bundle time:
define: {
__COMMIT_HASH__: JSON.stringify(COMMIT_HASH),
},
That freezes the hash into the bundle. The sidebar, Settings, and footer all render v.{__COMMIT_HASH__}, and an open tab keeps that value for as long as it stays open.
Next to the bundle, a small Vite plugin writes the same hash to version.json:
const emitVersionManifest = (): Plugin => ({
name: "emit-version-manifest",
apply: "build",
generateBundle() {
const payload = {
version: COMMIT_HASH,
commitHash: COMMIT_HASH,
builtAt: new Date().toISOString(),
};
this.emitFile({
type: "asset",
fileName: "version.json",
source: JSON.stringify(payload, null, 2),
});
},
});
builtAt is just there for me when inspecting a deploy; nothing in the app reads it.
When Netlify publishes dist, the bundle and version.json start out with the same hash. They only drift apart when a tab stays open across a deploy.
Asking the server
To check for an update, the tab fetches version.json and compares it with its own baked-in hash:
const response = await fetch(`/version.json?t=${Date.now()}`, {
cache: "no-store",
});
const { version } = await response.json();
if (version !== __COMMIT_HASH__) {
// production has moved
}
This request has to bypass every cache. If version.json came back from a cache, the tab would compare its hash against itself and think it was up to date forever.
The timestamp query and cache: "no-store" get past the browser cache. To get past the service worker, version.json is left out of the precache manifest and excluded from the worker’s navigation fallback. The request always goes straight to Netlify.
Handing off to the service worker
Knowing a new version exists is only half the job. Because the service worker serves the app from its own cache, reloading the page too early would just load the old build again.
Once a minute, and whenever you return to the tab, the browser checks for an updated sw.js. After a deploy, it downloads the new worker in the background and holds it in a waiting state. Once that worker is waiting, the app fetches version.json to confirm the hash actually changed before showing “Update available”.
Manual checks in Settings run the same handshake in reverse. The app checks version.json first. If the hash changed, it triggers a service worker update and waits for the new worker to finish installing before offering a reload button. On a slow connection, you see “download pending” rather than a reload button that wouldn’t actually update the app.
When you click reload, the waiting worker takes over and the tab comes back on the new commit.