@@ -3,7 +3,6 @@ import { Provider } from 'react-redux';
3
3
import { StaticRouter , matchPath } from 'react-router' ;
4
4
import { setMobileDetect , mobileParser } from 'react-responsive-redux' ;
5
5
import { renderToString } from 'react-dom/server' ;
6
- import { ErrorPage } from '@components/common' ;
7
6
import { getBundles } from 'react-loadable/webpack' ;
8
7
import Loadable from 'react-loadable' ;
9
8
import render from './render' ;
@@ -25,7 +24,8 @@ if (config.enableDynamicImports) {
25
24
}
26
25
27
26
export default function handleRender ( req , res ) {
28
- const initialState = { } ;
27
+ let context = { } , modules = [ ] , initialState = { } ;
28
+
29
29
// Create a new Redux store instance
30
30
const store = configureStore ( initialState ) ;
31
31
@@ -82,29 +82,16 @@ export default function handleRender(req, res) {
82
82
83
83
const matches = matchRoutes ( routes ) ;
84
84
85
- // No matched route, render a 404 page .
85
+ // No matched route, send an error .
86
86
if ( ! matches . length ) {
87
- res . contentType ( 'text/html' ) ;
88
- res . status ( 404 ) . send ( render ( < ErrorPage code = { 404 } /> , finalState ) ) ;
89
- return ;
87
+ return res . status ( 500 ) . send ( 'Server Error' ) ;
90
88
}
91
89
92
- // There's a match, render the component with the matched route, firing off
93
- // any fetchData methods that are statically defined on the server.
94
- const fetchData = matches . map ( match => {
95
- const { fetchData, ...rest } = match ; // eslint-disable-line no-unused-vars
96
-
97
- // return fetch data Promise, excluding unnecessary fetchData method
98
- return match . fetchData ( { store, ...rest } ) ;
99
- } ) ;
100
-
101
- let context = { } , modules = [ ] ;
102
-
103
90
const getComponent = ( ) => {
104
91
let component = (
105
92
< Provider store = { store } >
106
93
< StaticRouter context = { context } location = { req . baseUrl } >
107
- < App />
94
+ < App />
108
95
</ StaticRouter >
109
96
</ Provider >
110
97
) ;
@@ -120,19 +107,30 @@ export default function handleRender(req, res) {
120
107
return component ;
121
108
} ;
122
109
110
+ // There's a match, render the component with the matched route, firing off
111
+ // any fetchData methods that are statically defined on the server.
112
+ const fetchData = matches . map ( match => {
113
+ const { fetchData, ...rest } = match ; // eslint-disable-line no-unused-vars
114
+
115
+ // return fetch data Promise, excluding unnecessary fetchData method
116
+ return match . fetchData ( { store, ...rest } ) ;
117
+ } ) ;
118
+
123
119
// Execute the render only after all promises have been resolved.
124
120
Promise . all ( fetchData ) . then ( ( ) => {
125
121
const state = store . getState ( ) ;
126
122
const html = renderToString ( getComponent ( ) ) ;
127
123
const bundles = stats && getBundles ( stats , modules ) || [ ] ;
128
124
const markup = render ( html , state , bundles ) ;
125
+ const status = matches . length && matches [ 0 ] . match . path === '*' ? 404 : 200 ;
129
126
130
127
// A 301 redirect was rendered somewhere if context.url exists after
131
128
// rendering has happened.
132
129
if ( context . url ) {
133
130
return res . redirect ( 302 , context . url ) ;
134
131
}
135
132
136
- return res . status ( 200 ) . send ( markup ) ;
133
+ res . contentType ( 'text/html' ) ;
134
+ return res . status ( status ) . send ( markup ) ;
137
135
} ) ;
138
136
}
0 commit comments