Skip to content

Commit 276f42e

Browse files
committed
feat: add prerender feature
1 parent 61f4e81 commit 276f42e

File tree

7 files changed

+195
-28
lines changed

7 files changed

+195
-28
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ npm run dev
4040
# build
4141
npm run build
4242

43+
# build with localStorage cache
44+
npm run build:cache
45+
4346
# deploy for Github page
4447
npm run deploy
4548

@@ -48,6 +51,9 @@ npm run docker:build
4851

4952
# run docker image in container, after this you can visit demo by: http://localhost:8080
5053
npm run docker:run
54+
55+
# run into current docker container
56+
npm run docker:exec
5157
```
5258

5359
## Feature
@@ -60,7 +66,7 @@ npm run docker:run
6066
- [x] Frontend data mock demo using [mockjs](https://github.com/nuysoft/Mock).
6167
- [x] Docker deploy.
6268
- [x] _**Refactor with Typescript**_.
63-
- [ ] Integrated with [prerender-spa-plugin](https://github.com/chrisvfritz/prerender-spa-plugin) for pre-render.
69+
- [x] Integrated with [prerender-spa-plugin](https://github.com/chrisvfritz/prerender-spa-plugin) for pre-render.
6470

6571
## Demo
6672

README.zh-CN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ npm run dev
4141
# 构建
4242
npm run build
4343

44+
# 构建带有 localStorage 缓存版本
45+
npm run build:cache
46+
4447
# 发布到 Github 页面
4548
npm run deploy
4649

@@ -49,6 +52,9 @@ npm run docker:build
4952

5053
# 运行 docker 镜像,运行后,dome可以直接访问 http://localhost:8080
5154
npm run docker:run
55+
56+
# 进入当前运行的 docker 镜像
57+
npm run docker:exec
5258
```
5359

5460
## 功能
@@ -61,7 +67,7 @@ npm run docker:run
6167
- [x] 使用 [mockjs](https://github.com/nuysoft/Mock) 进行前端数据模拟。
6268
- [x] Docker部署方案.
6369
- [x] _**使用 Typescript 重构**_.
64-
- [ ] 集成 [prerender-spa-plugin](https://github.com/chrisvfritz/prerender-spa-plugin) 进行预渲染.
70+
- [x] 集成 [prerender-spa-plugin](https://github.com/chrisvfritz/prerender-spa-plugin) 进行预渲染.
6571

6672
## Demo
6773

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"name": "x-chart",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"private": true,
55
"description": "Draggable & resizable data visualization dashboard",
66
"author": "yugasun <yuga_sun@163.com>",
77
"scripts": {
88
"dev": "vue-cli-service serve",
99
"build": "vue-cli-service build",
10+
"build:cache": "cross-env CACHE=true vue-cli-service build",
11+
"build:git-page": "cross-env GIT_PAGE=true vue-cli-service build",
1012
"test": "npm run lint",
1113
"lint": "vue-cli-service lint",
1214
"deploy": "npm run build:git-page && gh-pages -d dist --remote origin",
13-
"build:git-page": "cross-env GIT_PAGE=true vue-cli-service build",
1415
"docker:build": "docker build -t x-chart .",
1516
"docker:exec": "docker exec -it x-chart bash",
1617
"docker:run": "docker run -itd -p 8080:80 --name=x-chart x-chart"
@@ -48,6 +49,7 @@
4849
"eslint-plugin-vue": "^5.0.0",
4950
"gh-pages": "^2.0.1",
5051
"node-sass": "^4.9.0",
52+
"prerender-spa-plugin": "^3.4.0",
5153
"sass-loader": "^7.0.1",
5254
"typescript": "^3.2.1",
5355
"uglifyjs-webpack-plugin": "^2.0.1",

src/mock/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Mock from 'mockjs';
22
import dashboardAPI from './dashboard';
33

44
Mock.setup({
5-
timeout: '350-600',
5+
timeout: '10-100',
66
});
77

88
// dashboard相关

src/template/index.cache.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<script>
2323
<% var resources = [];
24-
var keys = ["chunk_vendors", "app"];
24+
var keys = ["chunkLibs", "chunkElementUI", "app"];
2525
var dependecies = [
2626
0,
2727
[keys[0]],

vue.config.js

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
const path = require('path');
22
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
3-
3+
const PrerenderSPAPlugin = require('prerender-spa-plugin');
44
const pkg = require('./package.json');
55

66
const prod = process.env.NODE_ENV === 'production';
7+
const cache = process.env.CACHE === 'true';
78
const buildForGitPage = process.env.GIT_PAGE;
89

10+
function resolve(dir) {
11+
return path.join(__dirname, dir);
12+
}
13+
14+
const splitChunks = {
15+
chunks: 'all',
16+
cacheGroups: {
17+
libs: {
18+
name: 'chunkLibs',
19+
test: /[\\/]node_modules[\\/]/,
20+
priority: 10,
21+
chunks: 'initial',
22+
},
23+
elementUI: {
24+
name: 'chunkElementUI',
25+
priority: 20,
26+
test: /[\\/]node_modules[\\/]element-ui[\\/]/,
27+
},
28+
},
29+
};
30+
931
module.exports = {
1032
parallel: true,
1133
publicPath: buildForGitPage ? '/x-chart/' : '/',
1234
runtimeCompiler: true,
1335
configureWebpack: {
1436
optimization: {
37+
splitChunks,
1538
minimizer: prod
1639
? [
1740
new UglifyJsPlugin({
@@ -24,32 +47,39 @@ module.exports = {
2447
]
2548
: [],
2649
},
50+
plugins: [
51+
new PrerenderSPAPlugin({
52+
staticDir: resolve('dist'),
53+
routes: ['/'],
54+
}),
55+
],
2756
},
2857
chainWebpack: (config) => {
2958
// modify html-webpack-html configure
3059
config.plugin('html').tap((options) => {
3160
options[0].title = pkg.name;
32-
options[0].template = path.join(__dirname, 'src/template/index.html');
33-
options[0].favicon = path.join(__dirname, 'public/favicon.ico');
34-
if (prod) {
61+
options[0].template = resolve('src/template/index.html');
62+
options[0].favicon = resolve('public/favicon.ico');
63+
64+
// only set cache = true and prod mode will use cache template
65+
if (prod && cache) {
3566
options[0].inject = false;
36-
options[0].template = path.join(
37-
__dirname,
38-
'src/template/index.cache.html',
39-
);
67+
options[0].template = resolve('src/template/index.cache.html');
4068
options[0].gitPage = buildForGitPage;
4169
}
4270
return options;
4371
});
4472

4573
// modify url-loader for fonts
46-
config.module.rule('fonts').use('url-loader').loader('url-loader').tap((options) => {
47-
options.limit = 10000;
48-
// modify publicPath for git-pages
49-
options.publicPath = buildForGitPage
50-
? '/x-chart/'
51-
: '/';
52-
return options;
53-
});
74+
config.module
75+
.rule('fonts')
76+
.use('url-loader')
77+
.loader('url-loader')
78+
.tap((options) => {
79+
options.limit = 10000;
80+
// modify publicPath for git-pages
81+
options.publicPath = buildForGitPage ? '/x-chart/' : '/';
82+
return options;
83+
});
5484
},
5585
};

0 commit comments

Comments
 (0)