angular - 路由超时。 Angular 9 通用 + Firebase

标签 angular firebase google-cloud-functions angular-universal

我正在尝试设置 Angular 9 Universal + Firebase 应用程序。我已将通用包添加到我的项目和 Firebase 云功能中以支持 SSR。这是我的云函数的代码:

import * as functions from 'firebase-functions';
import * as path from 'path';

const universal = require(path.resolve(__dirname, '../dist/server/main')).app;

export const ssr = functions.runWith({ memory: "2GB", timeoutSeconds: 120 }).https.onRequest(universal);

我的 Firebase 配置:

{
  "hosting": {
    "public": "dist/browser",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "ssr"
      }
    ]
  }
}

构建通过且没有错误,应用程序似乎工作正常但路由不起作用。主路线 (/) 加载良好,我可以毫无错误地导航到其他路线,但如果我尝试在新选项卡中打开任何其他路线(例如 /books)或重新加载已经打开的路线我会得到超时异常。这是屏幕截图: error

我不知道为什么会出现这个错误。我已经尝试将超时时间增加到 120 秒,但没有帮助。任何建议我该如何解决?

最佳答案

如果您使用的是 angular universal 的脚手架版本并且您没有更改 server.ts 文件中的任何内容,则必须将调用通用函数的结果传递给 onRequest 函数。

所以,你必须改变这个:

const universal = require(path.resolve(__dirname, '../dist/server/main')).app;

export const ssr = functions.runWith({ memory: "2GB", timeoutSeconds: 120 }).https.onRequest(universal);

为此(你必须调用通用函数):

const universal = require(path.resolve(__dirname, '../dist/server/main')).app;

export const ssr = functions.runWith({ memory: "2GB", timeoutSeconds: 120 }).https.onRequest(universal());

为什么这是必要的?您的 app 函数不接收任何参数,并返回一个 server 对象(实际上是一个 Express-application 对象)。 onRequest 函数接收一个匿名函数,该函数接收一个 request 和一个 response。因此将 app 函数传递给 onRequest 没有多大意义。

我昨天遇到了这个完全相同的问题,我真的不知道 Firebase 如何能够提供根路由 (/),而不是任何子路由。

无论如何,我希望这个答案能解决您的问题!

卢西亚诺。

关于angular - 路由超时。 Angular 9 通用 + Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61352383/

相关文章:

html - 在 Angular 中使用主机选择器

angular - Compodoc 可以生成 Markdown 文档,而不是其默认的 HTML 文档吗?

firebase - Firestore可以使用一个查询更新多个符合条件的文档吗?

angular - 扩展 BaseRequestOptions 时未定义注入(inject)的依赖项

firebase - 在 Firestore 中创建对 Cloud Storage 文档的引用

android - 处理 Android 系统托盘中已关闭的 Firebase 通知

node.js - Firebase Cloud Functions HTTPS 中的参数

node.js - Firebase 的云功能 - 已验证电子邮件操作

firebase - 云函数错误 : cannot convert an array value in an array value

angular - 在 S3 中存储和查看 Base64 图像