const debug = import.meta.env.DEV ? console.debug : void 0; const inBrowser = import.meta.env.SSR === false; const prefetchedUrls = /* @__PURE__ */ new Set(); const listenedAnchors = /* @__PURE__ */ new WeakSet(); let prefetchAll = __PREFETCH_PREFETCH_ALL__; let defaultStrategy = __PREFETCH_DEFAULT_STRATEGY__; let clientPrerender = __EXPERIMENTAL_CLIENT_PRERENDER__; let inited = false; function init(defaultOpts) { if (!inBrowser) return; if (inited) return; inited = true; debug?.(`[astro] Initializing prefetch script`); prefetchAll ??= defaultOpts?.prefetchAll ?? false; defaultStrategy ??= defaultOpts?.defaultStrategy ?? "hover"; initTapStrategy(); initHoverStrategy(); initViewportStrategy(); initLoadStrategy(); } function initTapStrategy() { for (const event of ["touchstart", "mousedown"]) { document.body.addEventListener( event, (e) => { if (elMatchesStrategy(e.target, "tap")) { prefetch(e.target.href, { ignoreSlowConnection: true }); } }, { passive: true } ); } } function initHoverStrategy() { let timeout; document.body.addEventListener( "focusin", (e) => { if (elMatchesStrategy(e.target, "hover")) { handleHoverIn(e); } }, { passive: true } ); document.body.addEventListener("focusout", handleHoverOut, { passive: true }); onPageLoad(() => { for (const anchor of document.getElementsByTagName("a")) { if (listenedAnchors.has(anchor)) continue; if (elMatchesStrategy(anchor, "hover")) { listenedAnchors.add(anchor); anchor.addEventListener("mouseenter", handleHoverIn, { passive: true }); anchor.addEventListener("mouseleave", handleHoverOut, { passive: true }); } } }); function handleHoverIn(e) { const href = e.target.href; if (timeout) { clearTimeout(timeout); } timeout = setTimeout(() => { prefetch(href); }, 80); } function handleHoverOut() { if (timeout) { clearTimeout(timeout); timeout = 0; } } } function initViewportStrategy() { let observer; onPageLoad(() => { for (const anchor of document.getElementsByTagName("a")) { if (listenedAnchors.has(anchor)) continue; if (elMatchesStrategy(anchor, "viewport")) { listenedAnchors.add(anchor); observer ??= createViewportIntersectionObserver(); observer.observe(anchor); } } }); } function createViewportIntersectionObserver() { const timeouts = /* @__PURE__ */ new WeakMap(); return new IntersectionObserver((entries, observer) => { for (const entry of entries) { const anchor = entry.target; const timeout = timeouts.get(anchor); if (entry.isIntersecting) { if (timeout) { clearTimeout(timeout); } timeouts.set( anchor, setTimeout(() => { observer.unobserve(anchor); timeouts.delete(anchor); prefetch(anchor.href); }, 300) ); } else { if (timeout) { clearTimeout(timeout); timeouts.delete(anchor); } } } }); } function initLoadStrategy() { onPageLoad(() => { for (const anchor of document.getElementsByTagName("a")) { if (elMatchesStrategy(anchor, "load")) { prefetch(anchor.href); } } }); } function prefetch(url, opts) { url = url.replace(/#.*/, ""); const ignoreSlowConnection = opts?.ignoreSlowConnection ?? false; if (!canPrefetchUrl(url, ignoreSlowConnection)) return; prefetchedUrls.add(url); if (clientPrerender && HTMLScriptElement.supports?.("speculationrules")) { debug?.(`[astro] Prefetching ${url} with