Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 837fcd2

Browse files
committed
Move homepage rendering logic to routes so its ssr-able
1 parent 73e6297 commit 837fcd2

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

src/index.js

+8-27
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { initStore } from './store';
1313
import { getItemFromStorage } from './helpers/localStorage';
1414
import { theme } from './components/theme';
1515
import Routes from './routes';
16-
import Homepage from './views/homepage';
1716
import { addToastWithTimeout } from './actions/toasts';
1817
import registerServiceWorker from './registerServiceWorker';
1918
import type { ServiceWorkerResult } from './registerServiceWorker';
@@ -49,34 +48,16 @@ const store = initStore(window.__SERVER_STATE__ || initialState, {
4948
});
5049

5150
function render() {
52-
// if user is not stored in localStorage and they visit a blacklist url
53-
if (
54-
(!existingUser || existingUser === null) &&
55-
(window.location.pathname === '/' ||
56-
window.location.pathname === '/messages' ||
57-
window.location.pathname === '/messages/new' ||
58-
window.location.pathname === '/notifications')
59-
) {
60-
return ReactDOM.render(
61-
<Router history={history}>
51+
return ReactDOM.render(
52+
<Router history={history}>
53+
<ApolloProvider store={store} client={client}>
6254
<ThemeProvider theme={theme}>
63-
<Homepage />
55+
<Routes />
6456
</ThemeProvider>
65-
</Router>,
66-
document.querySelector('#root')
67-
);
68-
} else {
69-
return ReactDOM.render(
70-
<Router history={history}>
71-
<ApolloProvider store={store} client={client}>
72-
<ThemeProvider theme={theme}>
73-
<Routes />
74-
</ThemeProvider>
75-
</ApolloProvider>
76-
</Router>,
77-
document.querySelector('#root')
78-
);
79-
}
57+
</ApolloProvider>
58+
</Router>,
59+
document.querySelector('#root')
60+
);
8061
}
8162

8263
try {

src/routes.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import React, { Component } from 'react';
33
//$FlowFixMe
44
import { Route, Switch, Redirect } from 'react-router';
5+
import { connect } from 'react-redux';
56
//$FlowFixMe
67
import styled, { ThemeProvider } from 'styled-components';
78
import generateMetaInfo from 'shared/generate-meta-info';
@@ -27,6 +28,8 @@ import communitySettings from './views/communitySettings';
2728
import channelSettings from './views/channelSettings';
2829
import NewCommunity from './views/newCommunity';
2930
import ThreadSlider from './views/threadSlider';
31+
import Homepage from './views/homepage';
32+
import homepageFallback from './views/homepage/fallback';
3033

3134
const About = () =>
3235
<div>
@@ -68,8 +71,12 @@ class Routes extends Component {
6871
https://reacttraining.com/react-router/web/api/Switch
6972
*/}
7073
<Switch>
71-
<Route exact path="/" component={Dashboard} />
72-
<Route exact path="/home" component={Dashboard} />
74+
<Route exact path="/" component={homepageFallback(Dashboard)} />
75+
<Route
76+
exact
77+
path="/home"
78+
component={homepageFallback(Dashboard)}
79+
/>
7380

7481
{/* Public Business Pages */}
7582
<Route path="/about" component={About} />

src/views/homepage/fallback.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @flow
2+
// Fallback to the <Homepage /> component if there's no current user
3+
import React from 'react';
4+
import { connect } from 'react-redux';
5+
import Homepage from './';
6+
7+
// This is the component that determines at render time what to do
8+
const HomepageFallbackComp = props => {
9+
const { Component, currentUser, ...rest } = props;
10+
if (!currentUser || !Component) return <Homepage {...rest} />;
11+
return <Component {...rest} />;
12+
};
13+
14+
// Connect that component to the Redux state
15+
const ConnectedFallbackComp = connect(state => ({
16+
currentUser: state.users.currentUser,
17+
}))(HomepageFallbackComp);
18+
19+
const homepageFallback = Component => {
20+
return props => <ConnectedFallbackComp {...props} Component={Component} />;
21+
};
22+
23+
export default homepageFallback;

0 commit comments

Comments
 (0)