Skip to content

Commit 499c35e

Browse files
fix(client): normalize root WebSocket URLs (#5291)
The index segment is a client-side alias for root routes. Apply the existing normalization before path parameter substitution so $ws() targets the declared endpoint while preserving dynamic values named index.
1 parent 50b8788 commit 499c35e

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

‎src/client/client.test.ts‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,25 +1612,37 @@ describe('WebSocket URL Protocol Translation', () => {
16121612
it('Translates HTTP to ws', async () => {
16131613
const client = hc<AppType>('http://localhost')
16141614
client.index.$ws()
1615-
expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index')
1615+
expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/')
16161616
})
16171617

16181618
it('Translates HTTPS to wss', async () => {
16191619
const client = hc<AppType>('https://localhost')
16201620
client.index.$ws()
1621-
expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index')
1621+
expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/')
16221622
})
16231623

16241624
it('Keeps ws unchanged', async () => {
16251625
const client = hc<AppType>('ws://localhost')
16261626
client.index.$ws()
1627-
expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index')
1627+
expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/')
16281628
})
16291629

16301630
it('Keeps wss unchanged', async () => {
16311631
const client = hc<AppType>('wss://localhost')
16321632
client.index.$ws()
1633-
expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index')
1633+
expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/')
1634+
})
1635+
1636+
it('Preserves an index path parameter value', async () => {
1637+
const dynamicRoute = new Hono().get(
1638+
'/:id',
1639+
upgradeWebSocket(() => ({}))
1640+
)
1641+
const client = hc<typeof dynamicRoute>('http://localhost')
1642+
1643+
client[':id'].$ws({ param: { id: 'index' } })
1644+
1645+
expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index')
16341646
})
16351647
})
16361648

@@ -1672,7 +1684,7 @@ describe('WebSocket URL Protocol Translation with Query Parameters', () => {
16721684
tag: ['a', 'b'],
16731685
},
16741686
})
1675-
expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index?id=123&type=test&tag=a&tag=b')
1687+
expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/?id=123&type=test&tag=a&tag=b')
16761688
})
16771689

16781690
it('Translates HTTPS to wss and includes query parameters', async () => {
@@ -1683,7 +1695,7 @@ describe('WebSocket URL Protocol Translation with Query Parameters', () => {
16831695
type: 'secure',
16841696
},
16851697
})
1686-
expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index?id=456&type=secure')
1698+
expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/?id=456&type=secure')
16871699
})
16881700

16891701
it('Keeps ws unchanged and includes query parameters', async () => {
@@ -1694,7 +1706,7 @@ describe('WebSocket URL Protocol Translation with Query Parameters', () => {
16941706
type: 'plain',
16951707
},
16961708
})
1697-
expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index?id=789&type=plain')
1709+
expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/?id=789&type=plain')
16981710
})
16991711

17001712
it('Keeps wss unchanged and includes query parameters', async () => {
@@ -1705,7 +1717,7 @@ describe('WebSocket URL Protocol Translation with Query Parameters', () => {
17051717
type: 'secure',
17061718
},
17071719
})
1708-
expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index?id=1011&type=secure')
1720+
expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/?id=1011&type=secure')
17091721
})
17101722
})
17111723

@@ -1860,13 +1872,13 @@ describe('WebSocket Provider Integration', () => {
18601872
description: 'should initialize the WebSocket provider correctly',
18611873
url: 'http://localhost',
18621874
query: undefined,
1863-
expectedUrl: 'ws://localhost/index',
1875+
expectedUrl: 'ws://localhost/',
18641876
},
18651877
{
18661878
description: 'should correctly add query parameters to the WebSocket URL',
18671879
url: 'http://localhost',
18681880
query: { id: '123', type: 'test', tag: ['a', 'b'] },
1869-
expectedUrl: 'ws://localhost/index?id=123&type=test&tag=a&tag=b',
1881+
expectedUrl: 'ws://localhost/?id=123&type=test&tag=a&tag=b',
18701882
},
18711883
])('$description', ({ url, expectedUrl, query }) => {
18721884
const webSocketMock = vi.fn()

‎src/client/client.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ export const hc = <T extends Hono<any, any, any>, Prefix extends string = string
204204
return result.slice(baseUrl.replace(/\/+$/, '').length).replace(/^\/?/, '/')
205205
}
206206
if (method === 'ws') {
207+
const normalizedUrl = removeIndexString(url)
207208
const webSocketUrl = replaceUrlProtocol(
208-
opts.args[0]?.param ? replaceUrlParam(url, opts.args[0].param) : url,
209+
opts.args[0]?.param ? replaceUrlParam(normalizedUrl, opts.args[0].param) : normalizedUrl,
209210
'ws'
210211
)
211212
const targetUrl = new URL(webSocketUrl)

0 commit comments

Comments
 (0)