Skip to content

fix: cache webview top frame url in onPageStarted#38

Merged
jkadamczyk merged 1 commit intomasterfrom
kuba/add-caching-of-top-frame-url
Mar 5, 2026
Merged

fix: cache webview top frame url in onPageStarted#38
jkadamczyk merged 1 commit intomasterfrom
kuba/add-caching-of-top-frame-url

Conversation

@jkadamczyk
Copy link
Collaborator

@jkadamczyk jkadamczyk commented Mar 5, 2026

This will narrow down the possibility of getting the wrong url when calling webview.getUrl.

Summary by CodeRabbit

  • Bug Fixes
    • Improved URL handling in webview messages by capturing the committed page URL at the time of page load instead of at message delivery time, ensuring more consistent and reliable URL information.

@jkadamczyk jkadamczyk requested review from a team and tian000 March 5, 2026 11:54
@coderabbitai
Copy link

coderabbitai bot commented Mar 5, 2026

📝 Walkthrough

Walkthrough

The changes add URL state tracking to RNCWebView by introducing a new mLastCommittedUrl field with getter and setter methods, and update RNCWebViewClient to capture and store the URL when page loading begins.

Changes

Cohort / File(s) Summary
URL State Tracking
android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java
Added protected nullable field mLastCommittedUrl, along with public accessor setLastCommittedUrl(@Nullable String url) and mutator getLastCommittedUrl() methods for managing committed page URL state.
Page Load Handler
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java
Integrated call to setLastCommittedUrl(url) in onPageStarted to capture and store the URL when a page begins loading.
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: caching the webview top frame URL during onPageStarted, which aligns with the core objective of reducing incorrect URL returns.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch kuba/add-caching-of-top-frame-url

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

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java`:
- Line 82: mLastCommittedUrl is shared between WebView callbacks and
`@JavascriptInterface` bridge code and needs cross-thread visibility; make
mLastCommittedUrl volatile (and apply the same volatile protection to the other
shared mutable fields noted at lines 260-266 and 497-502) so updates on the UI
thread are immediately visible to background/bridge threads, and ensure any
compound read-modify-write sequences use a synchronized block or explicit lock
to preserve atomicity where used (search for mLastCommittedUrl and the other
listed fields to locate usages).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1bc5962b-0962-49b6-98a8-c4500e6a721c

📥 Commits

Reviewing files that changed from the base of the PR and between fa84f40 and e64d297.

📒 Files selected for processing (2)
  • android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java
  • android/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java

protected boolean nestedScrollEnabled = false;
protected ProgressChangedFilter progressChangedFilter;
protected boolean mActive = true;
protected @Nullable String mLastCommittedUrl;
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Protect mLastCommittedUrl with cross-thread visibility guarantees.

mLastCommittedUrl is shared mutable state across WebView callback flow and @JavascriptInterface bridge flow. As currently implemented, stale reads are possible; this can still produce incorrect topFrameUrl under timing races.

Proposed fix
-    protected `@Nullable` String mLastCommittedUrl;
+    protected volatile `@Nullable` String mLastCommittedUrl;
According to Android WebView documentation, are methods annotated with `@JavascriptInterface` invoked on the UI thread or on a background thread, and what synchronization/visibility guarantees are required when sharing fields with UI-thread callbacks?

Also applies to: 260-266, 497-502

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java` at
line 82, mLastCommittedUrl is shared between WebView callbacks and
`@JavascriptInterface` bridge code and needs cross-thread visibility; make
mLastCommittedUrl volatile (and apply the same volatile protection to the other
shared mutable fields noted at lines 260-266 and 497-502) so updates on the UI
thread are immediately visible to background/bridge threads, and ensure any
compound read-modify-write sequences use a synchronized block or explicit lock
to preserve atomicity where used (search for mLastCommittedUrl and the other
listed fields to locate usages).

@jkadamczyk jkadamczyk marked this pull request as draft March 5, 2026 11:59
@jkadamczyk jkadamczyk marked this pull request as ready for review March 5, 2026 11:59
@jkadamczyk jkadamczyk merged commit a04d4f7 into master Mar 5, 2026
19 checks passed
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.

2 participants