Skip to content

Commit 207257b

Browse files
authored
Refactor content type check for response handling
1 parent 75ac8d9 commit 207257b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/include-fragment-element.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,6 @@ export class IncludeFragmentElement extends HTMLElement {
237237
if (response.status !== 200) {
238238
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
239239
}
240-
const ct = response.headers.get('Content-Type')
241-
if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) {
242-
throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`)
243-
}
244240

245241
const responseText: string = await response.text()
246242
let data: string | CSPTrustedHTMLToStringable = responseText
@@ -259,6 +255,10 @@ export class IncludeFragmentElement extends HTMLElement {
259255
// the `load()` promise to resolve _before_ these
260256
// events are fired.
261257
this.#task(['error', 'loadend'], error as Error)
258+
const ct = response.headers.get('Content-Type')
259+
if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) {
260+
throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`)
261+
}
262262
throw error
263263
}
264264
}

0 commit comments

Comments
 (0)