Expose HMR retry limit in Vite config vs. keeping it hard‑coded
0 reputation · 14 Feb 2024, 00:22 UTC
0 reputation · 14 Feb 2024, 00:22 UTC
Vite’s Hot Module Replacement client currently retries a failed update up to a hard‑coded maximum of three attempts before falling back to a full page reload. The constant lives in the client overlay (src/node/server/hmr.ts) and can only be altered by forking Vite or replacing the HMR runtime with a custom plugin, which limits flexibility for teams that want different back‑off strategies or lower latency on flaky networks. An open discussion in the Vite repository weighs adding a server.hmr.retry option against keeping the value internal to avoid configuration overhead and potential misuse.
What trade‑offs exist between exposing this setting and preserving the current default behavior? How would a configurable limit affect duplicate‑write safety and developer experience? Should the API accept a simple number, a back‑off object, or remain unchanged?
Vite’s Hot Module Replacement (HMR) client currently retries a failed update up to three times before falling back to a full page reload. Exposing this limit as a configurable option would give teams control over retry aggressiveness, but it also introduces risks of duplicate writes, inconsistent behavior across environments, and potential misuse. A simple numeric option strikes a balance between flexibility and safety, while keeping the default of three attempts for most users.
src/node/server/hmr.ts and is set to maxRetries = 3 with a 1‑second pause between attempts.Teams that experience flaky networks often want faster recovery, while others value the safety of a full reload when an update fails repeatedly. A numeric option allows quick tuning without exposing complex back‑off logic that could be misconfigured. If more sophisticated control is needed, a dedicated plugin could still provide custom retry strategies.
Open vite.config.ts (or .js) and add a server.hmr.retry field:
export default defineConfig({
server: {
hmr: {
retry: 5 // retry up to 5 times instead of the default 3
}
}
});
Restart the dev server. The console will log the configured retry count on each HMR connection.
To verify duplicate writes, instrument a module with a console.log in its exported function and observe how many times it runs when a network timeout occurs.
Use browser dev‑tools throttling or a network simulation tool (e.g., tc) to emulate flaky conditions and compare the experience with the default and custom limits.
Expose a simple numeric option (server.hmr.retry) with a clear default of 3. Document the risks of duplicate writes and advise keeping the value low for modules that perform side effects. If a team requires more granular control, they can implement a custom HMR plugin instead of relying on the built‑in setting.
Do you have modules that perform non‑idempotent side effects (e.g., network requests or state mutations) that could be duplicated on retry? This information could influence whether the default should remain stricter or a higher limit is acceptable.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.