build-release-detection
v1.0.0
Published
Detect new deployments and notify users with a customizable modal or auto-reload. Works with React, Next.js, Vite, CRA and any framework.
Downloads
64
Maintainers
Readme
build-release-detection
Detect new deployments and notify users — or silently reload — without any server-side infrastructure.
After a deploy, browsers often serve stale JavaScript bundles from the old build. This causes blank screens, infinite loading, and confused users. This package polls a lightweight version.json file on your CDN/server and shows a non-intrusive modal when a new build is detected.
Features
- Polls
/version.json— tiny file, no backend needed - Update modal — built-in zero-dependency UI, fully customisable
- Silent mode — auto-reload without any modal, great for hot-fixes
- Snooze — "Remind me later" re-shows after a configurable interval
- Hook-first —
useBuildDetectionworks with any UI (MUI, shadcn, etc.) - TypeScript & JavaScript — ships full types, works in
.jstoo - Accessible — focus trap,
role="dialog",aria-modal, Escape key
Install
npm install build-release-detection
# or
yarn add build-release-detectionQuick Start (2 steps)
Step 1 — Generate version.json at build time
Add the generator to your build script in package.json:
{
"scripts": {
"build": "node node_modules/build-release-detection/scripts/generate-version.js && next build"
}
}For Vite / CRA:
"build": "node node_modules/build-release-detection/scripts/generate-version.js && vite build"Also add to .gitignore:
public/version.jsonStep 2 — Mount the provider
Next.js (Pages Router) — pages/_app.tsx:
import { BuildDetectionProvider } from "build-release-detection";
export default function App({ Component, pageProps }) {
return (
<BuildDetectionProvider>
<Component {...pageProps} />
</BuildDetectionProvider>
);
}React (Vite / CRA) — src/main.tsx:
import { BuildDetectionProvider } from "build-release-detection";
ReactDOM.createRoot(document.getElementById("root")!).render(
<BuildDetectionProvider>
<App />
</BuildDetectionProvider>
);That's it. On your next deploy the modal appears automatically.
Silent Mode (auto-reload, no modal)
For non-breaking deploys you can reload the page automatically with no user prompt.
At build time via env var:
SILENT_UPDATE=true npm run buildOr hardcode it in code:
<BuildDetectionProvider silent={true}>Configuration
<BuildDetectionProvider> props
All props are optional.
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| versionFileUrl | string | "/version.json" | URL of the version file to poll |
| pollInterval | number | 60000 | Poll frequency in ms (1 minute) |
| snoozeInterval | number | 900000 | "Remind later" duration in ms (15 min) |
| silent | boolean | false | Auto-reload without showing the modal |
| onNewBuild | (payload, prevId) => void | — | Callback fired when a new build is detected |
| disabled | boolean | false | Completely disable polling |
| title | string | "New version available 🚀" | Modal heading |
| body | string | "A new version…" | Modal description |
| reloadLabel | string | "Reload now" | Primary button label |
| snoozeLabel | string | "Remind me in 15 min" | Snooze button label |
| dismissLabel | string \| null | "×" | Close button label; null hides it |
| overlayClassName | string | — | Class added to the backdrop element |
| cardClassName | string | — | Class added to the modal card |
| overlayStyle | CSSProperties | — | Inline styles for the backdrop |
| cardStyle | CSSProperties | — | Inline styles for the card |
Using Your Own UI
Skip the built-in modal and wire up the hook directly:
import { useBuildDetection } from "build-release-detection";
function MyUpdateDialog() {
const {
isUpdateAvailable,
latestVersion,
currentBuildId,
reloadNow,
remindLater,
dismiss,
} = useBuildDetection({
pollInterval: 60_000,
snoozeInterval: 15 * 60_000,
onNewBuild: (payload, prevId) => {
console.log(`New build: ${payload.buildId} (was: ${prevId})`);
},
});
if (!isUpdateAvailable) return null;
// Use any UI library — MUI Dialog, shadcn Alert, etc.
return (
<div className="update-toast">
<p>New version available!</p>
<button onClick={reloadNow}>Reload</button>
<button onClick={remindLater}>Later</button>
</div>
);
}useBuildDetection return values
| Property | Type | Description |
|----------|------|-------------|
| isUpdateAvailable | boolean | true when a new build is detected and modal should show |
| latestVersion | VersionPayload \| null | The latest payload from the server |
| currentBuildId | string \| null | The buildId loaded when the user opened the app |
| reloadNow | () => void | Hard-reload via window.location.href |
| remindLater | () => void | Close modal and re-show after snoozeInterval |
| dismiss | () => void | Close modal (won't reappear until the next new build) |
generate-version.js Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| SILENT_UPDATE | "false" | Set to "true" to embed isSilent: true in the JSON |
| VERSION_FILE_PATH | public/version.json | Output path for the generated file |
| BUILD_ID | auto-generated | Override the buildId (e.g. your CI commit SHA) |
Using CI commit SHA as buildId:
BUILD_ID=$GITHUB_SHA npm run buildversion.json Shape
{
"buildId": "m3k1a2b-3f9c12d4",
"isSilent": false,
"builtAt": "2026-04-14T10:30:00.000Z"
}How It Works
User opens app
│
▼
Fetch version.json → store buildId as baseline (no modal)
│
▼ (every pollInterval)
Fetch version.json
│
├─ buildId unchanged ──→ nothing
│
└─ buildId changed!
│
├─ isSilent: true ──→ window.location.reload()
│
└─ isSilent: false
│
├─ snooze active ──→ wait
│
└─ show modal
├─ Reload Now ──→ window.location.reload()
└─ Remind Later ──→ snooze for snoozeIntervalPolling pauses automatically when the browser tab is hidden and resumes when the user switches back.
Testing Locally
- Build your app:
npm run build - Serve:
npx serve out(Next.js static) ornpx vite preview - Lower the poll interval to 10s for testing:
<BuildDetectionProvider pollInterval={10_000}> - Simulate a new deploy:
node -e " const fs = require('fs'); fs.writeFileSync( 'out/version.json', JSON.stringify({ buildId: Date.now().toString(), isSilent: false }) ) " - Wait 10 seconds — the modal appears.
- Revert
pollIntervalbefore production deploy.
Deploy Commands
# Standard deploy — shows modal to users
npm run build
# Silent deploy — auto-reloads without a modal
SILENT_UPDATE=true npm run build
# Deploy with CI commit SHA as buildId
BUILD_ID=$GITHUB_SHA npm run build