Skip to content

Commit b1fdd56

Browse files
author
yunkui.zhou
committed
去除模板引擎,以react来渲染页面
1 parent 64b4c49 commit b1fdd56

15 files changed

+107
-92
lines changed

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"eslint-plugin-standard": "^3.0.1",
3131
"express": "^4.16.3",
3232
"extract-text-webpack-plugin": "^4.0.0-beta.0",
33-
"hbs": "^4.0.1",
3433
"history": "^4.7.2",
3534
"less": "^3.0.1",
3635
"less-loader": "^4.1.0",
@@ -55,7 +54,6 @@
5554
"babel-plugin-transform-es3-property-literals": "^6.22.0",
5655
"babel-plugin-transform-runtime": "^6.23.0",
5756
"babel-register": "^6.26.0",
58-
"html-webpack-plugin": "^3.1.0",
5957
"mocha": "^5.0.5",
6058
"nodemon": "^1.17.2",
6159
"redux-devtools": "^3.4.1",

server/config/development.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ const injectDevelopmentTools = (app) => {
2323
}))
2424
app.use(webpackDevMiddleware(compiler, {
2525
publicPath: config.output.publicPath,
26-
serverSideRender: true
26+
serverSideRender: true,
27+
headers: {
28+
'Cache-Control': 'max-age=36000'
29+
}
2730
}))
2831
return '本地开发链接webpack与node服务'
2932
}

server/controller/components.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
'use strict';
22

33
import React from 'react'
4-
import {renderToString} from 'react-dom/server'
5-
import mapAssets from '../utils/mapAssets'
4+
import {toSream, setResHeaders} from '../react'
5+
import mapReactAssets from '../utils/mapReactAssets'
66
import Component from '../../client/pages/example/App'
7+
import Skeleton from '../react/skeleton'
78
import {StaticRouter} from 'react-router'
89
import express from 'express'
910
const router = express.Router()
1011
router.get('*', (req, res, next) => {
1112
//可以根据路径,针对某一个页面进行服务端渲染
1213
const location = `${req.baseUrl}${req.path}`
13-
console.log(req.path)
14-
const content = renderToString(<StaticRouter
15-
location={location}
16-
context={{}}
17-
basename={req.baseUrl}>
18-
<Component/>
19-
</StaticRouter>)
20-
res.render('components', {
21-
app: content,
22-
links: mapAssets('components/index.css'),
23-
scripts: mapAssets('components/index.js')
24-
})
14+
const Html = (props) => (
15+
<Skeleton
16+
title="react移动端组件"
17+
links={mapReactAssets('components/index.css')}
18+
scripts={mapReactAssets('components/index.js')}
19+
>
20+
<StaticRouter
21+
location={location}
22+
context={{}}
23+
basename={req.baseUrl}>
24+
<Component/>
25+
</StaticRouter>
26+
</Skeleton>
27+
)
28+
toSream(<Html />).pipe(setResHeaders(res))
2529
})
2630

