Skip to content

Commit 2417580

Browse files
committed
fix: relative urls are tricky
1 parent 875f25a commit 2417580

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

chat/src/components/chat-provider.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,17 @@ const useAgentAPIUrl = (): string => {
7373
// `window.location.origin` but this assumes that the application owns the
7474
// entire origin.
7575
// See: https://github.com/coder/coder/issues/18779#issuecomment-3133290494 for more context.
76-
const chatUrl = new URL(basePath, window.location.origin);
77-
const agentAPIUrl = new URL("../", chatUrl.toString()).toString();
78-
if (agentAPIUrl.endsWith("/")) {
79-
return agentAPIUrl.slice(0, -1);
76+
let chatURL: string = new URL(basePath, window.location.origin).toString();
77+
// NOTE: trailing slashes and relative URLs are tricky.
78+
// https://developer.mozilla.org/en-US/docs/Web/API/URL_API/Resolving_relative_references#current_directory_relative
79+
if (!chatURL.endsWith("/")) {
80+
chatURL += "/";
8081
}
81-
return agentAPIUrl;
82+
const agentAPIURL = new URL("..", chatURL).toString();
83+
if (agentAPIURL.endsWith("/")) {
84+
return agentAPIURL.slice(0, -1);
85+
}
86+
return agentAPIURL;
8287
};
8388

8489
export function ChatProvider({ children }: PropsWithChildren) {

0 commit comments

Comments
 (0)