1
- const privateData = new WeakMap ( )
1
+ interface CachedData {
2
+ src : string
3
+ data : Promise < string | Error >
4
+ }
5
+ const privateData = new WeakMap < IncludeFragmentElement , CachedData > ( )
2
6
3
7
function isWildcard ( accept : string | null ) {
4
8
return accept && ! ! accept . split ( ',' ) . find ( x => x . match ( / ^ \s * \* \/ \* / ) )
@@ -41,7 +45,7 @@ export default class IncludeFragmentElement extends HTMLElement {
41
45
this . setAttribute ( 'accept' , val )
42
46
}
43
47
44
- get data ( ) : Promise < string > {
48
+ get data ( ) : Promise < string | Error > {
45
49
return this . #getData( )
46
50
}
47
51
@@ -97,7 +101,7 @@ export default class IncludeFragmentElement extends HTMLElement {
97
101
} )
98
102
}
99
103
100
- load ( ) : Promise < string > {
104
+ load ( ) : Promise < string | Error > {
101
105
return this . #getData( )
102
106
}
103
107
@@ -133,11 +137,14 @@ export default class IncludeFragmentElement extends HTMLElement {
133
137
this . #busy = true
134
138
this . #observer. unobserve ( this )
135
139
try {
136
- const html = await this . #getData( )
140
+ const data = await this . #getData( )
141
+ if ( data instanceof Error ) {
142
+ throw data
143
+ }
137
144
138
145
const template = document . createElement ( 'template' )
139
146
// eslint-disable-next-line github/no-inner-html
140
- template . innerHTML = html
147
+ template . innerHTML = data
141
148
const fragment = document . importNode ( template . content , true )
142
149
const canceled = ! this . dispatchEvent (
143
150
new CustomEvent ( 'include-fragment-replace' , { cancelable : true , detail : { fragment} } )
@@ -150,12 +157,13 @@ export default class IncludeFragmentElement extends HTMLElement {
150
157
}
151
158
}
152
159
153
- #getData( ) : Promise < string > {
160
+ async #getData( ) : Promise < string | Error > {
154
161
const src = this . src
155
- let data = privateData . get ( this )
156
- if ( data && data . src === src ) {
157
- return data . data
162
+ const cachedData = privateData . get ( this )
163
+ if ( cachedData && cachedData . src === src ) {
164
+ return cachedData . data
158
165
} else {
166
+ let data : Promise < string | Error >
159
167
if ( src ) {
160
168
data = this . #fetchDataWithEvents( )
161
169
} else {
0 commit comments