Skip to content

Commit f415f24

Browse files
committed
Add accept header support via accept content attribute
1 parent 21f70c7 commit f415f24

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

include-fragment-element.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ export default class IncludeFragmentElement extends HTMLElement {
6565
}
6666
}
6767

68+
get accept() {
69+
return this.getAttribute('accept')
70+
}
71+
72+
set accept(val) {
73+
if (val) {
74+
this.setAttribute('accept', val)
75+
} else {
76+
this.removeAttribute('accept')
77+
}
78+
}
79+
6880
get data() {
6981
return getData(this)
7082
}
@@ -99,7 +111,7 @@ export default class IncludeFragmentElement extends HTMLElement {
99111
method: 'GET',
100112
credentials: 'same-origin',
101113
headers: {
102-
Accept: 'text/html'
114+
Accept: this.accept || 'text/html'
103115
}
104116
})
105117
}
@@ -115,8 +127,8 @@ export default class IncludeFragmentElement extends HTMLElement {
115127
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
116128
}
117129
const ct = response.headers.get('Content-Type')
118-
if (!ct || !ct.match(/^text\/html/)) {
119-
throw new Error(`Failed to load resource: expected text/html but was ${ct}`)
130+
if (!ct || !ct.match(this.accept ? this.accept : /^text\/html/)) {
131+
throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`)
120132
}
121133
return response
122134
})

0 commit comments

Comments
 (0)