首页 > 解决方案 > Angular 8 服务器端渲染不适用于 Firebase

问题描述

我正在使用 Angular Universal 建立一个 Angular 8 项目,以使我的项目对 SEO 更友好。我的实际阻碍是当我想运行时npm run server:ssr抛出与@firebase/app package.

我在 Windows 10 机器 Angular 8 和 Firebase ^6.2.0 上运行该项目

angular.json 文件:

          "builder": "@angular-devkit/build-angular:server",
          "options": {
            "outputPath": "dist/server",
            "main": "src/main.server.ts",
            "tsConfig": "src/tsconfig.server.json"
          },

这是我收到的信息:

@firebase/app:
      Warning: This is a browser-targeted Firebase bundle but it appears it is being
      run in a Node environment.  If running in a Node environment, make sure you
      are using the bundle specified by the "main" field in package.json.

      If you are using Webpack, you can specify "main" as the first item in
      "resolve.mainFields":
      https://webpack.js.org/configuration/resolve/#resolvemainfields

      If using Rollup, use the rollup-plugin-node-resolve plugin and specify "main"
      as the first item in "mainFields", e.g. ['main', 'module'].
      https://github.com/rollup/rollup-plugin-node-resolve


标签: node.jsangularfirebaseangularfireserver-side-rendering

解决方案


您使用的 firebase 包仅适用于浏览器/客户端。困扰您的代码位于服务器端。

您需要导入(和设置)firebase-admin才能在服务器端( NodeJS )上工作。

如果您对客户端和服务器使用相同的代码,请确保使用以下标志分隔客户端代码:

if( process.env.NODE_ENV === 'development') { 
    // client-side only code ... 
}

如果您需要更多帮助,请告诉我!


推荐阅读