Description
Bug Report
After reading the documentation as to how Capacitor works behind the scenes, it is unclear as to what the expected behavior should be when invoking APIs. Here is my understanding vs what I am seeing on my end:
- Capacitor serves static assets from an internal
localhost
server. - To address cookie issues w/ mismatched domains (
localhost
vs API endpoint domain), one can set theserver.hostname
property incapacitor.config.json
to match the endpoint domain.
Something that I am finding confusing which requires clarification in the documentation is how the capacitor router works when it is trying to distinguish requests intended for a remote server endpoint versus the static-asset-serving instance within Capacitor. Often static assets are hosted in the same domain as the API endpoints.
I assume that all requests go through the Capacitor router (static and API endpoints) and that it would determine if the requested endpoint matched an existing static asset and forward the request off to the matching server endpoint if it didn't find a matching static asset. Either this is a misunderstanding on my behalf on how the internal localhost
instance works and could use some clarification in the documentation, or my assumption is correct and I have some misconfiguration on my end that needs fixing.
This is what I am seeing when setting the hostname
parameter to match my single domain:
... <static assets successfully retrieved>
2022-12-28 10:44:16.515 6516-6576 Capacitor com.my-domain D Handling local request: https://www.my-domain.com/_app/immutable/assets/LoadMoreControl-be86b520.css
2022-12-28 10:44:16.585 6516-6576 Capacitor com.my-domain D Handling local request: https://www.my-domain.com/tracks.json?page_size=15&page_num=0
2022-12-28 10:44:16.586 6516-6576 Capacitor com.my-domain E Unable to open asset URL: https://www.my-domain.com/tracks.json?page_size=15&page_num=0
Based on the above error (Unable to open asset URL: <api endpoint>
), it seems as if Capacitor is treating all URLs as static assets. If that is the case, then what is the expected best practice for the hostname
setting? How can someone make API endpoint calls when the hostname
setting is set?
Would be helpful if some best practices were documented on how to handle cookies, remote endpoints, and static assets that would clarify similar misunderstandings.
Capacitor Version
💊 Capacitor Doctor 💊
Latest Dependencies:
@capacitor/cli: 4.6.1
@capacitor/core: 4.6.1
@capacitor/android: 4.6.1
@capacitor/ios: 4.6.1
Installed Dependencies:
@capacitor/ios: not installed
@capacitor/cli: 4.6.1
@capacitor/android: 4.6.1
@capacitor/core: 4.6.1
[success] Android looking great! 👌
Platform(s)
Android
Current Behavior
Non-static assets requests are rejected.
Expected Behavior
Unclear expectations when setting hostname
server property.
Code Reproduction
Other Technical Details
npm --version
output:
8.19.2
node --version
output:
v16.18.0
Additional Context
Ideally, Capacitor would serve static assets when present, and proxy request to endpoint when missing (as would be the case with dynamic URLs, or API endpoints).
I have a locally-running webserver that is serving the endpoints over HTTPS. My capacitor.config.json
is as follows (obfuscated):
{
"appId": "com.my-domain",
"appName": "MyDomain",
"bundledWebRuntime": false,
"webDir": "dist",
"plugins": {
"SplashScreen": {
"launchShowDuration": 0
}
},
"server": {
"hostname": "www.my-domain.com",
"androidScheme": "https"
}
}
I am using SvelteKit with a custom script that handles the build process. I also have an entry in my /etc/hosts file for www.my-domain.com along with a self-signed cert I have installed system-wide.
Activity