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

Skip to content

Commit ee7d21a

Browse files
committed
Preload only one <include-fragment>
When preloading menu content with an `<include-fragment>`, ignore any additional `<include-fragment>` children included in the server response.
1 parent e15b274 commit ee7d21a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DetailsMenuElement extends HTMLElement {
4444
}
4545

4646
const subscriptions = [focusOnOpen(details)]
47-
states.set(this, {details, subscriptions})
47+
states.set(this, {details, subscriptions, preloaded: false})
4848
}
4949

5050
disconnectedCallback() {
@@ -78,6 +78,12 @@ function loadFragment(event: Event) {
7878
const src = menu.getAttribute('src')
7979
if (!src) return
8080

81+
const state = states.get(menu)
82+
if (!state) return
83+
84+
if (state.preloaded) return
85+
state.preloaded = true
86+
8187
const loader = menu.querySelector('include-fragment')
8288
if (loader && !loader.hasAttribute('src')) {
8389
loader.addEventListener('loadend', () => autofocus(details))

test/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,5 +456,21 @@ describe('details-menu element', function() {
456456

457457
assert.equal('/test', loader.getAttribute('src'))
458458
})
459+
460+
it('does not fetch nested include-fragment', function() {
461+
const details = document.querySelector('details')
462+
const loader = details.querySelector('include-fragment')
463+
464+
details.dispatchEvent(new CustomEvent('mouseover'))
465+
assert.equal('/test', loader.getAttribute('src'), 'mouse hover should trigger fetch')
466+
467+
// Simulate include-fragment fetch.
468+
const response = document.createElement('include-fragment')
469+
loader.replaceWith(response)
470+
471+
details.open = true
472+
details.dispatchEvent(new CustomEvent('toggle'))
473+
assert(!response.hasAttribute('src'), 'toggle should not trigger second fetch')
474+
})
459475
})
460476
})

0 commit comments

Comments
 (0)