-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-data.js
More file actions
71 lines (60 loc) · 2.26 KB
/
Copy pathruntime-data.js
File metadata and controls
71 lines (60 loc) · 2.26 KB
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
(function () {
const STATIC_SOURCE = "static";
const API_SOURCE = "supabase";
const API_TIMEOUT_MS = 1800;
window.RUNNING_BOOTSTRAP_SOURCE = STATIC_SOURCE;
function hasUsableBootstrap(data) {
return Boolean(
data &&
Array.isArray(data.shoes) &&
data.shoes.length &&
data.lineupHistory &&
Array.isArray(data.lineupHistory.periods) &&
data.lineupHistory.periods.length &&
Array.isArray(data.lineupHistory.entries)
);
}
function applyBootstrap(data) {
window.RUNNING_SHOES = data.shoes;
window.RUNNING_LINEUP_HISTORY = data.lineupHistory;
window.RUNNING_LINEUP_PERIODS = data.lineupHistory.periods;
window.RUNNING_PRICE_QUERY_CONFIG = data.priceQueryConfig || window.RUNNING_PRICE_QUERY_CONFIG || {};
window.RUNNING_LINEUP_VERSION = data.version || data.lineupHistory.version || window.RUNNING_LINEUP_VERSION;
window.RUNNING_BOOTSTRAP_SOURCE = data.source || API_SOURCE;
}
async function loadBootstrap() {
if (shouldUseStaticOnly()) {
return;
}
const controller = new AbortController();
const timeout = window.setTimeout(() => controller.abort(), API_TIMEOUT_MS);
try {
const response = await fetch(`/api/bootstrap?v=${encodeURIComponent(window.RUNNING_BOOTSTRAP_VERSION || window.RUNNING_LINEUP_VERSION || "static")}`, {
headers: { Accept: "application/json" },
signal: controller.signal,
});
if (!response.ok) {
throw new Error(`bootstrap ${response.status}`);
}
const data = await response.json();
if (hasUsableBootstrap(data)) {
applyBootstrap(data);
}
} catch (error) {
window.RUNNING_BOOTSTRAP_SOURCE = STATIC_SOURCE;
if (isDebugBootstrap() && window.console && typeof window.console.warn === "function") {
window.console.warn("Using static lineup fallback.", error);
}
} finally {
window.clearTimeout(timeout);
}
}
function isDebugBootstrap() {
return new URLSearchParams(window.location.search).get("debugBootstrap") === "1";
}
function shouldUseStaticOnly() {
if (window.location.protocol === "file:") return true;
return window.location.hostname.endsWith("github.io");
}
window.RUNNING_BOOTSTRAP_READY = loadBootstrap();
})();