1
1
import React from 'react'
2
2
import { renderToString } from 'react-dom/server'
3
- import { StaticRouter } from 'react-router'
3
+ import { StaticRouter } from 'react-router-dom '
4
4
import StyleContext from 'isomorphic-style-loader/StyleContext'
5
5
import { Provider } from 'react-redux'
6
6
import routeList from '../../client/main/route-config'
7
7
import matchRoute from '../../common/match-route'
8
-
8
+ import App from '../../client/main/router'
9
9
import getStaticRoutes from '../utils/get-static-routes'
10
-
11
10
import proConfig from '../../../config/pro-config'
12
-
13
11
import getAssets from '../utils/assets'
14
- // css 同构的上下文
15
-
16
12
import getStore from '../../client/store/reducers'
17
13
18
14
const store = getStore ( )
19
15
20
- console . log ( 'store' , store )
21
-
22
- // 得到 store,默认没有数据
23
-
24
16
export default async ( ctx : any , next : any ) => {
25
17
const { path } = ctx . request
26
18
@@ -33,11 +25,12 @@ export default async (ctx: any, next: any) => {
33
25
34
26
// 查找到的目标路由对象
35
27
const matchResult = await matchRoute ( path , staticRoutesList )
36
- const { targetRoute, targetMatch } = matchResult
28
+ console . log ( 'matchResult' , matchResult )
29
+ const { targetRoute } = matchResult
37
30
38
31
// 进行数据预取,更新 store 内的数据
39
32
let fetchDataFn
40
- let fetchResult = { }
33
+ let fetchResult : any = { }
41
34
if ( targetRoute ) {
42
35
fetchDataFn = targetRoute . component ? targetRoute . component . getInitialProps : null
43
36
if ( fetchDataFn ) {
@@ -67,16 +60,16 @@ export default async (ctx: any, next: any) => {
67
60
styles . forEach ( ( style : any ) => cssObj . add ( style . _getContent ( ) ) )
68
61
const html = renderToString (
69
62
< Provider store = { store } >
70
- < StaticRouter location = { path } context = { context } >
63
+ < StaticRouter location = { path } context = { context as any } >
71
64
< StyleContext . Provider value = { { insertCss } } >
72
65
< App routeList = { staticRoutesList } />
73
66
</ StyleContext . Provider >
74
67
</ StaticRouter >
75
68
</ Provider >
76
69
)
77
-
70
+ console . log ( 'html' , html )
78
71
const styles : string [ ] = [ ]
79
- ; [ ... cssObj ] . forEach ( item => {
72
+ Array . from ( cssObj ) . forEach ( ( item : any ) => {
80
73
const [ mid , content ] = item [ 0 ]
81
74
styles . push ( `<style id="s${ mid } -0">${ content } </style>` )
82
75
} )
@@ -110,4 +103,5 @@ export default async (ctx: any, next: any) => {
110
103
${ assetsMap . js . join ( '' ) }
111
104
`
112
105
await next ( )
106
+ return null
113
107
}
0 commit comments