Hey, is there a way to set the base url that RPC w...
# kilua
c
Hey, is there a way to set the base url that RPC will call? by default it must use relative paths so when we send a client side RPC call from https://localhost:3000/something/something it sends the request to /something/rpc/...
r
Hello
It can be set using
rcp_url_prefix
global javascript variable, e.g. in the main
index.html
file, before adding application bundle file:
Copy code
<script>window["rpc_url_prefix"] = `/something/something';</script>
c
Sorry, I meant it should always send the request to http://localhost:3010/rpc/ so its using the absolute url
r
I don't know if I understand so let me explain from the beginning (I should put this in the docs). By default all RPC endpoints are called by using relative paths (
rpc/*
). It should work fine for most simple cases, where the app is deployed on
/
and when it's deployed using some custom path like
/something
. But it doesn't always work. One case is when the backend is published separately from the frontend (even on different hosts). The other case is when the application uses history api client routing (nice urls like
/some/other/route
) - the app will try to call
/some/other/rpc/*
endpoints and of course it will fail. Such app can also be deployed using a custom
/something
path, which makes things even harder.
The general solution for these problems is setting
rpc_url_prefix
global variable. Its value is read as
globalThis.jsGet("rpc_url_prefix")
, so it can be set in different ways (externally using
window["rpc_url_prefix"]
or in Kotlin code using
globalThis.jsSet()
). The value of the variable (if set) is added to the path called by the RPC client as
$urlPrefix/rpc
. The value can be both absolute and relative.
So, going back to typical cases. When the backend is deployed separately, you can use the full URL address (
window["rpc_url_prefix"] = "<http://example.com/backend_path>"
).
c
ahhhh right, my apologies. when I looked at the code and saw how
rpc_url_prefix
was used I thought it still used the relative url +
rpc_url_prefix
and then /rpc. but using
window["rpc_url_prefix"] = "<http://example.com/backend_path>"
is exactly what I was after
r
When the application is using client routing but is deployed on
/
you can use
window["rpc_url_prefix"] = "";
- it will make all RPC calls starting with
/
.
When the app is also deployed on different path (
/something
) you need to know (or guess) this path and set
window["rpc_url_prefix"] = "/something"
.
I have apps, which can be deployed both on
/
or using specific path
/something
and I'm using such this code in my `index.html`:
Copy code
var prefix = "";
        if (window.location.pathname.startsWith("/something/")) prefix = "/something";
        window["rpc_url_prefix"] = prefix;
c
appreciate it!