简体   繁体   English

Nuxt.js SSR 与 firebase - ReferenceError:导航器未定义

[英]Nuxt.js SSR with firebase - ReferenceError: navigator is not defined

I've made a simple web app with Nuxt(SSR) and firebase(firestore and hosting).我用 Nuxt(SSR)和 firebase(firestore 和托管)制作了一个简单的 web 应用程序。 It has some pages like index, about, contact and text editor.它有一些页面,如索引、关于、联系和文本编辑器。
When I do npm run build , it works with no problem.当我执行npm run build时,它可以正常工作。 But for npm run generate , it has an error message like:但是对于npm run generate ,它有如下错误信息:

ReferenceError: navigator is not defined
at Object.areCookiesEnabled (C:\app\node_modules\@firebase\util\dist\index.node.cjs.js:656:5)
at warnOnBrowserContextMismatch (C:\app\node_modules\@firebase\analytics\dist\index.cjs.js:1072:15)
at factory (C:\app\node_modules\@firebase\analytics\dist\index.cjs.js:1086:5)
at Component.instance.INTERNAL.registerComponent.component.Component.setServiceProps.settings [as instanceFactory] (C:\app\node_modules\@firebase\analytics\dist\index.cjs.js:1165:16)
at Provider.getOrInitializeService (C:\app\node_modules\@firebase\component\dist\index.cjs.js:222:39)
at Provider.getImmediate (C:\app\node_modules\@firebase\component\dist\index.cjs.js:120:33)
at FirebaseAppImpl._getService (C:\app\node_modules\@firebase\app\dist\index.node.cjs.js:230:49)
at FirebaseAppImpl.firebaseAppImpl.<computed> [as analytics] (C:\app\node_modules\@firebase\app\dist\index.node.cjs.js:442:39)
at Object.serviceNamespace [as analytics] (C:\app\node_modules\@firebase\app\dist\index.node.cjs.js:422:45)
at Object.<anonymous> (plugins/firebase.js:22:0)
at __webpack_require__ (webpack/bootstrap:25:0)
at Module.<anonymous> (server.js:2766:16)
at __webpack_require__ (webpack/bootstrap:25:0)
at Object.<anonymous> (server.js:639:18)
at __webpack_require__ (webpack/bootstrap:25:0)
at server.js:118:18

This message actually has been showing up occasionally when I run the app in dev mode and it usually disappeared when I refresh.当我在开发模式下运行应用程序时,这个消息实际上偶尔会出现,当我刷新时它通常会消失。
Here's what I did to fix it:这是我为修复它所做的:

  1. Delete the node_module folder and the dependencies that I don't use eventually in package.json file and npm install again.删除我在 package.json 文件中最终不使用的 node_module 文件夹和依赖项,然后再次npm install I even deleted the firebase-related files and folder and re-install firebase tools and init again.我什至删除了与firebase相关的文件和文件夹并重新安装firebase工具并再次初始化。
  2. At first the error message showed up only for /about page and any other pages are alright so I deleted the about page - and then the error shows up again for another page.起初,错误消息只出现在 /about 页面上,其他页面都没有问题,所以我删除了 about 页面 - 然后错误信息再次出现在另一个页面上。
  3. Before #2, I thought this error occurs because only about page has no.在#2之前,我认为会发生此错误是因为只有关于页面没有。 I did some research and figured out that it's because the page is client-side only or server-side only blah blah...so I added some script code on purpose but it didn't work too.我做了一些研究,发现这是因为该页面仅是客户端或服务器端等等等等……所以我故意添加了一些脚本代码,但它也不起作用。

I looked up dozens of articles on here or other forums, and none of it actually helps to figure out what I'm doing wrong.我在这里或其他论坛上查找了数十篇文章,但实际上并没有帮助找出我做错了什么。
This problem seems related to node or server-side things, but I'm quite new to Vue and also Node, so I couldn't fully understand what I have to do and where I should modify the code.这个问题似乎与节点或服务器端的事情有关,但我对 Vue 和 Node 都很陌生,所以我无法完全理解我必须做什么以及应该在哪里修改代码。
BTW my node is the latest LTS version and vue, nuxt, and all the dependencies are the latest versions as well.顺便说一句,我的节点是最新的 LTS 版本,而 vue、nuxt 和所有依赖项也是最新版本。
Thanks in advance.提前致谢。

Hey dont know if this helps, I ran into the same issue using NextJs.嘿不知道这是否有帮助,我使用 NextJs 遇到了同样的问题。 Here is how I solved it.这是我解决它的方法。 I wasnt using it in any of my SSR code either though so idk what happened.我也没有在我的任何 SSR 代码中使用它,但我不知道发生了什么。

const analytics = (): firebase.analytics.Analytics => firebase.analytics();

Then in your code to use it然后在你的代码中使用它

analytics().logEvent();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM