capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

bug: redirecting to external page and back causes net::ERR_CONNECTION_REFUSED

Open Nivani opened this issue 1 year ago • 1 comments

Bug Report

Capacitor Version

💊   Capacitor Doctor  💊

Latest Dependencies:

  @capacitor/cli: 4.0.1
  @capacitor/core: 4.0.1
  @capacitor/android: 4.0.1
  @capacitor/ios: 4.0.1

Installed Dependencies:

  @capacitor/ios: not installed
  @capacitor/cli: 4.0.1
  @capacitor/core: 4.0.1
  @capacitor/android: 4.0.1

[success] Android looking great! 👌

Platform(s)

  • Android

Current Behavior

When an app redirects to an external page using window.location.replace("<eg. login page here>") and that page redirects back to http://localhost, the app shows "Webpage not available. The webpage at http://localhost/... could not be loaded because: net::ERR_CONNECTION_REFUSED"

Connecting with DevTools and refreshing the page solves the problem, so the URL is not wrong.

Expected Behavior

The app should be shown instead of the "Webpage not available." page.

Code Reproduction

I've created a minimal reproduction here: https://github.com/Nivani/reproduce-ionic-redirect-issue

Exact steps to reproduce are in README.md

Other Technical Details

npm --version output: 8.3.1

node --version output: v16.14.0

pod --version output (iOS issues only):

Additional Context

Starting from this comment and later, people seem to be reporting the same issue: https://github.com/ionic-team/capacitor/issues/2391#issuecomment-640967455

This person also seems to be having the same issue: https://forum.ionicframework.com/t/willing-to-pay-err-connection-refused-android/221711/5

Since other people are reporting the same issue, I don't think the issue will be related to this, but: I'm running Windows, but my code is on WSL2 Ubuntu and building / running happens there.

Nivani avatar Aug 02 '22 15:08 Nivani

After some research, I think I found the reason for this behavior:

WebViewLocalServer.shouldInterceptRequest() is only called for the URL that redirects, but not for the redirect target.

More specifically, handler is null here when the method is called with the URL that is about to redirect back to the app: https://github.com/ionic-team/capacitor/blob/main/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java#L171 and it is not called anymore when the WebView follows the redirect to http://localhost/ resulting in the error.

I found this thread describing this behavior as working as intended: https://groups.google.com/a/chromium.org/g/android-webview-dev/c/FzajQrxaG48

Nivani avatar Aug 09 '22 16:08 Nivani

closing as duplicate of https://github.com/ionic-team/capacitor/issues/4240

jcesarmobile avatar May 26 '23 16:05 jcesarmobile

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Jun 25 '23 16:06 ionitron-bot[bot]