Skip to content

Commit 74a6a67

Browse files
committed
optimize slide loading
1 parent 0822ff4 commit 74a6a67

3 files changed

Lines changed: 132 additions & 2 deletions

File tree

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/slides/index.html

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,34 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<meta name="description" content="Reveal.js slide deck template for the BrickSim talk." />
88
<title>BrickSim Talk</title>
9-
<script type="module" crossorigin src="./assets/index-DilHXQ3U.js"></script>
9+
<link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin>
10+
<link rel="preload" as="style" href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/katex.min.css">
11+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/katex.min.css">
12+
<link rel="preload" as="script" fetchpriority="high"
13+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/katex.min.js">
14+
<link rel="preload" as="script" fetchpriority="high"
15+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/contrib/auto-render.min.js">
16+
<link rel="preload" as="font" type="font/woff2" crossorigin
17+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_AMS-Regular.woff2">
18+
<link rel="preload" as="font" type="font/woff2" crossorigin
19+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_Caligraphic-Regular.woff2">
20+
<link rel="preload" as="font" type="font/woff2" crossorigin
21+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_Main-Bold.woff2">
22+
<link rel="preload" as="font" type="font/woff2" crossorigin
23+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_Main-BoldItalic.woff2">
24+
<link rel="preload" as="font" type="font/woff2" crossorigin
25+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_Main-Italic.woff2">
26+
<link rel="preload" as="font" type="font/woff2" crossorigin
27+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_Main-Regular.woff2">
28+
<link rel="preload" as="font" type="font/woff2" crossorigin
29+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_Math-Italic.woff2">
30+
<link rel="preload" as="font" type="font/woff2" crossorigin
31+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_Script-Regular.woff2">
32+
<link rel="preload" as="font" type="font/woff2" crossorigin
33+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_Size1-Regular.woff2">
34+
<link rel="preload" as="font" type="font/woff2" crossorigin
35+
href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/fonts/KaTeX_Size2-Regular.woff2">
36+
<script type="module" crossorigin src="./assets/index-ONxJk7O4.js"></script>
1037
<link rel="stylesheet" crossorigin href="./assets/index-CugLyPkb.css">
1138
</head>
1239

@@ -15,6 +42,7 @@
1542
<div class="slides"></div>
1643
</div>
1744

45+
<script src="./loading-status.js"></script>
1846
</body>
1947

2048
</html>

docs/slides/loading-status.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
(() => {
2+
const status = document.createElement("div");
3+
status.setAttribute("role", "status");
4+
status.setAttribute("aria-live", "polite");
5+
Object.assign(status.style, {
6+
background: "#ffffff",
7+
border: "1px solid #b7c0c7",
8+
bottom: "8px",
9+
color: "#172026",
10+
display: "none",
11+
font: "12px -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif",
12+
left: "8px",
13+
lineHeight: "1.35",
14+
maxWidth: "min(720px, calc(100vw - 16px))",
15+
overflowWrap: "anywhere",
16+
padding: "6px 8px",
17+
position: "fixed",
18+
zIndex: "9999",
19+
});
20+
21+
document.body.append(status);
22+
show("Loading presentation...");
23+
24+
function show(message) {
25+
status.textContent = message;
26+
status.style.display = "block";
27+
}
28+
29+
function hide() {
30+
status.style.display = "none";
31+
}
32+
33+
function formatAssetUrl(assetUrl) {
34+
try {
35+
const url = new URL(assetUrl, window.location.href);
36+
const currentPathParts = window.location.pathname.split("/");
37+
currentPathParts.pop();
38+
const currentDirectory = `${currentPathParts.join("/")}/`;
39+
const relativePath = url.pathname.startsWith(currentDirectory)
40+
? url.pathname.slice(currentDirectory.length)
41+
: url.pathname;
42+
43+
return decodeURI(`${relativePath}${url.search}${url.hash}`);
44+
} catch {
45+
return assetUrl;
46+
}
47+
}
48+
49+
function getMediaUrl(element) {
50+
return element.currentSrc || element.src || element.getAttribute("src");
51+
}
52+
53+
function isMediaLoaded(element) {
54+
if (element instanceof HTMLImageElement) {
55+
return element.complete;
56+
}
57+
58+
if (element instanceof HTMLVideoElement) {
59+
return element.readyState >= HTMLMediaElement.HAVE_METADATA;
60+
}
61+
62+
return true;
63+
}
64+
65+
window.brickSimLoadingStatus = {
66+
hide,
67+
show,
68+
trackMedia(root) {
69+
const pendingMedia = new Map();
70+
71+
function showNextPending() {
72+
const nextUrl = pendingMedia.values().next().value;
73+
74+
if (nextUrl) {
75+
show(`Loading ${formatAssetUrl(nextUrl)}`);
76+
} else {
77+
hide();
78+
}
79+
}
80+
81+
for (const element of root.querySelectorAll("img, video")) {
82+
const assetUrl = getMediaUrl(element);
83+
84+
if (!assetUrl || isMediaLoaded(element)) {
85+
continue;
86+
}
87+
88+
const finish = () => {
89+
pendingMedia.delete(element);
90+
showNextPending();
91+
};
92+
93+
pendingMedia.set(element, assetUrl);
94+
element.addEventListener("load", finish, { once: true });
95+
element.addEventListener("loadedmetadata", finish, { once: true });
96+
element.addEventListener("error", finish, { once: true });
97+
}
98+
99+
showNextPending();
100+
},
101+
};
102+
})();

0 commit comments

Comments
 (0)