Skip to content

[Bug]: JS does not get injected into DOM if appUrl contains query parameter #7517

@theigl

Description

@theigl

Capacitor Version

💊 Capacitor Doctor 💊
Latest Dependencies:
@capacitor/cli: 6.1.0
@capacitor/core: 6.1.0
@capacitor/android: 6.1.0
@capacitor/ios: 6.1.0
Installed Dependencies:
@capacitor/cli: 6.1.0
@capacitor/android: 6.1.0
@capacitor/core: 6.1.0
@capacitor/ios: 6.1.0
[success] Android looking great! 👌

Other API Details

No response

Platforms Affected

  • iOS
  • Android
  • Web

Current Behavior

JS does not get injected into the page when appUrl contains a query parameter.

The Android Bridge attempts to normalize the path before passing it as allowedOrigins to WebViewCompat.addDocumentStartJavaScript, but it does not strip query parameters:

String allowedOrigin = appUrl;
Uri appUri = Uri.parse(appUrl);
if (appUri.getPath() != null) {
if (appUri.getPath().equals("/")) {
allowedOrigin = appUrl.substring(0, appUrl.length() - 1);
} else {
allowedOrigin = appUri.toString().replace(appUri.getPath(), "");
}
}
WebViewCompat.addDocumentStartJavaScript(webView, injector.getScriptString(), Collections.singleton(allowedOrigin));

Android includes the query parameter when evaluating allowedOrigins and JS does not get injected into the DOM.

Expected Behavior

JS gets injected into the page when the appUrl contains query parameters.

Project Reproduction

https://github.com/divatzk/capacitor-server-url-query-parameter/tree/main/capacitor-app

Additional Information

The issue can be seen in the sample project where server.url contains a query parameter:

  "server": {
    "url": "https://capacitor-server-url-param.web.app?param=test"
  }

Run the app as is and you should get this result:

capacitor-not-defined

If the query parameter is removed, Capacitor JS gets injected successfully:

capacitor-defined

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions