This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
39 lines (37 loc) · 1.45 KB
/
index.html
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
<!DOCTYPE html>
<head>
<title>PRPL HTML</title>
</head>
<body>
<h1>Hello world!</h1>
<p>Let's try implementing the <a href="https://web.dev/apply-instant-loading-with-prpl/" target="_blank">PRPL pattern</a> with HTML files only.</p>
<p>Try navigating to the <a href="second.html">second page</a>, which I have prefetched.</p>
<p>Then, try navigating to the <a href="third.html">third page</a>, which I have not prefetched.</p>
<!-- Prefetch script -->
<script>
// TODO - Do this in a worker
const secondPage = Array.from(document.querySelectorAll('a')).filter(page => page.href.includes('second'));
fetch(secondPage)
.then(response => response.text())
.then(html => localStorage.setItem(secondPage, html))
.catch(error => console.error('Failed to prefetch the second page.' , error));
</script>
<!-- Router -->
<script>
document.addEventListener('click', e => {
const anchor = e.target.closest('a');
if (anchor && anchor.href.includes('second')) {
e.preventDefault();
try {
const html = localStorage.getItem(anchor.href);
const parser = new DOMParser();
const targetDocumentBody = parser.parseFromString(html, 'text/html').body;
document.body = targetDocumentBody;
} catch (error) {
window.location.assign(anchor.href);
console.error('Failed to get html from local storage.' , error);
}
}
});
</script>
</body>