Skip to content

Feature: lazy load reload - #297

Open
santipalenque wants to merge 3 commits into
mainfrom
feature/lazy-load-reload
Open

Feature: lazy load reload#297
santipalenque wants to merge 3 commits into
mainfrom
feature/lazy-load-reload

Conversation

@santipalenque

@santipalenque santipalenque commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

https://app.clickup.com/t/9014802374/86bb1gp6z

Summary by CodeRabbit

  • New Features

    • Added automatic recovery for stale or unavailable application chunks by reloading the page.
    • Added safeguards to prevent repeated reload loops and improved reporting when recovery fails.
    • Added support for handling chunk-loading errors across lazy-loaded features and global application errors.
  • Tests

    • Added comprehensive coverage for chunk detection, reload behavior, error handling, and recovery scenarios.
  • Chores

    • Updated the package to version 5.0.45-beta.0.

@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review available on request

  • 🔍 Trigger review

Reviews should be triggered manually for repositories with fewer than 10 stars. Select Trigger review above or comment @coderabbitai review to review the latest changes. For a full review, comment @coderabbitai full review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6113560a-0146-40b6-9ad8-29e213fd7c21

📝 Walkthrough

Walkthrough

Adds a React/webpack chunk-load recovery utility with per-build reload protection, global error handling, Sentry filtering, Jest coverage, a Webpack entry, and a package version update.

Changes

Chunk Error Recovery

Layer / File(s) Summary
Chunk error classification
src/utils/lazy-with-reload.js, src/utils/__tests__/lazy-with-reload.test.js
Detects webpack chunk errors and HTML fallback SyntaxError values, including custom filename patterns, with matching tests.
Per-build reload state
src/utils/lazy-with-reload.js, src/utils/__tests__/lazy-with-reload.test.js
Tracks build fingerprints in sessionStorage, reloads once per build, confirms recovery, and reports repeated failures through Sentry or the console.
Runtime recovery integration
src/utils/lazy-with-reload.js, src/utils/__tests__/lazy-with-reload.test.js
Adds the React.lazy wrapper, global error listeners with cleanup, pending-reload confirmation, and Sentry event filtering.
Bundle and release wiring
webpack.common.js, package.json
Adds the utility as a Webpack entry and updates the package version to 5.0.45-beta.0.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ReactLazy
  participant lazyWithReload
  participant sessionStorage
  participant Window
  participant Sentry
  ReactLazy->>lazyWithReload: import module
  lazyWithReload->>sessionStorage: check build reload state
  lazyWithReload->>Window: reload on matching chunk error
  Window->>lazyWithReload: repeated failure after reload
  lazyWithReload->>Sentry: report recovery failure
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and refers to the main change: adding lazy-load reload behavior.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/lazy-load-reload

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 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.

Inline comments:
In `@src/utils/lazy-with-reload.js`:
- Around line 40-55: Update isHtmlParsedAsScriptError to recognize Firefox’s
SyntaxError wording, “expected expression, got '<',” in addition to the existing
“Unexpected token '<'” check. Preserve the existing SyntaxError detection and
filename pattern guard, while allowing either characteristic message to trigger
the stale-chunk fallback.
- Around line 124-143: Update reloadOnChunkError to track, in memory, whether a
reload has already been triggered during the current page lifecycle or tick
before consulting persisted reload state. Have subsequent concurrent chunk
failures return without calling reportRecoveryFailed, while preserving the
existing persisted-state check for failures occurring after navigation and the
current write/sessionStorage/reload flow for the first failure.
- Around line 210-224: Update chunkErrorSentryBeforeSend to extract the relevant
filename from each Sentry exception’s stack frames and pass it with the
exception type and value to isChunkLoadError. Preserve the existing filtering
behavior for genuine chunk-load errors while ensuring the HTML-as-script
SyntaxError path requires a filename matching chunkFilenamePattern.
- Around line 61-82: Compute and cache the build fingerprint eagerly during
module evaluation, before any lazy chunks can load, instead of initializing
cachedBuildFingerprint inside getBuildFingerprint. Keep getBuildFingerprint as a
read-only accessor and preserve the existing document-scripts fingerprinting and
non-browser fallback behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a5f6667e-34d6-40cb-a728-10d5b64ff9b6

