Skip to content

Commit 7bf3ab1

Browse files
committed
feature: vue ssr jit v 0.0.1
1 parent c6d3f66 commit 7bf3ab1

19 files changed

+25184
-36
lines changed

LICENSE

100644100755
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
MIT License
1+
The MIT License (MIT)
22

3-
Copyright (c) 2020 ZC
3+
Copyright (c) 2013-present, Yuxi (Evan) You
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
copies of the Software, and to permit persons to whom the Software is
1010
furnished to do so, subject to the following conditions:
1111

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
1414

1515
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dev:esm": "rollup -w -c scripts/config.js --environment TARGET:web-runtime-esm",
2020
"dev:test": "karma start test/unit/karma.dev.config.js",
2121
"dev:ssr": "rollup -w -c scripts/config.js --environment TARGET:web-server-renderer",
22+
"dev:ssr-jit": "rollup -w -c scripts/config.js --environment TARGET:vue-ssr-jit-dev",
2223
"dev:compiler": "rollup -w -c scripts/config.js --environment TARGET:web-compiler ",
2324
"dev:weex": "rollup -w -c scripts/config.js --environment TARGET:weex-framework",
2425
"dev:weex:factory": "rollup -w -c scripts/config.js --environment TARGET:weex-factory",

packages/vue-ssr-jit/README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# vue-ssr-jit
2+
3+
vue ssr 运行时优化
4+
5+
## 安装
6+
```
7+
npm install --save-dev vue-ssr-jit
8+
```
9+
10+
## 使用
11+
> vue 最低版本为 2.6.x ,vue-loader 最低版本为 15.x
12+
13+
```js
14+
/*
15+
* 将官方 ssr 渲染器替换成如下渲染器
16+
*/
17+
const { createBundleRenderer } = require('vue-ssr')
18+
```
19+
20+
如下为要被替换的官方渲染器
21+
```js
22+
const { createBundleRenderer } = require('vue-server-renderer')
23+
```
24+
25+
vue-loader webpack 配置 修改如下,添加自定义 compiler
26+
```js
27+
const compiler = require('vue-ssr/compiler')
28+
29+
...
30+
31+
{
32+
test: /\.vue$/,
33+
loader: 'vue-loader',
34+
options: {
35+
compiler,
36+
}
37+
}
38+
```
39+
40+
41+
#### entry-server.js 写法一
42+
适用于在实例化前就需要载入数据的应用
43+
44+
```js
45+
/* entry-server.js */
46+
47+
import { createApp } from './app'
48+
49+
/*
50+
* base 方法用于导出一个静态的 vue 实例
51+
*/
52+
export const base = () => {
53+
const {app} = createApp()
54+
return Promise.resolve(app)
55+
}
56+
57+
/*
58+
* 默认导出的 vue 实例
59+
*/
60+
export default (context) => {
61+
return base().then((app) => {
62+
return context.initSyncData(app.$store, context.payload).then(() => {
63+
context.state = app.$store.state
64+
return app
65+
})
66+
})
67+
}
68+
```
69+
70+
#### entry-server.js 写法二
71+
适用于在渲染过程中载入数据的应用
72+
73+
```js
74+
/* entry-server.js */
75+
76+
import { createApp } from './app'
77+
78+
/*
79+
* 直接导出 Vue 实例,渲染器自动根据动静配置选择是否载入 serverPrefetch
80+
*/
81+
export default (context) => {
82+
const {app} = createApp()
83+
return Promise.resolve(app)
84+
}
85+
```
86+
87+
## 注意
88+
89+
ssr 推导需要取一份静态实例作为优化基准,在静态实例服务端渲染周期内不要出现与特定用户相关的代码操作,类似如下操作不建议使用,除非你确定此数据与用户无关
90+
91+
```js
92+
data() {
93+
let cookie = cookie
94+
try {
95+
cookie = document.cookie
96+
} catch(e) {
97+
// 以某种方式取到了后台 cookie
98+
cookie = global.xxx.cookie
99+
}
100+
return {
101+
cookie
102+
}
103+
},
104+
```
105+
106+
cookie 在 serverPrefetch 方法内可被使用,以及在服务端渲染周期结束后也可以被使用,例如:`mounted``updated` 等等
107+
108+
可以使用如下插件检测页面代码是否在服务端渲染周期内使用了 cookie
109+
```js
110+
/* entry-client.js*/
111+
import ssrSafety from 'vue-ssr/ssr-safety'
112+
113+
Vue.use(ssrSafety)
114+
115+
```
116+

0 commit comments

Comments
 (0)