11import React from 'react'
22import ReactDOM from 'react-dom'
3+ import ReactDomServer from 'react-dom/server'
34import createStore from './store/createStore'
4- import AppContainer from './containers/AppContainer'
5+ // import AppContainer from './containers/AppContainer'
6+ import { match , Router , RouterContext } from 'react-router'
7+ import { Provider } from 'react-redux'
58
69// ========================================================
710// Store Instantiation
@@ -12,57 +15,94 @@ const store = createStore(initialState)
1215// ========================================================
1316// Render Setup
1417// ========================================================
15- const MOUNT_NODE = document . getElementById ( 'root' )
18+ let render
1619
17- let render = ( ) => {
18- const routes = require ( './routes/index' ) . default ( store )
19-
20- ReactDOM . render (
21- < AppContainer store = { store } routes = { routes } /> ,
22- MOUNT_NODE
23- )
24- }
20+ if ( ! window . __IS_SSR ) {
21+ const MOUNT_NODE = document . getElementById ( 'root' )
22+ render = ( ) => {
23+ const routes = require ( './routes/index' ) . default ( store )
24+ const browserHistory = require ( 'react-router' ) . browserHistory
25+ ReactDOM . render (
26+ < Provider store = { store } >
27+ < div style = { { height : '100%' } } >
28+ < Router history = { browserHistory } children = { routes } />
29+ </ div >
30+ </ Provider > ,
31+ MOUNT_NODE
32+ )
33+ }
2534
2635// ========================================================
2736// Developer Tools Setup
2837// ========================================================
29- if ( __DEV__ ) {
30- if ( window . devToolsExtension ) {
31- window . devToolsExtension . open ( )
38+ if ( __DEV__ ) {
39+ if ( window . devToolsExtension ) {
40+ window . devToolsExtension . open ( )
41+ }
3242 }
33- }
3443
3544// This code is excluded from production bundle
36- if ( __DEV__ ) {
37- if ( module . hot ) {
38- // Development render functions
39- const renderApp = render
40- const renderError = ( error ) => {
41- const RedBox = require ( 'redbox-react' ) . default
45+ if ( __DEV__ ) {
46+ if ( module . hot ) {
47+ // Development render functions
48+ const renderApp = render
49+ const renderError = ( error ) => {
50+ const RedBox = require ( 'redbox-react' ) . default
4251
43- ReactDOM . render ( < RedBox error = { error } /> , MOUNT_NODE )
44- }
52+ ReactDOM . render ( < RedBox error = { error } /> , MOUNT_NODE )
53+ }
4554
46- // Wrap render in try/catch
47- render = ( ) => {
48- try {
49- renderApp ( )
50- } catch ( error ) {
51- renderError ( error )
55+ // Wrap render in try/catch
56+ render = ( ) => {
57+ try {
58+ renderApp ( )
59+ } catch ( error ) {
60+ renderError ( error )
61+ }
5262 }
63+
64+ // Setup hot module replacement
65+ module . hot . accept ( './routes/index' , ( ) =>
66+ setImmediate ( ( ) => {
67+ ReactDOM . unmountComponentAtNode ( MOUNT_NODE )
68+ render ( )
69+ } )
70+ )
5371 }
72+ }
73+ } else {
74+ render = ( ) => {
75+ const routes = require ( './routes/index' ) . default ( store )
76+ let html = ''
77+ match ( { routes, location : '/' } , ( error , redirectionLocation , renderProps ) => {
78+ if ( error ) {
5479
55- // Setup hot module replacement
56- module . hot . accept ( './routes/index' , ( ) =>
57- setImmediate ( ( ) => {
58- ReactDOM . unmountComponentAtNode ( MOUNT_NODE )
59- render ( )
60- } )
61- )
80+ } else if ( redirectionLocation ) {
81+
82+ } else if ( renderProps ) {
83+ html = ReactDomServer . renderToString (
84+ < Provider store = { store } >
85+ < div style = { { height : '100%' } } >
86+ < RouterContext { ...renderProps } />
87+ </ div >
88+ </ Provider >
89+ )
90+ console . log ( )
91+ } else {
92+ // 404
93+ }
94+ } )
95+
96+ return html
6297 }
6398}
64-
6599// ========================================================
66100// Go!
67101// ========================================================
68- render ( )
102+ if ( ! window . __IS_SSR ) {
103+ render ( )
104+ }
105+
106+ export {
107+ render
108+ }
0 commit comments