Skip to content

Commit a3538f1

Browse files
authored
Merge pull request asapzacy#36 from asapzacy/fix/react-lazy-flash
Fix FOUC when switching routes
2 parents 698c108 + 7bcb8fa commit a3538f1

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react'
2-
import { Global as GlobalStyles, css } from '@emotion/core'
2+
import { Global, css } from '@emotion/core'
33

4-
const Global = () => (
5-
<GlobalStyles
4+
const GlobalStyles = () => (
5+
<Global
66
styles={theme => css`
77
* {
88
margin: 0;
@@ -78,4 +78,4 @@ const Global = () => (
7878
/>
7979
)
8080

81-
export default Global
81+
export default GlobalStyles

src/components/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* ------- base components ------- */
2-
import Global from './Global/Global'
2+
import GlobalStyles from './Global/GlobalStyles'
33
import Header from './Header/Header'
44
import Footer from './Footer/Footer'
55
import Social from './Social/Social'
@@ -35,7 +35,7 @@ import Diamond from './Diamond/Diamond'
3535
import UpdateTime from './UpdateTime/UpdateTime'
3636

3737
export {
38-
Global,
38+
GlobalStyles,
3939
Header,
4040
Footer,
4141
Social,

src/config/Routes.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,26 @@ const TestContainer = React.lazy(() =>
2929
)
3030

3131
const Routes = () => (
32-
<Switch>
33-
<Route exact={true} path={'/'} component={HomeContainer} />
34-
<Route exact={true} path={'/mlb'} component={MlbContainer} />
35-
<Route exact={true} path={'/mlb/scores'} component={MlbContainer} />
36-
<Route path={'/mlb/scores/:date'} component={MlbContainer} />
37-
<Route exact={true} path={'/nba'} component={NbaContainer} />
38-
<Route exact={true} path={'/nba/scores'} component={NbaContainer} />
39-
<Route path={'/nba/scores/:date'} component={NbaContainer} />
40-
<Route exact={true} path={'/nfl'} component={NflContainer} />
41-
<Route exact={true} path={'/nfl/scores'} component={NflContainer} />
42-
<Route path={'/nfl/scores/:week'} component={NflContainer} />
43-
<Route exact={true} path={'/nhl'} component={NhlContainer} />
44-
<Route exact={true} path={'/nhl/scores'} component={NhlContainer} />
45-
<Route path={'/nhl/scores/:date'} component={NhlContainer} />
46-
<Route path={'/about'} component={AboutContainer} />
47-
{__DEV__ && <Route path={'/test'} component={TestContainer} />}
48-
<Route path={'*'} component={NotFoundContainer} />
49-
</Switch>
32+
<React.Suspense fallback={null}>
33+
<Switch>
34+
<Route exact={true} path={'/'} component={HomeContainer} />
35+
<Route exact={true} path={'/mlb'} component={MlbContainer} />
36+
<Route exact={true} path={'/mlb/scores'} component={MlbContainer} />
37+
<Route path={'/mlb/scores/:date'} component={MlbContainer} />
38+
<Route exact={true} path={'/nba'} component={NbaContainer} />
39+
<Route exact={true} path={'/nba/scores'} component={NbaContainer} />
40+
<Route path={'/nba/scores/:date'} component={NbaContainer} />
41+
<Route exact={true} path={'/nfl'} component={NflContainer} />
42+
<Route exact={true} path={'/nfl/scores'} component={NflContainer} />
43+
<Route path={'/nfl/scores/:week'} component={NflContainer} />
44+
<Route exact={true} path={'/nhl'} component={NhlContainer} />
45+
<Route exact={true} path={'/nhl/scores'} component={NhlContainer} />
46+
<Route path={'/nhl/scores/:date'} component={NhlContainer} />
47+
<Route path={'/about'} component={AboutContainer} />
48+
{__DEV__ && <Route path={'/test'} component={TestContainer} />}
49+
<Route path={'*'} component={NotFoundContainer} />
50+
</Switch>
51+
</React.Suspense>
5052
)
5153

5254
export default Routes

src/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React from 'react'
22
import { render } from 'react-dom'
33
import { BrowserRouter as Router } from 'react-router-dom'
4-
import { AppContainer } from 'react-hot-loader'
4+
import { AppContainer as ReactHotLoader } from 'react-hot-loader'
55
import { ThemeProvider } from 'emotion-theming'
66
import ReactGA from 'react-ga'
77
import * as Sentry from '@sentry/browser'
88

99
import { MainContainer as Root } from 'containers'
10-
import { Global } from 'components'
10+
import { GlobalStyles } from 'components'
1111
import theme from './theme'
1212

1313
require('dotenv').config()
@@ -22,14 +22,12 @@ if (process.env.NODE_ENV === 'production') {
2222
const renderApp = Root => {
2323
render(
2424
<ThemeProvider theme={theme}>
25-
<Global />
26-
<AppContainer>
25+
<GlobalStyles />
26+
<ReactHotLoader>
2727
<Router>
28-
<React.Suspense fallback={<div />}>
29-
<Root />
30-
</React.Suspense>
28+
<Root />
3129
</Router>
32-
</AppContainer>
30+
</ReactHotLoader>
3331
</ThemeProvider>,
3432
document.getElementById('root')
3533
)

0 commit comments

Comments
 (0)