-
Notifications
You must be signed in to change notification settings - Fork 14
/
script.js
113 lines (105 loc) · 2.95 KB
/
script.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import buildAnalytics from "./analytics";
import constructPage from "./constructPage";
import observe from "./observe";
import observer from "./observer";
import navigate from "./navigate";
import prefetchFunction from "./prefetch";
import scan from "./scan";
export default function (
analytics,
cache,
containerSelector,
defaultAnimation,
delay,
forceRequestIdleCallback,
highPriorityPrefetch,
ignores,
intersectionObserverOptions,
limit,
localLinkDetector,
prefetch,
prefetchUpgradation,
progressBar,
progressBarOptions,
scanOnMount,
scrollIntoView,
scrollIntoViewOptions,
timeout
) {
const timeoutString =
typeof timeout === "number" ? `, { timeout: ${timeout} }` : "";
return `
((window, document, location) => {
const AstroSpa = window.spa ||= {};
${
localLinkDetector
? `const styleLocalLink = () => {
document.querySelectorAll("[data-active-class]").forEach((element) => {
element.classList.remove("active");
element.href === document.URL && element.classList.add(element.getAttribute("data-active-class"));
});
};
styleLocalLink();`
: ""
}
document.documentElement.insertAdjacentHTML(
"beforeend",
'<div role="region" style="display: none;" aria-live="polite" aria-atomic="true" id="astro-spa-live-region"></div>'
);
const callback = async () => {
${
cache
? `AstroSpa.cs || caches.delete("astro-spa");
AstroSpa.cs = true;
const cache = await caches.open("astro-spa");
const cachePage = async (href) => {
return cache.put(href, await fetch(href));
}`
: ""
}
let internalLinks = [];
${
prefetch
? `let prefetchTimeoutIDArray = [];
const detectDataSaverAndCache = async (href) => {
return (
navigator.connection?.saveData ${
cache ? "|| (await cache.match(href))" : ""
}
);
};`
: ""
}
${
observer(delay, intersectionObserverOptions, prefetch) +
constructPage(
analytics,
cache,
containerSelector,
defaultAnimation,
localLinkDetector,
progressBar,
progressBarOptions,
scanOnMount,
scrollIntoView,
scrollIntoViewOptions
)
}
window.addEventListener("popstate", () => { location.hash || constructPage(); });
${
navigate() +
prefetchFunction(cache, prefetch) +
scan(ignores, limit) +
observe(cache, highPriorityPrefetch, prefetch, prefetchUpgradation)
}
AstroSpa.scan();
${buildAnalytics(analytics)}
};
requestIdleCallback${
forceRequestIdleCallback
? `(callback${timeoutString});`
: `? requestIdleCallback(callback${timeoutString})
: setTimeout(callback${timeoutString});`
}
})(this, document, location);`;
}