From af5c542b3ff6cbbdeec6da1b1a03e58b007fd941 Mon Sep 17 00:00:00 2001 From: tdro Date: Sat, 16 Mar 2024 15:22:06 -0400 Subject: static/js/instantpage: Add custom prefetch --- assets/js/index.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'assets') diff --git a/assets/js/index.js b/assets/js/index.js index e0e0436..f6c1686 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -165,9 +165,18 @@ } })(); (function() { + function fetch(url, method, callback) { + const http = new XMLHttpRequest(); + http.onreadystatechange = function() { + if (http.readyState === 4 && http.status === 200) { + if (callback) callback(http); + } + }; + http.open(method, url); + http.send(); + return http; + } self.addEventListener("DOMContentLoaded", function() { - let mouseoverTimer; - let lastTouchTimestamp; const prefetches = new Set(); const prefetchElement = document.createElement("link"); const isSupported = prefetchElement.relList && prefetchElement.relList.supports && prefetchElement.relList.supports("prefetch") && window.IntersectionObserver && "isIntersecting" in IntersectionObserverEntry.prototype; @@ -175,6 +184,8 @@ const allowExternalLinks = "instantAllowExternalLinks" in document.body.dataset; const useWhitelist = "instantWhitelist" in document.body.dataset; const mousedownShortcut = "instantMousedownShortcut" in document.body.dataset; + let mouseoverTimer; + let lastTouchTimestamp; let delayOnHover = 65; let useMousedown = false; let useMousedownOnly = false; @@ -354,10 +365,12 @@ return; } const prefetcher = document.createElement("link"); - prefetcher.rel = "prefetch"; + prefetcher.rel = "custom-prefetch"; prefetcher.href = url; - document.head.appendChild(prefetcher); - prefetches.add(url); + fetch(url, "GET", function() { + document.head.appendChild(prefetcher); + prefetches.add(url); + }); } }); })(); -- cgit v1.2.3