Deploys and version skew
A tab runs the client of one build. After a deploy its next request reaches a server, a CDN or a service worker that holds another build: a lazy chunk it asks for is gone, a server function it calls has another id, a server island it asks for no longer exists. This is version skew. Hamur compares build ids: 16 hex characters of the client build, which change only when the client changes. Two deploys of the same client are never skew, and a deploy that changes only the server is not either.
Hamur's answer has four parts, from the quietest to the loudest:
| Part | What it does |
|---|---|
the version file and useAppUpdate | tell a tab that a newer build exists, so the next navigation loads it |
app.chunkErrors | reload once when a chunk of the old build fails to load |
| 409 answers | tell a tab that calls what the new server no longer has to reload |
skewProtection.keep and the Vercel deployment | keep the old build's files, or the old deploy, so a tab does not fail in the first place |
Every reload Hamur starts goes through reloadApp under one guard, so no path can loop.
import { defineConfig } from "hamur"
export default defineConfig({
framework: "react",
app: {
update: { pollInterval: 600_000, reloadOnNavigate: true },
chunkErrors: "reload",
restoreState: false,
},
skewProtection: { keep: 2 },
})
The version file
Every preset writes <baseURL>_version.json into its public directory, the static presets included, so the same URL answers on every host:
{ "buildId": "3f9a1c0d5e7b2a41", "time": 1796000000000 }
buildId is the build id of the client, the one in the page's payload. time is the build time in milliseconds: SOURCE_DATE_EPOCH times 1000 when that variable is set, else the time at which that build id was first built. A rebuild of the same client writes the same bytes, so the file is reproducible wherever <cacheDir> survives. Hamur serves it with cache-control: no-cache, because it keeps one name and changes on every deploy; a routeRules entry of your own for /_version.json wins. hamur dev writes none.
A public/_version.json fails the build: public/_version.json is the version file Hamur writes; rename it.
The generated service worker never answers the version file, so a worker cannot hide a new deploy from the client.
x-hamur-build
In a production client, Hamur's own same-origin requests that run code by id send x-hamur-build: <build id>: server functions (<baseURL>_hamur/fn/<id>), server islands (_hamur/islands/<name>) and server components (_hamur/components/<name>). Every answer of those three endpoints carries x-hamur-build: <the server's build id>, errors included. Nothing else reads the header, and hamur dev sends none.
The app update: useAppUpdate
An app update is a build with another build id than the one the tab runs. In a production client only, Hamur checks for one:
app.update.pollIntervalmilliseconds after start and then on that interval, while the tab is visible (default one hour);- when the tab becomes visible again and the last check is at least 60 seconds old;
- when an answer of
_hamur/fn/,_hamur/islands/or_hamur/components/carries anx-hamur-buildother than the tab's (once for each value), for example from a CDN copy of an older answer; - after a chunk error that
app.chunkErrorssays to look into.
A check is fetch("<baseURL>_version.json", { cache: "no-cache" }). When buildId differs from the tab's, the update is found: useAppUpdate().available turns true and stays true for that document, the hook app:update runs once, and with @hamur/pwa the registration is asked to check for a new worker. A failed fetch, a non-200 and a body without a buildId are no update, and nothing is logged.
| Key | Default | |
|---|---|---|
app.update.pollInterval | 3600000 | milliseconds between two checks while the tab is visible, or false to turn off the interval and the visibility check. The x-hamur-build hints still start a check |
app.update.reloadOnNavigate | true | once an update is known, the next navigation (a Link, navigate(), back or forward) is a full load of its target. false keeps it a client navigation |
With reloadOnNavigate the new build arrives on the user's next click, with no banner. When the reload guard refuses the load, the navigation runs on the client as usual. A hash-only change on the page on screen stays a client navigation.
useAppUpdate() is in every adapter. It returns { available, buildId, time, check }: buildId and time are the new build's and undefined until one is found, and check() runs a check now and resolves available; it never rejects. In the browser under hamur dev, and on the server, available is false and check() resolves false. The first read after hydration is false in every adapter, so the page matches the server's HTML.
import { useAppUpdate } from "@hamur/adapter-react"
export default function NewVersion() {
const { available, buildId, check } = useAppUpdate()
if (available) return <p role="status">Build {buildId} is live. The next link you click loads it.</p>
return <button onClick={() => void check()}>Check for updates</button>
}
The same value in each library: React and Preact give values, Vue readonly refs, Solid accessors, Svelte getters, and html getters plus subscribe(listener). The reference is useAppUpdate.
The client hook app:update receives { buildId, time }. Register it in an app plugin, together with app:chunkError:
import { definePlugin } from "hamur/app"
export default definePlugin((app) => {
app.hooks.hook("app:update", ({ buildId, time }) => {
console.log(`build ${buildId} (${new Date(time).toISOString()}) is live; the next navigation loads it`)
})
app.hooks.hook("app:chunkError", ({ stage, url, reload }) => {
if (!reload) console.warn(`a ${stage} chunk failed on ${url}; reload when it suits the user`)
})
})
Chunk errors: app.chunkErrors
A page, layout or app-root chunk that fails to load after a deploy is tried twice. app.chunkErrors says what follows:
| Value | After the second failure of a page, layout or app-root chunk | After a failed import() of your own code |
|---|---|---|
"reload" (default) | reloads the URL, once per 10 seconds | checks for an app update, so the next navigation is a full load |
"reload-immediate" | reloads the URL, once per 10 seconds | reloads the current URL, once per 10 seconds |
"manual" | reloads nothing; app:chunkError runs with reload: false, and the 503 error page shows, or the page stays as it is | nothing |
A failed import() of your own code (a lazy component) is thrown to its caller in every mode, and is reported to app:chunkError with stage: "lazy". The hook receives { error, url, stage, reload }: stage is "startup", "navigation" or "lazy", and reload says whether Hamur now reloads url. hamur dev reports "startup" and "navigation" only. Any other value fails at load: app.chunkErrors must be "reload", "reload-immediate" or "manual" (got "auto").
Reload, guarded: reloadApp
reloadApp(options?) loads a URL as a document and returns whether it did. It is what Hamur calls for a chunk error, a full load after an app update, a 409 and usePwa().update(), and you can call it for a reload of your own.
import { reloadApp } from "hamur/app"
export function reloadWithState(): boolean {
return reloadApp({ force: true, persistState: true })
}
| Option | Default | |
|---|---|---|
path | the current URL | an app path (app.baseURL is added) or an absolute same-origin URL |
ttl | 10000 | milliseconds during which a second reload of the same URL is refused |
force | false | skip the guard, for a reload the user asked for |
persistState | app.restoreState | carry the app state over the reload |
The guard. Before it loads, reloadApp writes the sessionStorage item hamur:reload, { url, at }, with the target's URL without its hash. A call for a URL the item names within ttl does nothing and returns false. A stamp that cannot be written (private mode, a full quota) also returns false. All of Hamur's reloads share the item, so a chunk error and an update cannot take turns on one URL. On the server reloadApp throws TypeError: reloadApp() runs in the browser only.
State. With persistState, it also writes hamur:state, { url, expires, state }, with the app state serialized as the page payload is, so Date, Map and the types of app/transport.ts survive. After the next app:mounted the client reads the item, removes it, and, when it names the current URL and has not expired, sets each of its keys in the app state. That includes everything useState holds, in plain sessionStorage, which is why app.restoreState is false by default. A value that cannot be serialized is logged and the reload goes on without the item.
409 answers from the server
A server answers a request that sent x-hamur-build with another id than its own with 409 only where it cannot answer what that client asked:
<baseURL>_hamur/fn/<id>with an id the server's table does not have;<baseURL>_hamur/islands/<name>or_hamur/components/<name>with a name the server does not have, or with sealed props that do not open (props are sealed for one build).
The answer has content-type: application/json, cache-control: no-store, x-hamur-build: <the server's id> and the body {"reload":true}; on the function endpoint also x-hamur-fn: reload. The same request with the same build id, or with no header, keeps its 404 or 400. A function id the server still has runs as always, whatever the header says, and its input is validated as usual. hamur dev never answers a 409.
The client reacts by calling reloadApp() for the current URL and leaves the call pending while the page unloads, so several islands reload once and none shows an error. When the guard refuses the reload, a server function call rejects with a HamurError of status 409 and the message The app was updated; reload the page, and an island or a server component shows its error as for any failed answer. See Handlers for the function stub.
Kept builds: skewProtection.keep
skewProtection.keep is an integer from 0 to 10, 0 by default. Above 0, each build saves the files below .output/public/<buildAssetsDir> in <cacheDir>/skew/<build id>/ and copies the files of the last keep earlier builds into its own output. A tab opened before the deploy then still loads its lazy chunks from the new server. A name the new build already has is left as it is, and saved builds older than keep are removed.
- It needs
app.buildAssetsDirbelow the root, such as/_hamur/, because only there is every file hashed:skewProtection.keep needs an app.buildAssetsDir below the root, such as "/_hamur/" (got "/"). Akeepthat is not an integer from 0 to 10 fails at load:skewProtection.keep must be an integer from 0 to 10 (got 1.5). - The cache must survive between builds.
<cacheDir>isnode_modules/.cache/hamurby default; on CI, cache it as you cache the build cache. With nothing saved the build logsskewProtection.keep: no earlier build in node_modules/.cache/hamur/skewand keeps nothing. - Kept files are in no service worker list and in no import map. Only an old tab asks for them. They are served as every file there is, with
immutable, and thebunandnodepresets serve them with ETags and compressed variants. keep: 0writes nothing to<cacheDir>/skew/, andhamur devkeeps nothing.
Kept files do not help a server function or an island that the new server no longer has; that is what the 409 is for.
Vercel skew protection
Vercel keeps earlier deploys and answers a request from the deploy it names, by ?dpl=<deployment id> on a URL or an x-deployment-id header. When the build runs with VERCEL_SKEW_PROTECTION_ENABLED=1 and VERCEL_DEPLOYMENT_ID set, which Vercel sets once skew protection is on for the project, the vercel and vercel-static presets pin a page to its deploy:
| What | Carries |
|---|---|
the import map's URLs, the entry script, the modulepreload and stylesheet links | ?dpl=<id> |
| Hamur's own requests: functions, islands, server components, payload files, route manifest and fixed-params files | x-deployment-id: <id> |
the version file, document loads (Link full loads, reloadApp) and sw.js | nothing: they must reach the newest deploy |
There is no app.deploymentId key and no HAMUR_DEPLOYMENT_ID variable. Hamur reads the two Vercel variables at build, writes the id as payload.deploymentId and does not hash it into the build id, so a redeploy of one client keeps its build id. A URL inside a CSS or JavaScript file and the public files are not pinned; skewProtection.keep keeps those that are renamed. Without the two variables the output is byte for byte what it is without this feature, and hamur dev never pins. Hamur's own 409 compares build ids and never reads the deployment id: with skew protection on, the old deploy answers an old tab, so the 409 is reached only once Vercel no longer keeps that deploy. See vercel.
Other platforms have no pinning in Hamur; they use the parts above.
Per preset
| Preset | Version file and sw.js | Old tabs after a deploy |
|---|---|---|
bun, node and the node hosts | public files with the no-cache rules and an ETag | 409 and chunk-error reloads; skewProtection.keep keeps old chunks |
static, cloudflare-static, vercel-static and the static hosts | files; the rules reach _headers or config.json where the host reads one | payload files carry the build id and the client reloads on a mismatch; keep keeps old chunks |
cloudflare, cloudflare-pages, netlify | files; the rules in _headers | as bun |
vercel | files; the rules in config.json | with skew protection the old deploy answers; otherwise as bun |
Errors
| When | Message |
|---|---|
| build | public/_version.json is the version file Hamur writes; rename it |
| load | app.update.pollInterval must be a positive integer of milliseconds or false (got 0), app.update.reloadOnNavigate must be a boolean (got "yes") |
| load | app.chunkErrors must be "reload", "reload-immediate" or "manual" (got "auto") |
| load | skewProtection.keep must be an integer from 0 to 10 (got 1.5), skewProtection.keep needs an app.buildAssetsDir below the root, such as "/_hamur/" (got "/") |
| run time | HamurError 409 The app was updated; reload the page, TypeError: reloadApp() runs in the browser only |