From 207257b827aaef109fd8d7c2e656ff8e4fac5d1a Mon Sep 17 00:00:00 2001 From: Jon Rohan Date: Tue, 2 Sep 2025 12:30:08 -0700 Subject: [PATCH 1/4] Refactor content type check for response handling --- src/include-fragment-element.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/include-fragment-element.ts b/src/include-fragment-element.ts index c8e0280..807bf39 100644 --- a/src/include-fragment-element.ts +++ b/src/include-fragment-element.ts @@ -237,10 +237,6 @@ export class IncludeFragmentElement extends HTMLElement { if (response.status !== 200) { throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`) } - const ct = response.headers.get('Content-Type') - if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) { - throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`) - } const responseText: string = await response.text() let data: string | CSPTrustedHTMLToStringable = responseText @@ -259,6 +255,10 @@ export class IncludeFragmentElement extends HTMLElement { // the `load()` promise to resolve _before_ these // events are fired. this.#task(['error', 'loadend'], error as Error) + const ct = response.headers.get('Content-Type') + if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) { + throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`) + } throw error } } From 62642dc40456f5e1b162dbd1205135b95bd85af3 Mon Sep 17 00:00:00 2001 From: Jon Rohan Date: Tue, 2 Sep 2025 14:50:34 -0700 Subject: [PATCH 2/4] Move content type check before general status !== 200 --- src/include-fragment-element.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/include-fragment-element.ts b/src/include-fragment-element.ts index 807bf39..64725cf 100644 --- a/src/include-fragment-element.ts +++ b/src/include-fragment-element.ts @@ -234,6 +234,10 @@ export class IncludeFragmentElement extends HTMLElement { try { await this.#task(['loadstart']) const response = await this.fetch(this.request()) + const ct = response.headers.get('Content-Type') + if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) { + throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`) + } if (response.status !== 200) { throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`) } @@ -255,10 +259,6 @@ export class IncludeFragmentElement extends HTMLElement { // the `load()` promise to resolve _before_ these // events are fired. this.#task(['error', 'loadend'], error as Error) - const ct = response.headers.get('Content-Type') - if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) { - throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`) - } throw error } } From c17ae31fd73c547e9d8f9166ae526acd45e7ba44 Mon Sep 17 00:00:00 2001 From: Jon Rohan Date: Tue, 2 Sep 2025 14:55:51 -0700 Subject: [PATCH 3/4] Update error message assertions in tests --- test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test.js b/test/test.js index 19066e1..7e1728b 100644 --- a/test/test.js +++ b/test/test.js @@ -213,7 +213,7 @@ suite('include-fragment-element', function () { await el.data throw new Error('el.data did not throw') } catch (error) { - assert.match(error, /the server responded with a status of 406/) + assert.match(error, /expected text\/html but was text\/plain/) } }) @@ -343,7 +343,7 @@ suite('include-fragment-element', function () { assert.equal(event.bubbles, false) assert.equal(event.cancelable, false) assert.instanceOf(event.detail.error, Error) - assert.equal(event.detail.error.message, 'Failed to load resource: the server responded with a status of 500') + assert.equal(event.detail.error.message, 'Failed to load resource: expected text/html but was text/plain;charset=UTF-8') }) test('adds is-error class on 500 status', async function () { From c7b1ca135e3a28303e4df6ffa4c31469156bbdf9 Mon Sep 17 00:00:00 2001 From: Jon Rohan Date: Tue, 2 Sep 2025 14:58:11 -0700 Subject: [PATCH 4/4] Refactor error message assertion in test --- test/test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 7e1728b..9796278 100644 --- a/test/test.js +++ b/test/test.js @@ -343,7 +343,10 @@ suite('include-fragment-element', function () { assert.equal(event.bubbles, false) assert.equal(event.cancelable, false) assert.instanceOf(event.detail.error, Error) - assert.equal(event.detail.error.message, 'Failed to load resource: expected text/html but was text/plain;charset=UTF-8') + assert.equal( + event.detail.error.message, + 'Failed to load resource: expected text/html but was text/plain;charset=UTF-8', + ) }) test('adds is-error class on 500 status', async function () {