Skip to content

Commit 770d9ca

Browse files
fix: graciouslyFetch returns undefined on network error, crashes updates_for callers
When a fetch fails (network error, CORS, timeout), graciouslyFetch() catches the error but implicitly returns undefined. Callers in updates_for_element.js then crash with TypeError when calling .text() on the undefined result. Fix: return new Response('') in the catch block so callers always receive a valid Response object. Also add defensive guards at both call sites for defense in depth. Fixes #310 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ee1c8b1 commit 770d9ca

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

javascript/elements/updates_for_element.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export default class UpdatesForElement extends SubscribingElement {
120120
const response = await graciouslyFetch(url, {
121121
'X-Cable-Ready': 'update'
122122
})
123-
this.html[url] = await response.text()
123+
if (response) this.html[url] = await response.text()
124124
}
125125
})
126126
)
@@ -254,7 +254,7 @@ class Block {
254254
)
255255

256256
const frameTemplate = document.createElement('template')
257-
frameTemplate.innerHTML = await frameResponse.text()
257+
if (frameResponse) frameTemplate.innerHTML = await frameResponse.text()
258258

259259
// recurse here to get all nested eager loaded frames
260260
await this.resolveTurboFrames(frameTemplate.content)

javascript/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ async function graciouslyFetch (url, additionalHeaders) {
202202
return response
203203
} catch (e) {
204204
console.error(`Could not fetch ${url}`)
205+
return new Response('')
205206
}
206207
}
207208

0 commit comments

Comments
 (0)