-
Notifications
You must be signed in to change notification settings - Fork 154
/
index.html
78 lines (71 loc) · 2.47 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
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
<html>
<head>
<title>The Tractor Store</title>
<script src="https://unpkg.com/history@4.9.0"></script>
<script src="http://localhost:3001/pages.js" async></script>
<script src="http://localhost:3002/pages.js" async></script>
<script src="http://localhost:3003/pages.js" async></script>
<link href="/app-shell.css" rel="stylesheet" />
</head>
<body>
<div id="app-shell">
<div id="app-content"><span>content not loaded yet</span></div>
</div>
<script type="module">
console.log("app-shell init");
const appContent = document.querySelector("#app-content");
const routes = {
"/product/": "decide-pages",
"/checkout/": "checkout-pages",
"/": "inspire-pages"
};
function findComponentName(pathname) {
const prefix = Object.keys(routes).find(key =>
pathname.startsWith(key)
);
return routes[prefix];
}
function updateTopLevelComponent(location) {
console.log("app-shell updateTopLevelComponent");
const next = findComponentName(location.pathname);
const current = appContent.firstChild;
if (current.nodeName.toLowerCase() !== next) {
console.log("app-shell updateTopLevelComponent change");
console.log("route change (top level)");
const newComponent = document.createElement(next);
appContent.replaceChild(newComponent, current);
} else {
console.log("app-shell updateTopLevelComponent no-change");
}
}
window.appHistory = window.History.createBrowserHistory();
window.appHistory.listen(updateTopLevelComponent);
updateTopLevelComponent(window.location);
document.addEventListener("click", e => {
console.log("app-shell click");
if (e.target.nodeName === "A") {
const href = e.target.getAttribute("href");
window.appHistory.push(href);
e.preventDefault();
}
});
</script>
<footer>
random string: <strong></strong> / generated on app shell load<br />
will only change on a <strong>hard navigation</strong>
</footer>
<style>
footer {
position: fixed;
bottom: 0;
left: 0;
right: 0;
padding: 1rem;
}
</style>
<script type="module">
const randomString = Math.round(Math.random() * 2e5).toString(16);
document.querySelector("footer strong").innerHTML = randomString;
</script>
</body>
</html>