📥 Commits

Reviewing files that changed from the base of the PR and between 24b90d8 and 400adfb.

📒 Files selected for processing (4)
  • package.json
  • src/utils/__tests__/lazy-with-reload.test.js
  • src/utils/lazy-with-reload.js
  • webpack.common.js

Comment thread src/utils/lazy-with-reload.js
Comment thread src/utils/lazy-with-reload.js Outdated
Comment thread src/utils/lazy-with-reload.js
Comment thread src/utils/lazy-with-reload.js
@smarcet
smarcet requested review from smarcet and a lite review from Copilot August 11, 2026 16:47
Comment thread src/utils/lazy-with-reload.js Outdated
if (!looksLikeSyntaxError || !/Unexpected token '<'/i.test(message)) {
return false;
}
return !filename || chunkFilenamePattern.test(filename);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@santipalenque The filename-less branch of the SyntaxError heuristic turns unrelated JSON.parse failures into page reloads and hides them from Sentry.

return !filename || chunkFilenamePattern.test(filename) matches any SyntaxError whose message contains "Unexpected token '<'" when no filename is available — and the two call sites that pass no filename are exactly the ones where unrelated errors arrive. Verified concrete scenario (V8/Chromium): new Response('<html>502</html>').json() rejects with SyntaxError: Unexpected token '<', "<html>502</html>" is not valid JSON. If that rejection goes unhandled (a fetch of an API that returned an HTML 502/proxy/auth page), onUnhandledRejection (line 189) passes it here with filename === undefined, and the user's page does a full reload mid-session — once per build, with the next occurrence filing a false "Chunk load error persisted after auto-reload" report. The same filename-less call in chunkErrorSentryBeforeSend (line 218) permanently drops this whole class of errors from Sentry monitoring.

Tightening this loses no genuine webpack case: webpack's JSONP runtime sets error.name = "ChunkLoadError" on every chunk-load failure it detects through the import() promise, including the "script responded 200 with HTML but the chunk never registered" case (webpack/lib/web/JsonpChunkLoadingRuntimeModule.js, loadingEnded handler, errorType 'missing') — so the ChunkLoadError path already covers the stale-chunk failure this branch is trying to catch.

Suggested fix:

  • In onUnhandledRejection, match only ChunkLoadError (import() rejections are never raw SyntaxErrors).
  • In isHtmlParsedAsScriptError, require a filename that matches chunkFilenamePattern (drop the !filename || escape) — the genuine HTML-as-script error arrives via the global error event with event.filename set.
  • In chunkErrorSentryBeforeSend, extract the culprit filename from the exception's stack frames and pass it through (as the existing CodeRabbit thread on the beforeSend hook suggests).
  • Update the test "matches a SyntaxError for 'Unexpected token <' with no filename" (lazy-with-reload.test.js line 57), which currently pins the permissive behavior, and add a regression test asserting a JSON.parse-style SyntaxError does NOT trigger a reload via unhandledrejection.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a reusable chunk-load recovery utility to the UI core library so applications can automatically recover from stale/unavailable lazy-loaded webpack chunks by triggering a guarded page reload, with reporting for non-recoverable cases.

Changes:

  • Added lazyWithReload, initChunkErrorRecovery, and Sentry filtering helpers to detect webpack chunk-load failures and recover via a one-time-per-build reload.
  • Added Jest coverage for detection, reload-guard behavior, global error/unhandledrejection handling, and Sentry filtering behavior.
  • Exposed the new utility via the webpack build entrypoints and bumped the package version to a beta release.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
webpack.common.js Exposes the new utils/lazy-with-reload entry so consumers can import it from the built package.
src/utils/lazy-with-reload.js Implements chunk-load error detection, guarded reload recovery, and Sentry beforeSend filtering helper.
src/utils/tests/lazy-with-reload.test.js Adds unit tests covering detection and recovery behavior.
package.json Updates package version to 5.0.45-beta.0.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/utils/lazy-with-reload.js
Comment thread src/utils/lazy-with-reload.js Outdated
Comment thread src/utils/__tests__/lazy-with-reload.test.js

@smarcet smarcet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@santipalenque please review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants