Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(routing): add asyncComponent from react-async-component
Browse files Browse the repository at this point in the history
feat(routing): add `asyncComponent` from `react-async-component`
  • Loading branch information
Metnew committed Nov 3, 2017
1 parent 2dcaa6e commit 4d9ec26
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/common/routing/routes.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
// @flow
import React from 'react'
import {Route} from 'react-router-dom'
import RouteAuth from 'components/addons/RouteAuth'
// import Dashboard from 'containers/Dashboard'
// import Login from 'containers/Login'
import {asyncComponent} from 'react-async-component'
import {Loader} from 'semantic-ui-react'
import type {RouteItem} from 'types'

const loadLazyComponentFnConstructor = (url: string) => async () => {
const importCreator = (url: string) => async () => {
// NOTE: there isn't any duplication here
// Read Webpack docs about code-splitting for more info.
if (process.env.BROWSER) {
const loadComponent = await import(/* webpackMode: "lazy", webpackChunkName: "[request].lazy" */ `containers/${url}/index.jsx`)
return loadComponent
const resolve = import(/* webpackMode: "lazy", webpackChunkName: "[request].lazy" */ `containers/${url}/index.jsx`)
return resolve
}
const loadComponent = await import(/* webpackMode: "eager" */ `containers/${url}/index.jsx`)
return loadComponent
const resolve = import(/* webpackMode: "eager" */ `containers/${url}/index.jsx`)
return resolve
}

const asyncComponentCreator = url => {
const resolve = importCreator(url)
return asyncComponent({
resolve,
LoadingComponent () {
return <Loader>Loading...</Loader>
}
})
}

// FIXME: sidebar routes and app routes should be separated!
Expand All @@ -24,19 +35,17 @@ const routes: Array<RouteItem> = [
icon: 'newspaper',
name: 'Dashboard',
sidebarVisible: true,
lazy: true,
tag: RouteAuth,
component: loadLazyComponentFnConstructor('Dashboard')
component: asyncComponentCreator('Dashboard')
},
{
path: '/links',
name: 'Links',
exact: true,
lazy: true,
icon: 'bookmark',
sidebarVisible: true,
tag: RouteAuth,
component: loadLazyComponentFnConstructor('Links')
component: asyncComponentCreator('Links')
},
{
external: true,
Expand All @@ -48,9 +57,8 @@ const routes: Array<RouteItem> = [
{
path: '/auth',
name: 'Auth',
lazy: true,
tag: Route,
component: loadLazyComponentFnConstructor('Login')
component: asyncComponentCreator('Login')
}
]

Expand Down

0 comments on commit 4d9ec26

Please sign in to comment.