Skip to content

Commit 33800d2

Browse files
authored
Merge pull request rails#35525 from audiolion/feature/dynamic-actioncable-websocket-url
feat(js): Dynamic Actioncable WebSocket URL
2 parents b3e6d5f + 9333f86 commit 33800d2

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

actioncable/app/javascript/action_cable/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ export function getConfig(name) {
3030
}
3131

3232
export function createWebSocketURL(url) {
33-
if (url && !/^wss?:/i.test(url)) {
33+
const webSocketURL = typeof url === 'function' ? url() : url;
34+
35+
if (webSocketURL && !/^wss?:/i.test(webSocketURL)) {
3436
const a = document.createElement("a")
35-
a.href = url
37+
a.href = webSocketURL
3638
// Fix populating Location properties in IE. Otherwise, protocol will be blank.
3739
a.href = a.href
3840
a.protocol = a.protocol.replace("http", "ws")
3941
return a.href
4042
} else {
41-
return url
43+
return webSocketURL
4244
}
4345
}

actioncable/test/javascript/src/unit/action_cable_test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,13 @@ module("ActionCable", () => {
4141

4242
assert.equal(consumer.url, testURL)
4343
})
44+
45+
test("uses function to generate URL", assert => {
46+
const generateURL = () => {
47+
return testURL
48+
}
49+
const consumer = ActionCable.createConsumer(generateURL)
50+
assert.equal(consumer.url, testURL)
51+
})
4452
})
4553
})

0 commit comments

Comments
 (0)