Vue SSR 組件加載:Node 端渲染提示 window/document 沒有定義


業務場景

  • 首先來看一個簡單的 Vue 組件test.vue
<template>
  <div>
    <h2>clientHeight: {{ clientHeight }} px </h2>
  </div>
</template>

<script type="text/babel">
  export default {
    data(){
      return {
      }
    },
    computed :{
      clientHeight() {
        return document.body.clientHeight;
      }
    },
    mounted(){
    }
  }
</script>

  

  • 上面 test.vue 組件通過 Vue computed 屬性 clientHeight 直接獲取 document 的文檔高度,這段代碼在前端渲染是不會報錯的,也能拿到正確的值。但如果把這個組件放到 SSR(Server Side Render) 模式下, 就會報如下錯誤:
ReferenceError: document is not defined 

解決方案

  • 通過 typeof 判斷是否是存在 document 對象, 如果存在則執行后面代碼。 這種方式雖然能解決問題, 但在 Webpack 構建壓縮時, 不會執行的代碼不會被剔除,也會打包到 js 文件中去, 因為這個是在運行期才知道結果的, 所以在 Webpack 構建方案中,不建議使用 typeof 方式判斷。而是使用 Webpack 提供的 webpack.DefinePlugin 插件定義常量解決。
clientHeight() {
   return typeof document === 'object' ? document.body.clientHeight : '';
}

  

  • 使用 Webpack 提供的 webpack.DefinePlugin 插件定義常量解決。 這里直接使用 easywebpack 內置的全局 Webpack 常量 EASY_ENV_IS_BROWSER 進行判斷。 這樣在構建壓縮期間, 如果是 Node 模式構建, EASY_ENV_IS_BROWSER 會被替換為 false,如果是 Browser 模式構建, EASY_ENV_IS_BROWSER 會被替換為 true,最后構建后代碼也就是變成了 true 或者 false 的常量。 因為這個是構建期間執行的,壓縮插件剔除永遠不會被執行的代碼, 也就是 dead_code
clientHeight() {
   return EASY_ENV_IS_BROWSER ? document.body.clientHeight : '';
}

  

 

NPM Vue 組件 SSR 支持

針對上面這種自己寫的代碼,我們可以通過這種方式解決,因為可以直接修改。但如果我們引入的一個 npm Vue 插件想進行SSR渲染, 但這個插件里面使用了 window/docment 等瀏覽器對象, 並沒有對 SSR 模式進行兼容,這個時候該如何解決呢?

一般我們通過 通過 v-if 來決定是否渲染該組件 和 Vue 只在前端掛載組件解決問題 可以解決。


通過 v-if 來決定是否渲染該組件

<template>
  <div v-if="isBrowser">
    <Loading></Loading>
  </div>
</template>

<script type="text/babel">
  export default {
    componets:{
     Loading: () =>import('vue-loading');
    }
    data(){
      return {
        isBrowser: EASY_ENV_IS_BROWSER
      }
    },
    mounted(){
    }
  }
</script>

  


Vue 只在前端掛載組件解決問題

<template>
  <div>
    <Loading></Loading>
  </div>
</template>

<script type="text/babel">
  export default {
    data(){
      return {
      }
    },
    beforeMount() {
      // 只會在瀏覽器執行  
      this.$options.components.Loading = () =>import('vue-loading');
    },
    mounted(){
    }
  }
</script>

  

loading 組件因為沒有注冊, 在 SSR 模式, <Loading></Loading> 會被原樣輸出到 HTML 中,不會報錯且不能被瀏覽器識別, 在顯示時不會有內容。當 SSR 直出 HTML 后,瀏覽器模式中執行 beforeMount 掛載組件, 從而達到解決服務端渲染報錯的問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM