Skip to content

Commit

Permalink
chore: Rewrite preloadLinkObserver in pure JS
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrubisch committed Oct 5, 2023
1 parent c207f5b commit 8ae96cf
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/core/drive/preloader.js
Expand Up @@ -5,6 +5,13 @@ export class Preloader {

constructor(delegate) {
this.delegate = delegate
this.preloadLinkObserver = new MutationObserver((mutationList, observer) => {
mutationList.forEach((mutation) => {
if (mutation.attributeName !== "data-turbo-preload" || mutation.target.tagName !== "A") return

this.preloadURL(mutation.target)
})
})
}

get snapshotCache() {
Expand All @@ -15,9 +22,11 @@ export class Preloader {
if (document.readyState === "loading") {
return document.addEventListener("DOMContentLoaded", () => {
this.preloadOnLoadLinksForView(document.body)
this.observeLinksForView(document.body)
})
} else {
this.preloadOnLoadLinksForView(document.body)
this.observeLinksForView(document.body)
}
}

Expand All @@ -27,6 +36,13 @@ export class Preloader {
}
}

observeLinksForView(element) {
this.preloadLinkObserver.observe(element, {
attributes: true,
subtree: true,
})
}

async preloadURL(link) {
const location = new URL(link.href)

Expand Down

0 comments on commit 8ae96cf

Please sign in to comment.