更強(qiáng)大的vue ssr實(shí)現(xiàn)預(yù)取數(shù)據(jù)的方式
我在前幾天的一篇文章中吹了兩個(gè)牛皮,截圖為證:

現(xiàn)在可以松口氣的說(shuō),這兩個(gè)牛皮都實(shí)現(xiàn)了,不過(guò) vue-suspense 改名了,叫做 vue-async-manager 了,他能幫你管理 Vue 應(yīng)用中的異步組件的加載和 API 的調(diào)用,提供了與 React Suspense 同名的 `<Suspense>` 組件,Github:
shuidi-fed/vue-async-manager github.com

第二個(gè)牛皮是在開(kāi)發(fā) vue-async-manager 的過(guò)程中臨時(shí)產(chǎn)生的一個(gè)靈感,覺(jué)得相同的技巧可以用在 SSR 預(yù)取數(shù)據(jù)上,但是當(dāng)時(shí)還沒(méi)有嘗試,只是有個(gè)想法而已,不過(guò)很幸運(yùn),我成功了,GitHub:
https://github.com/HcySunYang/vue-ssr-prefetcher github.com
vue-ssr-prefetcher 為 Vue 的 SSR 提供更直觀更強(qiáng)大的數(shù)據(jù)預(yù)取方式(壓縮后僅 1kb )。讓我們來(lái)看看這張對(duì)比圖:

接下來(lái)詳細(xì)介紹一下 vue-ssr-prefetcher:
Why?
在 Vue 的服務(wù)端渲染中做數(shù)據(jù)預(yù)取的方式大概可以總結(jié)為兩種,一種是以 nuxt/ream 為代表的 asyncData 方案,另一種是 Vue 原生提供的 serverPrefetch 組件選項(xiàng)。然而這兩種方案都有一些缺點(diǎn):
nuxt/ream 的 asyncData :
- 不能訪(fǎng)問(wèn) this
- 只能用于路由組件(或 page 組件)
- 需要通過(guò)返回對(duì)象(或 promise)將數(shù)據(jù)暴露到渲染環(huán)境
Vue 原生提供的 serverPrefetch :
- 只運(yùn)行于服務(wù)端,客戶(hù)端需要另外編寫(xiě)數(shù)據(jù)獲取邏輯,并避免數(shù)據(jù)的重復(fù)獲取
- 只能預(yù)取 store 數(shù)據(jù),無(wú)法將數(shù)據(jù)暴露到組件級(jí)別的渲染環(huán)境并發(fā)送到客戶(hù)端
以上兩種方案還擁有一個(gè)共同的弊端: 不夠直觀 (不直觀,因?yàn)檫@與開(kāi)發(fā) SPA 時(shí)編寫(xiě)代碼的方式不同), vue-ssr-prefetcher 提供了一種更直觀的數(shù)據(jù)預(yù)取方案,換句話(huà)說(shuō)你在預(yù)取數(shù)據(jù)的過(guò)程中看不出來(lái)任何 SSR 的痕跡,就想在編寫(xiě) SPA 應(yīng)用一樣。
安裝
yarn add vue-ssr-prefetcher
Or use npm :
npm install vue-ssr-prefetcher --save
使用
vue-ssr-prefetcher 提供了兩個(gè) vue 插件: serverPlugin 和 clientPlugin ,分別用于 server entry 和 client entry 。
在 server entry 中:
import Vue from 'vue'
import createApp from './createApp'
// 1. 引入 serverPlugin
import { serverPlugin } from 'vue-ssr-prefetcher'
// 2. 安裝插件
Vue.use(serverPlugin)
export default async context => {
const { app, router, store } = createApp()
router.push(context.url)
await routerReady(router)
// 3. 設(shè)置 context.rendered 為 serverPlugin.done
context.rendered = serverPlugin.done
// 4. app.$$selfStore 是 serverPlugin 插件注入的屬性
context.state = {
$$stroe: store ? store.state : undefined,
$$selfStore: app.$$selfStore
}
return app
}
function routerReady (router) {
return new Promise(resolve => {
router.onReady(resolve)
})
}
serverPlugin 會(huì)在根組件實(shí)例上注入 app.$$selfStore 屬性,存儲(chǔ)著組件級(jí)別的數(shù)據(jù),你只需要將他添加到 context.state 中即可。另外,你還需要將 context.rendered 設(shè)置為 serverPlugin.done 。
在 client entry 中:
import Vue from 'vue'
import createApp from './createApp'
// 1. 引入插件
import { clientPlugin } from 'vue-ssr-prefetcher'
// 2. 安裝插件
Vue.use(clientPlugin)
const { app, router, store } = createApp()
router.onReady(() => {
// 3. 從 window.__INITIAL_STATE__ 中解構(gòu)出 $$selfStore
const { $$selfStore } = window.__INITIAL_STATE__
// 4. 將數(shù)據(jù)添加到跟組件實(shí)例
if ($$selfStore) app.$$selfStore = $$selfStore
app.$mount('#app')
// 5. 這個(gè)非常重要,它用于避免重復(fù)獲取數(shù)據(jù),并且一定要在 $mount() 函數(shù)之后
clientPlugin.$$resolved = true
})
來(lái)看看接下來(lái)如何做數(shù)據(jù)預(yù)取
按照上面的介紹配置完成后,你就可以在任何組件的 created 鉤子中發(fā)送請(qǐng)求預(yù)取數(shù)據(jù):
export default {
name: 'Example',
data() {
return { name: 'Hcy' }
},
async created() {
// this.$createFetcher() 函數(shù)是 clientPlugin 注入的
// 接收一個(gè)返回 promise 的函數(shù)作為參數(shù),例如用于請(qǐng)求 api 函數(shù)
const fetcher = this.$createFetcher(fetchName)
const res = await fetcher()
this.name = res.name
}
}
如上代碼所示,和過(guò)去唯一不同的就是你需要調(diào)用 this.$createFetcher 函數(shù)創(chuàng)建一個(gè) fetcher ,你可能會(huì)覺(jué)得這不爽,然而實(shí)際上 this.$createFetcher 做的事情很簡(jiǎn)單,下面是它的源碼:
Vue.prototype.$createFetcher = function(fetcher) {
const vm = this
return function(params: any) {
const p = fetcher(params)
vm.$$promises.push(p)
return p
}
}
僅僅是一個(gè)簡(jiǎn)單的包裝,因此我們可以把通過(guò) this.$createFetcher 函數(shù)創(chuàng)建得到的 fetcher 認(rèn)為與原函數(shù)相同。
雖然看上去和開(kāi)發(fā) SPA 應(yīng)用時(shí)沒(méi)什么不同,但 vue-ssr-prefetcher 為你做了很多事情,讓我們來(lái)對(duì)比一下,還是剛才的那種圖:

