Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a59d2c6

Browse files
committed
fix(lazy): ensure src changes don't trigger an early load
1 parent c590212 commit a59d2c6

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export default class IncludeFragmentElement extends HTMLElement {
111111
attributeChangedCallback(attribute: string, oldVal:string|null): void {
112112
if (attribute === 'src') {
113113
// Source changed after attached so replace element.
114-
if (this.isConnected) {
114+
if (this.isConnected && this.loading === 'eager') {
115115
handleData(this)
116116
}
117117
} else if (attribute === 'loading') {

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,21 @@ suite('include-fragment-element', function() {
466466
])
467467
})
468468

469+
test('loading=lazy does not load when src is changed', function() {
470+
const div = document.createElement('div')
471+
div.innerHTML = '<include-fragment loading="lazy" src="">loading</include-fragment>'
472+
div.hidden = true
473+
document.body.appendChild(div)
474+
div.firstChild.src = '/hello'
475+
return Promise.race([
476+
when(div.firstChild, 'load').then(() => {
477+
throw new Error('<include-fragment loading=lazy> loaded too early')
478+
}),
479+
new Promise(resolve => setTimeout(resolve, 100))
480+
])
481+
})
482+
483+
469484
test('loading=lazy loads as soon as element visible on page', function() {
470485
const div = document.createElement('div')
471486
div.innerHTML = '<include-fragment loading="lazy" src="/hello">loading</include-fragment>'

0 commit comments

Comments
 (0)