[GH-ISSUE #3370] [Issue]: Stale-chunk dynamic-import failures ('Failed to fetch dynamically imported module') aren't auto-recovered #1822

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

Originally created by @kolacheee on GitHub (Jul 8, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3370

Originally assigned to: @SpicyMarinara, @kolacheee on GitHub.

Summary

When a lazy-loaded route/chunk fails to load — e.g. after an update the running page references a hashed chunk (CharacterEditor-<hash>.js) that the new build deleted, or a stale service-worker precache serves an old one — the app shows "Failed to fetch dynamically imported module: …/assets/CharacterEditor-.js" and stays broken. There is no automatic recovery for this specific failure, so users hit it repeatedly across updates until they manually clear the service worker / site data.

Expected behavior

A failed dynamic import (stale/missing chunk) should trigger a one-time automatic recovery — clear the service worker + caches and reload so the browser fetches the current build — instead of surfacing the error. (The app already does exactly this for version skew via recoverFromVersionSkew → forceRefreshSpa.)

Actual behavior

The app registers no vite:preloadError handler, so a failed import() is not recovered — it surfaces to the user as "Failed to fetch dynamically imported module." The existing recoverFromVersionSkew only fires on a health-check client/server version mismatch, which does not cover a bare chunk-load failure: the client and server can report the same version yet the page still references a stale/missing chunk from a sticky SW precache.

Steps to reproduce

  1. Run a build and open the app (the service worker precaches the JS chunks).
  2. Update + rebuild — chunk hashes change and the old chunk files are removed by clean:stale-client.
  3. With a tab still open (or a sticky service worker), open something lazy-loaded (e.g. the Character editor) → "Failed to fetch dynamically imported module: …/assets/CharacterEditor-<hash>.js". It does not self-heal; you must manually clear the SW / site data.

Environment

  • Marinara Engine version: 2.1.1 (staging)
  • Install type: source (start.bat)
  • OS + version: Windows 11 (not OS-specific)
  • Browser or app shell: web app (PWA)

Logs, screenshots, or video

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

Additional context

Root cause: the PWA precaches JS chunks (packages/client/vite.config.ts — globPatterns: ["**/*.{js,css,png,svg,ico,woff2}"], registerType: "autoUpdate") and lazy routes are code-split (CharacterEditor = lazy(() => import(...)) in AppShell). After an update, a running page or a stale SW can reference a chunk hash the new build deleted → the import() 404s. Vite fires a vite:preloadError window event for exactly this, but the app has no listener (grep -r "vite:preloadError" packages/client/src → none). The recovery primitive already exists: forceRefreshSpa (packages/client/src/lib/browser-runtime.ts) unregisters the service worker, clears all caches, and reloads with a cache-busting query param.

Suggested fix: register a vite:preloadError listener that calls forceRefreshSpa, guarded by a short cooldown (a sessionStorage timestamp) so a genuinely broken build — or a page that re-triggers the same import immediately after reload — can't reload-loop: after one attempt within the window, let Vite rethrow so the app's error boundary Reload button shows.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jul 8, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3370 Originally assigned to: @SpicyMarinara, @kolacheee on GitHub. ## Summary When a lazy-loaded route/chunk fails to load — e.g. after an update the running page references a hashed chunk (`CharacterEditor-<hash>.js`) that the new build deleted, or a stale service-worker precache serves an old one — the app shows *"Failed to fetch dynamically imported module: …/assets/CharacterEditor-<hash>.js"* and stays broken. There is **no automatic recovery** for this specific failure, so users hit it repeatedly across updates until they manually clear the service worker / site data. ## Expected behavior A failed dynamic import (stale/missing chunk) should trigger a **one-time automatic recovery** — clear the service worker + caches and reload so the browser fetches the current build — instead of surfacing the error. (The app already does exactly this for version skew via `recoverFromVersionSkew` → `forceRefreshSpa`.) ## Actual behavior The app registers **no `vite:preloadError` handler**, so a failed `import()` is not recovered — it surfaces to the user as *"Failed to fetch dynamically imported module."* The existing `recoverFromVersionSkew` only fires on a health-check client/server **version** mismatch, which does not cover a bare chunk-load failure: the client and server can report the same version yet the page still references a stale/missing chunk from a sticky SW precache. ## Steps to reproduce 1. Run a build and open the app (the service worker precaches the JS chunks). 2. Update + rebuild — chunk hashes change and the old chunk files are removed by `clean:stale-client`. 3. With a tab still open (or a sticky service worker), open something lazy-loaded (e.g. the **Character editor**) → *"Failed to fetch dynamically imported module: …/assets/CharacterEditor-&lt;hash&gt;.js"*. It does not self-heal; you must manually clear the SW / site data. ## Environment - Marinara Engine version: 2.1.1 (staging) - Install type: source (`start.bat`) - OS + version: Windows 11 (not OS-specific) - Browser or app shell: web app (PWA) ## Logs, screenshots, or video ``` Failed to fetch dynamically imported module: http://127.0.0.1:7860/assets/CharacterEditor-DeDItuoD.js ``` ## Additional context Root cause: the PWA precaches JS chunks (`packages/client/vite.config.ts` — `globPatterns: ["**/*.{js,css,png,svg,ico,woff2}"]`, `registerType: "autoUpdate"`) and lazy routes are code-split (`CharacterEditor = lazy(() => import(...))` in `AppShell`). After an update, a running page or a stale SW can reference a chunk hash the new build deleted → the `import()` 404s. Vite fires a `vite:preloadError` window event for exactly this, but the app has no listener (`grep -r "vite:preloadError" packages/client/src` → none). The recovery primitive already exists: `forceRefreshSpa` (`packages/client/src/lib/browser-runtime.ts`) unregisters the service worker, clears all caches, and reloads with a cache-busting query param. Suggested fix: register a `vite:preloadError` listener that calls `forceRefreshSpa`, guarded by a short cooldown (a sessionStorage timestamp) so a genuinely broken build — or a page that re-triggers the same import immediately after reload — can't reload-loop: after one attempt within the window, let Vite rethrow so the app's error boundary Reload button shows. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:43:01 +07:00
  • closed this issue
  • added the
    in-pr
    bug
    labels
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#1822
No description provided.