Skip to content

Commit ba6677e

Browse files
committed
fix: ts check
1 parent bf3ec4b commit ba6677e

File tree

9 files changed

+34
-41
lines changed

9 files changed

+34
-41
lines changed

src/client/main/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import StyleContext from 'isomorphic-style-loader/StyleContext'
55
import { Provider } from 'react-redux'
66
import getStore from '@/store/reducers'
77
import App from './router'
8-
import routeList from './route-config'
8+
import routeList, { IRoute } from './route-config'
99
import matchRoute from '../../common/match-route'
1010
import proConfig from '../../../config/pro-config'
1111
import { Store } from 'redux'
1212

13-
function renderDom(routeList: any[], initStoreState?: Store) {
13+
function renderDom(routeList: IRoute[], initStoreState?: Store) {
1414
const insertCss = (...styles: any[]) => {
1515
const removeCss = styles.map(style => style._insertCss()) // 客户端执行,插入style
1616
return () => removeCss.forEach(dispose => dispose()) // 组件卸载时 移除当前的 style 标签
@@ -32,9 +32,9 @@ function renderDom(routeList: any[], initStoreState?: Store) {
3232
)
3333
}
3434

35-
function clientRender(routeList: any) {
35+
function clientRender(routeList: IRoute[]) {
3636
let initialData: any = null
37-
let initStoreState: any = null
37+
let initStoreState:Store|undefined
3838
const textDom = document.getElementById('ssrTextInitData') as HTMLTextAreaElement | null
3939
const storeDom = document.getElementById(
4040
'ssrTextInitStoreData'

src/client/main/route-config.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import React from 'react'
2+
export interface IRoute{
3+
path:string
4+
component: Function
5+
exact:boolean
6+
}
7+
8+
let routes: IRoute[] = []
29

3-
let routes: any[] = []
10+
// 这种不能被做到热更新 那么应该reload啊
11+
// todo 这里有bug
412
function pageNotFound({ staticContext }: any) {
513
if (staticContext) {
614
staticContext.code = 404

src/client/modules/home/routes.tsx

-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1-
import React from 'react'
21
import AsyncLoader from '@/components/asyncLoader'
32

4-
// 这种不能被做到热更新 那么应该reload啊
5-
// todo 这里有bug
6-
function pageNotFound({ staticContext }: any) {
7-
if (staticContext) {
8-
staticContext.code = 404
9-
}
103

11-
return (
12-
<div>
13-
<p>404页面page22</p>
14-
<p>12</p>
15-
</div>
16-
)
17-
}
184
export default [
195
{
206
path: '/',
217
component: AsyncLoader(() => import('./index')),
228
exact: true
23-
},
24-
{
25-
path: '*',
26-
component: pageNotFound,
27-
exact: true
289
}
2910
]

src/common/match-route.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import { IRoute } from '@/main/route-config'
12
import { matchPath } from 'react-router-dom'
23

34
export interface IMatchRoute {
45
targetRoute: any
56
targetMatch: any
67
}
7-
export default (path: string, routeList: any[]): IMatchRoute => {
8+
export default (path: string, routeList:IRoute[]): IMatchRoute => {
89
let targetRoute
910
let targetMatch
1011

1112
for (const item of routeList) {
12-
targetMatch = matchPath(path, item)
13+
targetMatch = matchPath(path, item as any)
1314
if (targetMatch) {
1415
targetRoute = item // 查找到第一个路由后停止查找
1516
break

src/server/middlewares/genHtml.tsx

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,38 @@ import { Store } from 'redux'
1111

1212
interface IGenResult {
1313
html: string
14-
fetchResult: any
14+
fetchResult:any
1515
}
1616
export default async function genHtml(
1717
path: string,
18-
insertCss: any,
18+
insertCss: Function,
1919
store: Store
2020
): Promise<IGenResult> {
2121
// 获得静态路由
2222
const staticRoutesList = await getStaticRoutes(routeList)
2323

2424
// 查找到的目标路由对象
25-
const matchResult = await matchRoute(path, staticRoutesList as any[])
25+
const matchResult = await matchRoute(path, staticRoutesList)
2626
const { targetRoute } = matchResult
2727

2828
// 进行数据预取,更新 store 内的数据
2929
let fetchDataFn
30-
let fetchResult: any = {}
30+
let fetchResult= {}
3131
if (targetRoute) {
3232
fetchDataFn = targetRoute.component ? targetRoute.component.getInitialProps : null
3333
if (fetchDataFn) {
34-
fetchResult = await fetchDataFn({ store }) // 更新 state
34+
// 更新 state
35+
fetchResult = await fetchDataFn({ store })
3536
}
3637
}
3738

38-
// 将预取数据在这里传递过去 组内通过props.staticContext获取
39-
const context = {
39+
// 将预取数据在这里传递过去 组内通过props.staticContext获取 IntrinsicAttributes
40+
const context: any= {
4041
initialData: fetchResult
4142
}
4243
const html = renderToString(
4344
<Provider store={store}>
44-
<StaticRouter location={path} context={context as any}>
45+
<StaticRouter location={path} context={context}>
4546
<StyleContext.Provider value={{ insertCss }}>
4647
<App routeList={staticRoutesList} />
4748
</StyleContext.Provider>

src/server/middlewares/ssr.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import ejs from 'ejs'
55
import fs from 'fs'
66
import path from 'path'
77
import genHtml from './genHtml'
8+
import Koa from 'koa'
89

910
const ejsPath = path.join(__dirname, '../../templates/server.ejs')
1011
const store = getStore()
11-
export default async (ctx: any, next: any): Promise<null> => {
12+
export default async (ctx:Koa.Context, next: Koa.Next): Promise<null> => {
1213
const { path } = ctx.request
1314

1415
if (path.indexOf('.') > -1) {
1516
ctx.body = null
1617
return next()
1718
}
1819
const cssObj = new Set() // CSS for all rendered React components
19-
const insertCss = (...styles: any) =>
20+
const insertCss = (...styles: any[]) =>
2021
styles.forEach((style: any) => cssObj.add(style._getContent()))
2122
const { html, fetchResult } = await genHtml(path, insertCss, store)
2223
const { page } = fetchResult || {}

src/server/utils/assets.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ interface IAssets {
55
js: string[]
66
css: string[]
77
}
8-
export default function getAssets(): any {
8+
export default function getAssets(): IAssets {
99
// let devHost = '//localhost:9001';
1010
const devHost = `//localhost:${8080}`
1111

12-
const jsFiles = ['libs.js', 'main.js', 'styles.js']
13-
const cssFiles = ['styles.css']
12+
// const jsFiles = ['libs.js', 'main.js', 'styles.js']
13+
// const cssFiles = ['styles.css']
1414

1515
const assets: IAssets = {
1616
js: [],

src/server/utils/get-ip.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import os from 'os'
33
export default function getNetworkAddress(): string {
44
const interfaces = os.networkInterfaces()
55
for (const name of Object.keys(interfaces)) {
6-
for (const item of interfaces[name] as any) {
6+
for (const item of interfaces[name]!) {
77
const { address, family, internal } = item
88
if (family === 'IPv4' && !internal) {
99
return address

src/server/utils/get-static-routes.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// 路由静态化处理
22

3+
import { IRoute } from '@/main/route-config'
34
import proConfig from '../../../config/pro-config'
45

56
const checkIsAsyncRoute = (component:any) => {
67
return component && component[proConfig.asyncComponentKey]
78
}
89

910
// 将路由转换为静态路由
10-
async function getStaticRoutes(routes:any) {
11+
async function getStaticRoutes(routes:IRoute[]) {
1112
const key = '__dynamics_route_to_static'
1213
if (global[key]) {
1314
return global[key]

0 commit comments

Comments
 (0)