Commit f67cbe5b authored by Viacheslav Pavlov's avatar Viacheslav Pavlov
Browse files

WIP: Loadable with SSR

parent 1133e707
import * as Loadable from 'react-loadable';
import { configureBackendApi } from 'utilities/requestUtils';
/**
......@@ -11,6 +12,8 @@ const PORT = process.env.PORT || 3000;
configureBackendApi({ apiUrl: `http://localhost:${PORT}/proxy` });
// start server on PORT
server.listen(PORT, () => {
console.log('HTTP server listening on: ' + PORT);
Loadable.preloadAll().then(() => {
server.listen(PORT, () => {
console.log('HTTP server listening on: ' + PORT);
});
});
import Loading from 'ui/common/Loading';
import * as Loadable from 'react-loadable';
import { LoadableWrapper } from 'utilities/LoadableUtil';
const publicRoutes = [
{
path: '/a/:filterCode(v.+)?',
component: Loadable({
loader: () => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/BrowsePage'),
loading: Loading,
}),
component: new LoadableWrapper(() => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/BrowsePage')),
exact: true,
},
{
path: '/a/overview/:filterCode(v.+)?',
component: Loadable({
loader: () => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/OverviewPage'),
loading: Loading,
}),
component: new LoadableWrapper(() => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/OverviewPage')),
exact: true,
},
{
path: '/a/map/:filterCode(v.+)?',
component: Loadable({
loader: () => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/MapPage'),
loading: Loading,
}),
component: new LoadableWrapper(() => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/MapPage')),
exact: true,
},
{
path: '/a/:uuid([a-z\\-0-9]+)',
component: Loadable({
loader: () => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/DisplayPage'),
loading: Loading,
}),
component: new LoadableWrapper(() => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/DisplayPage')),
exact: true,
},
{
// We need to take out the '10.' prefix for matching to work
path: '/10.:doi(\\d+\/.+)',
component: Loadable({
loader: () => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/DisplayPage'),
loading: Loading,
}),
component: new LoadableWrapper(() => import(/* webpackMode:"lazy", webpackChunkName: "accessions" */'accessions/ui/DisplayPage')),
exact: true,
},
];
......
......@@ -2,6 +2,7 @@ import * as React from 'react';
import Switch from 'react-router/Switch';
import { Route } from 'react-router-dom';
import AuthorizedRouteComponent from 'ui/common/authorized/AuthorizedRouteComponent';
import { LoadableWrapper } from 'utilities/LoadableUtil';
const renderRoutes = (routes, parentPath = '', extraProps = {}) => {
return routes ? (
......@@ -16,16 +17,18 @@ const renderRoutes = (routes, parentPath = '', extraProps = {}) => {
exact={ route.exact }
strict={ route.strict }
render={ (props) => {
const RouteComponent = route.component instanceof LoadableWrapper ? route.component.component : route.component;
if (route.auth && route.auth.length > 0) {
return (
<AuthorizedRouteComponent authorities={ route.auth }>
<Route path={ props.match.url } render={ () => (
<route.component { ...props } { ...extraProps } route={ route }/>
<RouteComponent { ...props } { ...extraProps } route={ route }/>
) }/>
</AuthorizedRouteComponent>
);
}
return <route.component { ...props } { ...extraProps } route={ route }/>;
return <RouteComponent { ...props } { ...extraProps } route={ route }/>;
} }
/>);
},
......
import Loading from 'ui/common/Loading';
import * as Loadable from 'react-loadable';
class LoadableWrapper {
public component;
public needs;
public constructor(loader) {
this.component = Loadable({
loader: () => loader()
.then((res) => {
this.needs = res.default.needs;
return res;
}),
loading: Loading,
});
}
}
export { LoadableWrapper };
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment