Repository navigation
Conversation
A /assets/ chunk from a previous build no longer exists after a deploy; serving the SPA shell at that URL makes the browser fail dynamic imports on a text/html module. Exclude /assets/ from the SPA fallback so the miss surfaces as a 404 the client can react to. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
After a deploy, an open tab still references the previous build's content-hashed chunks and the first lazy navigation dies with "Failed to fetch dynamically imported module". Handle Vite's vite:preloadError by reloading onto the current build, guarded to at most one reload per minute so a persistently missing chunk cannot reload-loop. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Warning Review limit reached
Next review available in: 47 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughMissing ChangesAsset failure recovery
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Browser
participant frontendHandler.ServeHTTP
participant installPreloadErrorReload
participant sessionStorage
participant window.location.reload
Browser->>frontendHandler.ServeHTTP: Request missing /assets/ chunk
frontendHandler.ServeHTTP-->>Browser: Return HTTP 404
Browser->>installPreloadErrorReload: Dispatch vite:preloadError
installPreloadErrorReload->>sessionStorage: Read reload guard timestamp
installPreloadErrorReload->>sessionStorage: Store eligible reload timestamp
installPreloadErrorReload->>window.location.reload: Reload page
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
web/src/lib/reloadOnPreloadError.test.ts (1)
18-56: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPrevent event listener leaks across tests.
Since
windowis shared across tests in Vitest, callinginstallPreloadErrorReloadmultiple times accumulates event listeners. While the mocked clocks and isolated storage happen to prevent the tests from interfering with each other right now, this state leakage makes the test suite brittle to future changes.Consider updating
installPreloadErrorReloadto return a cleanup function, and invoking it in anafterEachblock.♻️ Proposed refactor for cleanup
Update the source in
web/src/lib/reloadOnPreloadError.ts:-export function installPreloadErrorReload(deps?: Partial<PreloadErrorReloadDeps>): void { +export function installPreloadErrorReload(deps?: Partial<PreloadErrorReloadDeps>): () => void { const reload = deps?.reload ?? (() => window.location.reload()); const now = deps?.now ?? Date.now; const getItem = deps?.getItem ?? ((k: string) => sessionStorage.getItem(k)); const setItem = deps?.setItem ?? ((k: string, v: string) => sessionStorage.setItem(k, v)); - window.addEventListener("vite:preloadError", (event) => { + const handler = (event: Event) => { const lastReloadAt = Number(getItem(GUARD_KEY) ?? 0); if (now() - lastReloadAt < GUARD_WINDOW_MS) { // We already reloaded moments ago and the chunk is still missing; // let the failure surface instead of reload-looping. return; } event.preventDefault(); setItem(GUARD_KEY, String(now())); reload(); - }); + }; + + window.addEventListener("vite:preloadError", handler); + return () => window.removeEventListener("vite:preloadError", handler); }Then, in the test file, capture and call the cleanups:
+import { afterEach } from "vitest"; describe("installPreloadErrorReload", () => { + let cleanups: Array<() => void> = []; + + afterEach(() => { + cleanups.forEach((cleanup) => cleanup()); + cleanups = []; + }); it("reloads when a dynamically imported chunk fails to load", () => { const { reload, deps } = harness(() => 1_000_000); - installPreloadErrorReload(deps); + cleanups.push(installPreloadErrorReload(deps)); window.dispatchEvent(new Event("vite:preloadError")); expect(reload).toHaveBeenCalledTimes(1); }); it("does not reload again within the loop-guard window", () => { let clock = 1_000_000; const { reload, deps } = harness(() => clock); - installPreloadErrorReload(deps); + cleanups.push(installPreloadErrorReload(deps)); window.dispatchEvent(new Event("vite:preloadError")); expect(reload).toHaveBeenCalledTimes(1); // Post-reload page still hits the error seconds later: give up instead // of reload-looping. clock += 5_000; - installPreloadErrorReload(deps); + cleanups.push(installPreloadErrorReload(deps)); window.dispatchEvent(new Event("vite:preloadError")); expect(reload).toHaveBeenCalledTimes(1); }); it("reloads again once the guard window has passed (a later deploy)", () => { let clock = 1_000_000; const { reload, deps } = harness(() => clock); - installPreloadErrorReload(deps); + cleanups.push(installPreloadErrorReload(deps)); window.dispatchEvent(new Event("vite:preloadError")); expect(reload).toHaveBeenCalledTimes(1); clock += 10 * 60_000; - installPreloadErrorReload(deps); + cleanups.push(installPreloadErrorReload(deps)); window.dispatchEvent(new Event("vite:preloadError")); expect(reload).toHaveBeenCalledTimes(2); }); });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/src/lib/reloadOnPreloadError.test.ts` around lines 18 - 56, Update installPreloadErrorReload in reloadOnPreloadError.ts to return a cleanup function that removes its registered window event listener. In the installPreloadErrorReload tests, capture each returned cleanup and invoke it from afterEach so listeners do not accumulate across tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@web/src/lib/reloadOnPreloadError.test.ts`:
- Around line 18-56: Update installPreloadErrorReload in reloadOnPreloadError.ts
to return a cleanup function that removes its registered window event listener.
In the installPreloadErrorReload tests, capture each returned cleanup and invoke
it from afterEach so listeners do not accumulate across tests.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: cfbce971-a6c2-4eba-9c6c-d82a307d7600
📒 Files selected for processing (5)
internal/server/frontend.gointernal/server/frontend_test.goweb/src/lib/reloadOnPreloadError.test.tsweb/src/lib/reloadOnPreloadError.tsweb/src/main.tsx
Review feedback: window is shared across vitest tests, so repeated installs accumulated listeners. The installer now returns a remover and the tests detach in afterEach. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Summary
Every deploy changes the content-hashed
/assets/chunk names. An open tab from before the deploy fails its next lazy navigation with "Failed to fetch dynamically imported module" — made worse by the SPA fallback answering the missing chunk URL with 200 + index.html, so the browser rejects an HTML document as a JS module instead of seeing a clean miss.Two fixes, observed and verified on a production install today:
/assets/paths that don't resolve to a bundled file now return 404 instead of falling through to the SPA shell. Fingerprinted assets are immutable; an unknown one is a stale reference, not an app route.vite:preloadErrorby reloading once onto the current build, guarded to at most one reload per minute so a genuinely broken deploy cannot reload-loop.Tests
go test ./internal/server(new:TestFrontendHandlerReturns404ForMissingAssets)pnpm exec vitest run src/lib/reloadOnPreloadError.test.ts(3 cases: reload, loop guard, guard expiry)AI-use disclosure
Implemented with Claude Code (TDD).
🤖 Generated with Claude Code
Summary by CodeRabbit