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