-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
52 lines (47 loc) · 1.29 KB
/
router.js
File metadata and controls
52 lines (47 loc) · 1.29 KB
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 React from 'react';
import Router from 'universal-router';
import ImportClient from 'remote-modules';
import Main from './Main';
const client = new ImportClient({
uri: `${process.browser ? '' : 'http://nginx'}/fragments`,
externalRequire: process.browser ? global.require : undefined
});
const scope = process.browser ? 'browser' : 'node';
const routes = {
children: [
{
path: '/:fragment',
action: async (ctx, { fragment }) => {
const createElement = await client.import(`<${fragment}/@${scope}>`);
if (!process.browser) {
const [css, js] = await Promise.all([
client.renderStatic(`<${fragment}/@browser>`, 'css'),
client.renderStatic(`<${fragment}/@browser>`, 'js')
]);
ctx.stylesheets.push(css);
ctx.scripts.push(js);
}
return createElement(ctx);
}
},
{
action: async ({ next }) => client.reset(next)
},
{
path: '/',
action: () => <h1>Home</h1>
}
],
action: async ctx => <Main {...ctx.store}>{await ctx.next()}</Main>
};
export default initialContext => {
const router = new Router(routes, {
context: initialContext,
errorHandler: err => {
console.error(err.stack);
router.context.status = err.code || 500;
return err.code === 404 ? <h1>Not Found</h1> : <h1>Internal Server Error</h1>;
}
});
return router;
};