Lazy load pages

This commit is contained in:
Ben Grant 2021-03-11 11:30:36 +11:00
parent 53c1921a87
commit 4c16a971a2
4 changed files with 54 additions and 8 deletions

View file

@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState, Suspense, lazy } from 'react';
import {
BrowserRouter,
Switch,
@ -6,14 +6,13 @@ import {
} from 'react-router-dom';
import { ThemeProvider, Global } from '@emotion/react';
import { Settings } from 'components';
import {
Home,
Event,
} from 'pages';
import { Settings, Loading } from 'components';
import theme from 'theme';
const Home = lazy(() => import('pages/Home/Home'));
const Event = lazy(() => import('pages/Event/Event'));
const App = () => {
const darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
const [isDark, setIsDark] = useState(darkQuery.matches);
@ -61,8 +60,16 @@ const App = () => {
})}
/>
<Switch>
<Route path="/" component={Home} exact />
<Route path="/:id" component={Event} exact />
<Route path="/" exact render={props => (
<Suspense fallback={<Loading />}>
<Home {...props} />
</Suspense>
)} />
<Route path="/:id" exact render={props => (
<Suspense fallback={<Loading />}>
<Event {...props} />
</Suspense>
)} />
</Switch>
<Settings />

View file

@ -0,0 +1,12 @@
import {
Wrapper,
Loader,
} from './loadingStyle';
const Loading = () => (
<Wrapper>
<Loader />
</Wrapper>
);
export default Loading;

View file

@ -0,0 +1,26 @@
import styled from '@emotion/styled';
export const Wrapper = styled.main`
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
`;
export const Loader = styled.div`
@keyframes load {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
height: 24px;
width: 24px;
border: 3px solid ${props => props.theme.primary};
border-left-color: transparent;
border-radius: 100px;
animation: load .5s linear infinite;
`;

View file

@ -9,6 +9,7 @@ export { default as Legend } from './Legend/Legend';
export { default as AvailabilityViewer } from './AvailabilityViewer/AvailabilityViewer';
export { default as AvailabilityEditor } from './AvailabilityEditor/AvailabilityEditor';
export { default as Error } from './Error/Error';
export { default as Loading } from './Loading/Loading';
export { default as Center } from './Center/Center';
export { default as Donate } from './Donate/Donate';