2731
export default {

server/controller/integration.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
'use strict'
1+
'use strict';
22

33
import express from 'express'
44
import React from 'react'
5-
import {renderToString} from 'react-dom/server'
6-
import mapAssets from '../utils/mapAssets'
5+
import {toSream, setResHeaders} from '../react'
6+
import Skeleton from '../react/skeleton'
7+
import mapReactAssets from '../utils/mapReactAssets'
78
import {StaticRouter} from 'react-router'
89
import {Provider} from 'react-redux'
910
import store from '../../client/pages/tools/store/forServer'
@@ -14,20 +15,24 @@ import Integration from '../../client/pages/integration/App'
1415
const router = express.Router()
1516
router.get('*', (req, res, next) => {
1617
const location = `${req.baseUrl}${req.path}`
17-
const content = renderToString(<Provider store={store(reducers)}>
18-
<StaticRouter
19-
location={location}
20-
context={{}}
21-
basename={req.baseUrl}
18+
const Html = (props) => (
19+
<Skeleton
20+
title="整合redux"
21+
links={mapReactAssets('integration/index.css')}
22+
scripts={mapReactAssets('integration/index.js')}
2223
>
23-
<Content component={Integration}/>
24-
</StaticRouter>
25-
</Provider>)
26-
res.render('integration', {
27-
app: content,
28-
links: mapAssets('integration/index.css'),
29-
scripts: mapAssets('integration/index.js')
30-
})
24+
<Provider store={store(reducers)}>
25+
<StaticRouter
26+
location={location}
27+
context={{}}
28+
basename={req.baseUrl}
29+
>
30+
<Content component={Integration}/>
31+
</StaticRouter>
32+
</Provider>
33+
</Skeleton>
34+
)
35+
toSream(<Html />).pipe(setResHeaders(res))
3136
})
3237

3338
export default {

server/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import pages from './routes/pages'
77
const config = require('config')
88
const app = express()
99
console.log(config.injectDevelopmentTools && config.injectDevelopmentTools(app))
10-
app.set('views', path.join(__dirname, 'views'));
11-
app.set('view engine', 'hbs')
12-
app.use(express.static(path.resolve(__dirname, '../public')))
10+
const options = {
11+
dotfiles: 'ignore',
12+
etag: true,
13+
extensions: ['htm', 'html'],
14+
index: false,
15+
maxAge: 3600 * 24 * 7 * 1000,
16+
redirect: false
17+
}
18+
app.use(express.static(path.resolve(__dirname, '../public'), options))
1319

1420
//页面链式路由入口
1521
app.use('/p', pages)

server/react/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
import {renderToString, renderToNodeStream} from 'react-dom/server'
3+
4+
export const toString = (reactElement) => renderToString(reactElement)
5+
export const toSream = (reactElement) => renderToNodeStream(reactElement)
6+
export const setResHeaders = (res) => res.set({
7+
'Content-Type': 'text/html',
8+
'Cache-Control': 'no-cache,no-store',
9+
'Expires': -1
10+
})

server/react/skeleton.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
import React from 'react'
3+
4+
const Skeleton = (props) => {
5+
return (<html lang="zh-cn">
6+
<head>
7+
<meta charSet="utf-8"/>
8+
<meta name="description" content="react app"/>
9+
<meta httpEquiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
10+
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/>
11+
<meta name="format-detection" content="telephone=no"/>
12+
<title>{props.title}</title>
13+
{props.links}
14+
</head>
15+
<body>
16+
<div id="bd" className="bd">{props.children}</div>
17+
{props.scripts}
18+
</body>
19+
</html>)
20+
}
21+
export default Skeleton

server/routes/pages.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* Created by ink on 2018/5/3.
33
*/
4-
import extractMapping from '../middleware/extractMapping'
54
import express from 'express'
5+
import extractMapping from '../middleware/extractMapping'
66

77
import integrationController from '../controller/integration'
88
import componentsController from '../controller/components'

server/utils/mapReactAssets.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
import React from 'react'
3+
const mapReactAssets = (key) => {
4+
let mapObj = global.staticAssetsMapping
5+
if (typeof global.staticAssetsMapping === 'string' && global.staticAssetsMapping) {
6+
mapObj = JSON.parse(global.staticAssetsMapping)
7+
}
8+
const value = mapObj[key]
9+
const jsMatch = /\.js$/
10+
const cssMath = /\.css$/
11+
let srcString = ''
12+
if (jsMatch.test(value)) {
13+
srcString = <script src={value}></script>
14+
} else if (cssMath.test(value)) {
15+
srcString = <link rel="stylesheet" href={value} />
16+
}
17+
return srcString
18+
}
19+
export default mapReactAssets

server/utils/mapAssets.js renamed to server/utils/mapStringAssets.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Created by ink on 2018/4/3.
33
*/
4-
const mapAssets = (key) => {
4+
const mapStringAssets = (key) => {
55
let mapObj = global.staticAssetsMapping
66
if (typeof global.staticAssetsMapping === 'string' && global.staticAssetsMapping) {
77
mapObj = JSON.parse(global.staticAssetsMapping)
@@ -17,4 +17,4 @@ const mapAssets = (key) => {
1717
}
1818
return srcString
1919
}
20-
export default mapAssets
20+
export default mapStringAssets

server/views/README.md

-3
This file was deleted.

server/views/components.hbs

-17
This file was deleted.

server/views/homepage.hbs

-21
This file was deleted.

server/views/integration.hbs

-11
This file was deleted.

webpack.config.dev.babel.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const config = {
4242
disableHostCheck: true,
4343
contentBase: [contentPath],
4444
historyApiFallback: true,
45-
stats: 'minimal'
45+
stats: 'minimal',
46+
compress: true
4647
},
4748
plugins: [
4849
new webpack.NamedModulesPlugin(),

0 commit comments

Comments
 (0)