You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dynamic client base URL and custom WebSocket topic field #148
However, in my application, the API base URL can change at runtime (for example, when switching between servers or environments).
Ideally, I'd like to keep the same client and request instances, including their integrations with React hooks, while dynamically updating the base URL.
Questions:
Is there a recommended way to update the client's base URL without recreating the client?
Can the URL be provided dynamically, for example through a getter or callback evaluated when sending a request?
If the URL changes, will previously created requests automatically use the new URL?
How should caching and request deduplication behave when switching between different base URLs?
Expected behavior:
constclient=createClient({url: ()=>config.apiUrl,});// Or alternatively:client.setUrl("https://another-api.example.com");
WebSocket: Custom topic field ("type" instead of "topic")
Our WebSocket backend uses the "type" field instead of "topic" for message routing.
importtype{SocketInstance}from"@hyper-fetch/sockets";exporttypeDynamicSocketUrl={getUrl: ()=>string;/** Closes the current connection and opens a new one at `url`. Listeners stay subscribed. */setUrl: (url: string)=>Promise<void>;};exportconstwithDynamicSocketUrl=<TSocketextendsSocketInstance>(socket: TSocket): TSocket&DynamicSocketUrl=>{socket.onConnect(({ connection })=>{if(!connection.url)thrownewError("Socket URL is not set: call setUrl() first");returnconnection;});constsetUrl=async(next: string)=>{if(next===socket.url)return;socket.url=next;awaitsocket.disconnect();if(next)awaitsocket.connect();};returnObject.assign(socket,{getUrl: ()=>socket.url, setUrl });};
Of course, we can add a custom WS adapter, but it requires rewriting the entire setConnector. It would be great to be able to overwrite only a small part of the connector, rather than the entire logic.
Activity
present-g commentedon Oct 20, 2026
It is definitely not the best way to solve the dynamic URL issue, but it works. It would be great to find a more native solution.
HTTP wrapper:
WS wrapper:
Regarding the topic/type issue in WS.
Proposal, add an optional codec to WebsocketAdapter
Of course, we can add a custom WS adapter, but it requires rewriting the entire
setConnector. It would be great to be able to overwrite only a small part of the connector, rather than the entire logic.present-g commentedon Oct 21, 2026
@prc5, hi, could you take a look at this when you have a free moment?
prc5 commentedon Oct 21, 2026
Amazing feedback, thank you! It is now being deployed 🫡
Read about it here: #149
github-actions commentedon Oct 21, 2026
🎉 This issue has been resolved in version 9.2.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
feat(core): 🎸 client.setUrl and invalidate-all cache
feat(sockets): 🎸 socket.setUrl and encode/decode adapter options
present-g commentedon Oct 21, 2026
@prc5, awesome, thank you so much! You are the best, as usual!
prc5 commentedon Oct 22, 2026
@present-g how is it looking from your side? Everything working as expected? 👀