當(dāng)然了 vue-ssr-prefetcher 還為你做了:
- 避免重復(fù)獲取數(shù)據(jù)
- 當(dāng)路由跳轉(zhuǎn)時(shí)應(yīng)該能夠正常發(fā)送請(qǐng)求
而你幾乎什么都不需要做, 唯一需要做的就是使用 this.$createFetcher 函數(shù)創(chuàng)建 fetcher ,但這真的沒(méi)有任何黑科技。
為了配合 vuex 一塊使用,你只需要:
export default {
name: 'Example',
async created() {
const fetcher = this.$createFetcher(() => this.$store.dispatch('someAction'))
fetcher()
}
}
當(dāng)然了使用 mapActions 將 action 映射為 methods 也是可以的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解vue服務(wù)端渲染(SSR)初探
- 基于vue-ssr服務(wù)端渲染入門(mén)詳解
- 詳解Vue基于 Nuxt.js 實(shí)現(xiàn)服務(wù)端渲染(SSR)
- 簡(jiǎn)單的Vue SSR的示例代碼
- Vue2 SSR渲染根據(jù)不同頁(yè)面修改 meta
- Vue SSR 組件加載問(wèn)題
- Vue基于NUXT的SSR詳解
- 詳解Vue2 SSR 緩存 Api 數(shù)據(jù)
- Vue 集成 storybook的方法
- 從0到1構(gòu)建vueSSR項(xiàng)目之路由的構(gòu)建
- 淺談VueJS SSR 后端繪制內(nèi)存泄漏的相關(guān)解決經(jīng)驗(yàn)
相關(guān)文章
vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
vue?element-plus圖片預(yù)覽實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue?element-plus圖片預(yù)覽實(shí)現(xiàn)的相關(guān)資料,最近的項(xiàng)目中有圖片預(yù)覽的場(chǎng)景,也是踩了一些坑,在這里總結(jié)一下,需要的朋友可以參考下2023-07-07
vue3中element-plus表格搜索過(guò)濾數(shù)據(jù)
本文主要介紹了vue3中element-plus表格搜索過(guò)濾數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
Vue?3?中動(dòng)態(tài)賦值?ref?的應(yīng)用示例解析
Vue3引入了Composition?API,其中ref是核心概念,允許開(kāi)發(fā)者聲明響應(yīng)式狀態(tài),本文通過(guò)一個(gè)具體示例,探討了在Vue3中如何使用ref進(jìn)行動(dòng)態(tài)賦值,尤其是在處理DOM相關(guān)操作時(shí)的應(yīng)用,通過(guò)ref動(dòng)態(tài)賦值,可以有效管理組件內(nèi)的狀態(tài),提高代碼的可維護(hù)性和清晰度2024-09-09

