@@ -3,7 +3,7 @@ const privateData = new WeakMap()
3
3
const observer = new IntersectionObserver ( entries => {
4
4
for ( const entry of entries ) {
5
5
if ( entry . isIntersecting ) {
6
- const { target} = entry
6
+ const { target} = entry
7
7
observer . unobserve ( target )
8
8
if ( ! ( target instanceof IncludeFragmentElement ) ) return
9
9
if ( target . loading === 'lazy' ) {
@@ -52,7 +52,7 @@ function getData(el: IncludeFragmentElement) {
52
52
return data . data
53
53
} else {
54
54
if ( src ) {
55
- data = el . load ( )
55
+ data = fetchDataWithEvents ( el )
56
56
} else {
57
57
data = Promise . reject ( new Error ( 'missing src' ) )
58
58
}
@@ -61,6 +61,46 @@ function getData(el: IncludeFragmentElement) {
61
61
}
62
62
}
63
63
64
+ function fetchDataWithEvents ( el : IncludeFragmentElement ) {
65
+ // We mimic the same event order as <img>, including the spec
66
+ // which states events must be dispatched after "queue a task".
67
+ // https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element
68
+ return task ( )
69
+ . then ( ( ) => {
70
+ el . dispatchEvent ( new Event ( 'loadstart' ) )
71
+ return el . fetch ( el . request ( ) )
72
+ } )
73
+ . then ( response => {
74
+ if ( response . status !== 200 ) {
75
+ throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
76
+ }
77
+ const ct = response . headers . get ( 'Content-Type' )
78
+ if ( ! isWildcard ( el . accept ) && ( ! ct || ! ct . includes ( el . accept ? el . accept : 'text/html' ) ) ) {
79
+ throw new Error ( `Failed to load resource: expected ${ el . accept || 'text/html' } but was ${ ct } ` )
80
+ }
81
+ return response . text ( )
82
+ } )
83
+ . then ( data => {
84
+ // Dispatch `load` and `loadend` async to allow
85
+ // the `load()` promise to resolve _before_ these
86
+ // events are fired.
87
+ task ( ) . then ( ( ) => {
88
+ el . dispatchEvent ( new Event ( 'load' ) )
89
+ el . dispatchEvent ( new Event ( 'loadend' ) )
90
+ } )
91
+ return data
92
+ } , error => {
93
+ // Dispatch `error` and `loadend` async to allow
94
+ // the `load()` promise to resolve _before_ these
95
+ // events are fired.
96
+ task ( ) . then ( ( ) => {
97
+ el . dispatchEvent ( new Event ( 'error' ) )
98
+ el . dispatchEvent ( new Event ( 'loadend' ) )
99
+ } )
100
+ throw error
101
+ } )
102
+ }
103
+
64
104
function isWildcard ( accept : string | null ) {
65
105
return accept && ! ! accept . split ( ',' ) . find ( x => x . match ( / ^ \s * \* \/ \* / ) )
66
106
}
@@ -146,44 +186,7 @@ export default class IncludeFragmentElement extends HTMLElement {
146
186
}
147
187
148
188
load ( ) : Promise < string > {
149
- observer . unobserve ( this )
150
- // We mimic the same event order as <img>, including the spec
151
- // which states events must be dispatched after "queue a task".
152
- // https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element
153
- return task ( )
154
- . then ( ( ) => {
155
- this . dispatchEvent ( new Event ( 'loadstart' ) )
156
- return this . fetch ( this . request ( ) )
157
- } )
158
- . then ( response => {
159
- if ( response . status !== 200 ) {
160
- throw new Error ( `Failed to load resource: the server responded with a status of ${ response . status } ` )
161
- }
162
- const ct = response . headers . get ( 'Content-Type' )
163
- if ( ! isWildcard ( this . accept ) && ( ! ct || ! ct . includes ( this . accept ? this . accept : 'text/html' ) ) ) {
164
- throw new Error ( `Failed to load resource: expected ${ this . accept || 'text/html' } but was ${ ct } ` )
165
- }
166
- return response . text ( )
167
- } )
168
- . then ( data => {
169
- // Dispatch `load` and `loadend` async to allow
170
- // the `load()` promise to resolve _before_ these
171
- // events are fired.
172
- task ( ) . then ( ( ) => {
173
- this . dispatchEvent ( new Event ( 'load' ) )
174
- this . dispatchEvent ( new Event ( 'loadend' ) )
175
- } )
176
- return data
177
- } , error => {
178
- // Dispatch `error` and `loadend` async to allow
179
- // the `load()` promise to resolve _before_ these
180
- // events are fired.
181
- task ( ) . then ( ( ) => {
182
- this . dispatchEvent ( new Event ( 'error' ) )
183
- this . dispatchEvent ( new Event ( 'loadend' ) )
184
- } )
185
- throw error
186
- } )
189
+ return getData ( this )
187
190
}
188
191
189
192
fetch ( request : RequestInfo ) : Promise < Response > {
0 commit comments