Skip to content

Commit 5dcbe5f

Browse files
committed
Added prefetching to HTML books
Took 1 hour 33 minutes
1 parent 72fa00d commit 5dcbe5f

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

_layouts/book-html.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@
3636
{% endunless %}
3737
</div>
3838

39-
<script src="/assets/js/resize.js"></script>
39+
<script src="/assets/js/book-html.js"></script>

assets/js/book-html.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const page = document.querySelector(".post");
2+
const content = document.querySelector(".book-page-html");
3+
4+
const scale = (page, content) => { content.style.transform = `scale(${Math.min(1, page.clientWidth / content.scrollWidth)})`; }
5+
scale(page, content);
6+
// eliminates resize flashes
7+
content.style.visibility = "visible";
8+
window.addEventListener("resize", () => scale(page, content));
9+
10+
// prefetch HTML and image
11+
content.querySelectorAll("a").forEach(anchor => {
12+
if (!anchor.href) return;
13+
const listener = () => {
14+
const link_html = document.createElement('link');
15+
link_html.href = anchor.href;
16+
link_html.rel = "prefetch";
17+
link_html.type = "document";
18+
document.head.appendChild(link_html);
19+
20+
const parts = anchor.href.split('/');
21+
const book = parts[parts.length - 3].replace("-html", "").replace("-","_");
22+
const image = parts[parts.length - 2].replace("-","_");
23+
24+
const link_img = document.createElement('link');
25+
link_img.href = `/assets/images/book/tinkers/3.10.2/${book}/${image}.png`;
26+
link_img.rel = "prefetch";
27+
link_img.type = "image";
28+
document.head.appendChild(link_img);
29+
30+
anchor.removeEventListener("mouseover", listener);
31+
};
32+
anchor.addEventListener('mouseover', listener);
33+
});

assets/js/resize.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)