gpt4 book ai didi

firebase - 是否可以独立于请求的 URL 在 Firebase 托管重写中缓存 Cloud Function 调用结果?

转载 作者:行者123 更新时间:2023-12-04 09:27:39 25 4
gpt4 key购买 nike

我正在将 Firebase 托管中的多个路径重写为始终返回相同结果的云函数。我需要调用一次该函数,缓存它的结果,然后将它返回给对这些路径中的任何一个的任何后续请求。然而,作为documentation states ,缓存内容基于:

  • The hostname
  • The path
  • The query string
  • The content of the request headers specified in the Vary header

因此,如果请求不同的 URL,将再次调用该函数。但是有没有办法避免这种情况?设置 Cache-Control header 确实会在请求相同的 URL 时阻止函数再次调用,但在不同的 URL 时则不会。

这是我的托管和功能配置: firebase.json :
{
"hosting": {
"rewrites": [
{
"source": "**",
"function": "myFunction"
}
]
}
}
functions/index.ts :
import * as functions from "firebase-functions"

export const myFunction = functions.https.onRequest((req, res) => {
res.set("Cache-Control", "public, max-age=31536000")
res.send("This is a Cloud Function.")
})

最佳答案

Firebase 托管(和网络浏览器)的缓存行为始终取决于 URL 路径。
有几种方法可以尝试解决此问题,具体取决于您的目标:

  • 使用重定向(例如,到 / )而不是重写。然后,云功能只能在已知(和可缓存)路径上提供内容。
  • 在每个路径上提供静态页面而不是云函数,然后让该静态页面使用 javascript 在已知的缓存路径上调用您的云函数。初始静态页面不会被缓存,但它应该比函数更快。
  • 关于firebase - 是否可以独立于请求的 URL 在 Firebase 托管重写中缓存 Cloud Function 调用结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62950265/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com