-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.jsx
52 lines (45 loc) · 1.34 KB
/
client.jsx
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
import { Context } from '@bikeshaving/crank';
import { renderer } from '@bikeshaving/crank/dom';
import navaid from 'navaid';
import App from './components/app';
import routes from './routes';
import './styles/styles.css';
const root = document.getElementById('root');
const $isClient = true;
const $router = navaid();
const $fetch = window.fetch.bind(window);
// TODO: with hydration rendering, I don't need the following
// in order to read server rendering-time data.
const $data = {};
for (const meta of document.head.querySelectorAll('meta[name="ssr"]') || []) {
const { id, content } = meta.dataset;
if (id) $data[id] = JSON.parse(atob(content || '') || 'null');
}
for (const [$route, module] of routes) {
$router.on($route, async ($params) => {
let $isSSR;
if (Object.keys(root.dataset).includes('ssr')) {
delete root.dataset.ssr;
$isSSR = true;
} else {
$isSSR = false;
}
Object.assign(Context.prototype, {
$isClient,
$router,
$route,
$params,
$fetch,
$isSSR,
$data,
});
const { default: Route } = await module();
renderer.render(
<App>
<Route {...$params} />
</App>,
root
);
});
}
$router.listen();