-
Notifications
You must be signed in to change notification settings - Fork 14
/
observe.js
53 lines (49 loc) · 1.58 KB
/
observe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
export default function (
cache,
highPriorityPrefetch,
prefetch,
prefetchUpgradation
) {
return `AstroSpa.observe = async (anchor) => {
const addEventListener = anchor.addEventListener.bind(anchor);
const { href } = anchor;
const navigateCallback = (e) => {
if (!e.ctrlKey) {
e.preventDefault();
AstroSpa.navigate(href);
}
};
addEventListener("click", navigateCallback);
addEventListener("keyup", (e) => { e.key === "Enter" && navigateCallback(e); });
${
prefetch
? `if (!anchor.hasAttribute("data-spa-no-prefetch")) {
if (!(await detectDataSaverAndCache(href))) {
${
highPriorityPrefetch
? cache
? "cachePage(href)"
: "fetch(href)"
: `anchor.hasAttribute("data-spa-high-priority-prefetch")
? ${cache ? "cachePage(href)" : "fetch(href)"}
: observer.observe(anchor)`
}
}
const callback = async () => {
if (!(await detectDataSaverAndCache(href))) {
${
prefetchUpgradation
? `anchor.hasAttribute("data-spa-no-prefetch-upgradation")
? AstroSpa.prefetch(href)
: ${cache ? "cachePage(href);" : "fetch(href)"}`
: "AstroSpa.prefetch(href)"
}
}
};
addEventListener("mouseover", callback);
addEventListener("touchstart", callback, { passive: true });
}`
: ""
}
};`;
}