Skip to content

Commit 66d7f17

Browse files
Remove remaining swup:page:view listeners (#2070)
1 parent 41df85d commit 66d7f17

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

assets/js/sidebar/sidebar-list.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { el, getCurrentPageSidebarType, qs, qsAll } from '../helpers'
22
import { getSidebarNodes } from '../globals'
33

4+
// Sidebar list is only rendered when needed.
5+
// Mobile users may never see the sidebar.
46
let init = false
57
export function initialize () {
68
if (init) return
@@ -88,15 +90,15 @@ export function initialize () {
8890
}))
8991
})
9092

91-
window.addEventListener('hashchange', markCurrentHashInSidebar)
92-
93-
// We listen to swup:page:view event because we need to trigger
94-
// markCurrentHashInSidebar() before scollNodeListToCurrentCategory.
95-
window.addEventListener('swup:page:view', markCurrentHashInSidebar)
93+
// Update new sidebar list with current hash.
9694
markCurrentHashInSidebar()
9795

9896
// Triggers layout, defer.
9997
requestAnimationFrame(scrollNodeListToCurrentCategory)
98+
99+
// Keep updated with future changes.
100+
window.addEventListener('hashchange', markCurrentHashInSidebar)
101+
window.addEventListener('exdoc:loaded', markCurrentHashInSidebar)
100102
}
101103

102104
/**

assets/js/swup.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import SwupA11yPlugin from '@swup/a11y-plugin'
33
import SwupProgressPlugin from '@swup/progress-plugin'
44
import { isEmbedded } from './globals'
55

6-
window.addEventListener('DOMContentLoaded', () => {
6+
// Emit exdoc:loaded each time content loads:
7+
// - on initial page load (DOMContentLoaded)
8+
// - on subsequent SWUP page loads (page:view)
9+
const emitExdocLoaded = () => {
710
window.dispatchEvent(new Event('exdoc:loaded'))
8-
})
11+
}
12+
13+
window.addEventListener('DOMContentLoaded', emitExdocLoaded)
914

1015
if (!isEmbedded && window.location.protocol !== 'file:') {
1116
new Swup({
@@ -17,10 +22,9 @@ if (!isEmbedded && window.location.protocol !== 'file:') {
1722
path === window.location.pathname + '.html'
1823
},
1924
linkSelector: 'a[href]:not([href^="/"]):not([href^="http"])',
25+
hooks: {
26+
'page:view': emitExdocLoaded
27+
},
2028
plugins: [new SwupA11yPlugin(), new SwupProgressPlugin({delay: 500})]
2129
})
22-
23-
window.addEventListener('swup:page:view', () => {
24-
window.dispatchEvent(new Event('exdoc:loaded'))
25-
})
2630
}

0 commit comments

Comments
 (0)