vue ssr error: TypeError: Cannot read property 'replace' of undefined


在開發vue ssr應用時, yarn build  yarn start 之后啟動正常:

info Server running at: http://0.0.0.0:6606

在訪問頁面時,發現頁面降級到SPA,服務端報錯:

debug Server rendering encountered an error: { TypeError: Cannot read property 'replace' of undefined }

 

經過排查,錯誤出在 vue-server-renderer(v2.6.11) 中:

9081 TemplateRenderer.prototype.getUsedAsyncFiles = function getUsedAsyncFiles (context) {
9082   if (!context._mappedFiles && context._registeredComponents && this.mapFiles) {
9083     var registered = Array.from(context._registeredComponents);
9084     context._mappedFiles = this.mapFiles(registered).map(normalizeFile);
9085   }
9086   return context._mappedFiles
9087 };

原因是 this.mapFiles(registered) 中沒有匹配到css的sourcemap文件

修改源碼過濾掉 undefined 可以解決報錯(錯誤示范)

可以修改 vue.config.js 配置文件(項目由vue cli3/4創建):

module.exports = {
+  css: {
+    extract: true,
+    sourceMap: true
+  }
};

為css啟用sourcemap就行了,問題解決。記得重新build


免責聲明!

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



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