[PR #3372] [MERGED] fix(client): auto-recover from stale-chunk dynamic-import failures #5907

Closed
opened 2026-10-23 04:05:01 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/3372
Author: @kolacheee
Created: 7/8/2026
Status: ✅ Merged
Merged: 7/8/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/3370-preload-error-recovery


📝 Commits (3)

  • 30b7cc2 fix(client): auto-recover from stale-chunk dynamic-import failures (#3370)
  • 3d06b94 Merge branch 'staging' into fix/3370-preload-error-recovery
  • 0d5b4d0 fix(client): fall back after preload recovery failure

📊 Changes

2 files changed (+45 additions, -0 deletions)

View changed files

📝 packages/client/src/lib/browser-runtime.ts (+40 -0)
📝 packages/client/src/main.tsx (+5 -0)

📄 Description

Linked issue

Closes #3370

Why this change

Lazy routes are code-split (CharacterEditor = lazy(() => import(...))) and the PWA precaches the JS chunks (registerType: "autoUpdate", globPatterns: ["**/*.{js,css,…}"]). After an update, a still-running page — or a sticky service-worker precache — can reference a hashed chunk the new build already deleted (clean:stale-client removes old hashes). The lazy import() then 404s and surfaces:

Failed to fetch dynamically imported module: http://127.0.0.1:7860/assets/CharacterEditor-<hash>.js

There's no recovery for this: the app only auto-heals on a health-check version mismatch (recoverFromVersionSkew), which a bare chunk-load failure doesn't trigger (client and server can report the same version yet a sticky SW still serves a stale chunk). So users hit it repeatedly across updates until they manually clear the service worker / site data.

What changed

  • packages/client/src/lib/browser-runtime.ts — add registerPreloadErrorRecovery(), a vite:preloadError window listener that reuses the existing forceRefreshSpa recovery (unregister the service worker, clear caches, reload with a cache-busting query param — the same thing the version-skew path already does). Guarded by a short sessionStorage cooldown (20 s) so a genuinely broken build — or a page that re-triggers the same import right after reload — can't reload-loop: after one attempt within the window, preventDefault is skipped so Vite rethrows and the app's recovery boundary shows its Reload button.
  • packages/client/src/main.tsx — call registerPreloadErrorRecovery() at startup.

Client-only; no server / shared / schema changes.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • Client build (tsc) and ESLint pass: pnpm --filter @marinara-engine/client build, pnpm --filter @marinara-engine/client lint.
  • Simulate the failure without needing a mid-session update — in the running app's DevTools console:
    1. window.dispatchEvent(new Event("vite:preloadError", { cancelable: true })) → the page should clear caches/SW and reload with a ?chunk_reload=… param (check the URL after reload, and Application → Service Workers / Cache Storage are cleared).
    2. Immediately dispatch it again (within 20 s) → it should not reload again (cooldown guard); confirm via sessionStorage.getItem("mari-preload-recovery-at").
  • Real-world check: with a tab open, rebuild the client (pnpm build), then open the Character editor in the stale tab → instead of the "Failed to fetch dynamically imported module" error, the app should reload once and recover.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

Robustness fix; no docs or version impact.

UI evidence (if applicable)

N/A — startup recovery hook; no visual change (it replaces an error screen with a one-time reload).

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling for failed dynamic imports so the app can recover automatically instead of showing a hard error.
    • Added a browser-side refresh recovery path for stale chunks, helping pages load correctly after updates.
    • Reduced repeated reload loops by adding a short cooldown between recovery attempts.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/3372 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 7/8/2026 **Status:** ✅ Merged **Merged:** 7/8/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/3370-preload-error-recovery` --- ### 📝 Commits (3) - [`30b7cc2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/30b7cc23603ce2dca1a071de72d71a0246cd1872) fix(client): auto-recover from stale-chunk dynamic-import failures (#3370) - [`3d06b94`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3d06b94c5026bb7631ca15c5ba4b75c1aff18b30) Merge branch 'staging' into fix/3370-preload-error-recovery - [`0d5b4d0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0d5b4d0288d167f1ad7700ce3b2f4d211ec4a406) fix(client): fall back after preload recovery failure ### 📊 Changes **2 files changed** (+45 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/lib/browser-runtime.ts` (+40 -0) 📝 `packages/client/src/main.tsx` (+5 -0) </details> ### 📄 Description ## Linked issue Closes #3370 ## Why this change Lazy routes are code-split (`CharacterEditor = lazy(() => import(...))`) and the PWA precaches the JS chunks (`registerType: "autoUpdate"`, `globPatterns: ["**/*.{js,css,…}"]`). After an update, a still-running page — or a sticky service-worker precache — can reference a hashed chunk the new build already deleted (`clean:stale-client` removes old hashes). The lazy `import()` then 404s and surfaces: ``` Failed to fetch dynamically imported module: http://127.0.0.1:7860/assets/CharacterEditor-<hash>.js ``` There's no recovery for this: the app only auto-heals on a health-check **version** mismatch (`recoverFromVersionSkew`), which a bare chunk-load failure doesn't trigger (client and server can report the same version yet a sticky SW still serves a stale chunk). So users hit it repeatedly across updates until they manually clear the service worker / site data. ## What changed - `packages/client/src/lib/browser-runtime.ts` — add `registerPreloadErrorRecovery()`, a `vite:preloadError` window listener that reuses the existing `forceRefreshSpa` recovery (unregister the service worker, clear caches, reload with a cache-busting query param — the same thing the version-skew path already does). Guarded by a short **sessionStorage cooldown** (20 s) so a genuinely broken build — or a page that re-triggers the same import right after reload — can't reload-loop: after one attempt within the window, `preventDefault` is skipped so Vite rethrows and the app's recovery boundary shows its Reload button. - `packages/client/src/main.tsx` — call `registerPreloadErrorRecovery()` at startup. Client-only; no server / shared / schema changes. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes - Client build (tsc) and ESLint pass: `pnpm --filter @marinara-engine/client build`, `pnpm --filter @marinara-engine/client lint`. - Simulate the failure without needing a mid-session update — in the running app's DevTools console: 1. `window.dispatchEvent(new Event("vite:preloadError", { cancelable: true }))` → the page should clear caches/SW and reload with a `?chunk_reload=…` param (check the URL after reload, and Application → Service Workers / Cache Storage are cleared). 2. Immediately dispatch it again (within 20 s) → it should **not** reload again (cooldown guard); confirm via `sessionStorage.getItem("mari-preload-recovery-at")`. - Real-world check: with a tab open, rebuild the client (`pnpm build`), then open the Character editor in the stale tab → instead of the "Failed to fetch dynamically imported module" error, the app should reload once and recover. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) Robustness fix; no docs or version impact. ## UI evidence (if applicable) N/A — startup recovery hook; no visual change (it replaces an error screen with a one-time reload). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved handling for failed dynamic imports so the app can recover automatically instead of showing a hard error. * Added a browser-side refresh recovery path for stale chunks, helping pages load correctly after updates. * Reduced repeated reload loops by adding a short cooldown between recovery attempts. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:05:01 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5907
No description provided.