Skip to content

Commit 3f6dfd5

Browse files
authored
fix(client): continue connection discovery after HTTP errors (#146)
1 parent 96a8040 commit 3f6dfd5

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

packages/devframe/src/client/rpc.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ describe('getDevframeRpcClient — connection meta base', () => {
5656

5757
it('publishes the meta annotated with the absolute base it resolved from', async () => {
5858
const served: ConnectionMeta = { backend: 'websocket', websocket: { path: '__ws' } }
59-
vi.stubGlobal('fetch', vi.fn(async () => ({ json: async () => served }) as any))
59+
vi.stubGlobal('fetch', vi.fn(async () => ({
60+
ok: true,
61+
status: 200,
62+
json: async () => served,
63+
}) as any))
6064

6165
await getDevframeRpcClient({ baseURL: '/__foo/', otpParam: false })
6266

@@ -66,6 +70,31 @@ describe('getDevframeRpcClient — connection meta base', () => {
6670
expect(lastWsUrl()).toBe('ws://localhost:5173/__foo/__ws')
6771
})
6872

73+
it('falls back when a base returns a non-successful JSON response', async () => {
74+
const served: ConnectionMeta = { backend: 'websocket', websocket: { path: '__ws' } }
75+
const fetchSpy = vi.fn()
76+
.mockResolvedValueOnce({
77+
ok: false,
78+
status: 404,
79+
json: async () => ({ status: 404, error: 'Not Found' }),
80+
})
81+
.mockResolvedValueOnce({
82+
ok: true,
83+
status: 200,
84+
json: async () => served,
85+
})
86+
vi.stubGlobal('fetch', fetchSpy)
87+
88+
await getDevframeRpcClient({
89+
baseURL: ['/__missing/', '/__foo/'],
90+
otpParam: false,
91+
})
92+
93+
expect(fetchSpy).toHaveBeenNthCalledWith(1, '/__missing/__connection.json')
94+
expect(fetchSpy).toHaveBeenNthCalledWith(2, '/__foo/__connection.json')
95+
expect(lastWsUrl()).toBe('ws://localhost:5173/__foo/__ws')
96+
})
97+
6998
it('inherits the publisher base so a child at another base dials the shared endpoint', async () => {
7099
// A same-origin parent already published its meta, carrying the base it was
71100
// resolved against (`/__devtools/`), not this child's base (`/__foo/`).

packages/devframe/src/client/rpc.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@ export async function getDevframeRpcClient(
305305
const errors: Error[] = []
306306
for (const base of bases) {
307307
try {
308-
connectionMeta = await fetch(withBase(DEVFRAME_CONNECTION_META_FILENAME, base))
309-
.then(r => r.json()) as ConnectionMeta
308+
const response = await fetch(withBase(DEVFRAME_CONNECTION_META_FILENAME, base))
309+
if (!response.ok)
310+
throw new Error(`Failed to fetch connection meta: ${response.status}`)
311+
connectionMeta = await response.json() as ConnectionMeta
310312
resolvedBaseURL = base
311313
// Publish the meta annotated with the absolute base it was resolved
312314
// against (`baseUrl`), so a same-origin child mounted at another base

0 commit comments

Comments
 (0)