-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (93 loc) · 2.67 KB
/
index.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
let loading = false;
let lastPathname = null;
let lastProps = null;
let Layout = null;
function clickHandler(e) {
const { origin, pathname } = e.target;
if (typeof pathname !== 'string') return;
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;
if (origin !== location.origin) return;
rerender(e.target, {
preventDefault: () => e.preventDefault(),
pushState: () => window.history.pushState({}, '', e.target.href),
}).catch(errorHandler);
}
function popstateHandler() {
rerender(location).catch(errorHandler);
}
async function main() {
document.addEventListener('click', clickHandler);
window.addEventListener('popstate', popstateHandler);
await rerender(location, { isHydrate: true }).catch(errorHandler);
}
async function errorHandler(e) {
console.error(e);
console.log('Error occured, disable spa.');
document.removeEventListener('click', clickHandler);
window.removeEventListener('popstate', popstateHandler);
}
async function rerender(
{ pathname, hash, href },
{ preventDefault = () => {}, pushState = () => {}, isHydrate = false } = {},
) {
if (pathname === lastPathname) {
if (!hash) {
preventDefault();
}
return;
}
let propsPath = pathname;
if (propsPath.endsWith('/')) {
propsPath += 'index.html';
}
if (!propsPath.endsWith('.html')) {
return;
}
propsPath = propsPath.replace(/\.html$/, '_props.js');
preventDefault();
if (loading === true) {
return;
}
loading = true;
if (!isHydrate) {
// If render not complete in 0.1s, render a loading icon instead.
setTimeout(() => {
if (loading === false) return;
window.ReactDOM.render(
window.React.createElement(Layout, {
...lastProps,
loading: true,
}),
document,
);
}, 100);
}
const props = (await import(propsPath)).default;
window.pageProps = props;
// Layout changed, reload page
if (lastProps && lastProps.layoutPath !== props.layoutPath) {
location.href = href;
return;
}
let layoutPath = props.layoutPath.replace(/\.tsx$/, '.js');
Layout = (await import(`${props.config.root}${layoutPath}`)).default;
if (isHydrate) {
window.ReactDOM.hydrate(window.React.createElement(Layout, props), document);
} else {
pushState();
window.ReactDOM.render(window.React.createElement(Layout, props), document);
if (!hash) {
window.scrollTo(0, 0);
} else {
const element = document.getElementById(hash.slice(1));
if (element) {
element.scrollIntoView();
}
}
window.dispatchEvent(new Event('rerender'));
}
lastPathname = pathname;
lastProps = props;
loading = false;
}
main().catch(errorHandler);