Agent Wranglers

Permissions, flags and failures Recommended

Recover when a new release replaces the old files

When a tab still runs the old release and a code chunk fails to load after a deploy, reload once to get the new release. Keep older files online as keep the previous build's assets sets.

How to do it

A tab opened before a deploy asks for chunk files that the new release may have removed. Catch failed dynamic imports, such as Vite's vite:preloadError event or webpack's ChunkLoadError, save any draft, and reload once. Store the reload time in sessionStorage, and skip a second reload within a minute, so a broken release can't loop. Also check the build version from a small endpoint and offer a reload when it changes.

Example

const RELOAD_FLAG = 'reloaded-for-release';

const RELOAD_GUARD_MS = 60_000;

addEventListener('vite:preloadError', event => {
  const lastReload = Number(sessionStorage.getItem(RELOAD_FLAG) ?? 0);
  if (Date.now() - lastReload < RELOAD_GUARD_MS) return;
  event.preventDefault();
  sessionStorage.setItem(RELOAD_FLAG, String(Date.now()));
  saveDrafts();
  location.reload();
});
import os

BUILD = os.environ["BUILD_ID"]

@app.get("/api/version")
def version():
    return {"build": BUILD}, 200, {"Cache-Control": "no-store"}

How to check

Deploy a release to staging, then click an in-app link in a tab opened before the deploy. The next screen loads, and the tab reloads at most once.

For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers app interface playbook: https://agentwranglers.se/playbook/app-ui/permissions-flags-and-failures/new-release-reload.