Firebase 模拟器 CDN 缓存

问题描述 投票:0回答:1

如何配置 Firebase 模拟器(托管)以遵守来自 Firebase 函数的响应时设置的缓存控制标头?

示例(Python)

from firebase_functions import https_fn
from firebase_admin import initialize_app

initialize_app()

HTTPS_CACHING_HEADER = {'Cache-Control': 'public, max-age=300, s-maxage=300'}


@https_fn.on_request(region='europe-north1')
def foo(req: https_fn.Request) -> https_fn.Response:
  print("**function was executed**")
  return https_fn.Response(headers=HTTPS_CACHING_HEADER, response="some content")

使用给定的配置(firebase.json)

{
  "functions": [
    {
      "source": "functions",
      "codebase": "default",
      "ignore": [
        "venv",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ]
    }
  ],
  "emulators": {
    "functions": {
      "port": 5001
    },
    "hosting": {
      "port": 8888
    },
    "ui": {
      "enabled": true
    },
    "singleProjectMode": true
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/foo",
        "function": {
          "functionId": "foo",
          "region": "europe-north1",
          "pinTag": true
        }
      }
    ]
  }
}

是什么让我觉得这不起作用?

多次使用以下请求

curl

$ curl http://127.0.0.1:8888/foo -v                                                                                                                    7 ↵
*   Trying 127.0.0.1:8888...
* Connected to 127.0.0.1 (127.0.0.1) port 8888
> GET /foo HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/1.1 200 OK
< x-powered-by: Express
< server: gunicorn
< date: Sun, 25 Feb 2024 18:57:37 GMT
< connection: keep-alive
< cache-control: public, max-age=300, s-maxage=300
< content-type: text/html; charset=utf-8
< content-length: 4507
< vary: Accept-Encoding, Authorization, Cookie

产生以下日志;对托管端点的后续调用将触发该函数,我希望当响应中返回缓存控制标头时不会触发该函数。

i  hosting: 127.0.0.1 - - [25/Feb/2024:18:47:59 +0000] "GET /foo HTTP/1.1" 200 4507 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
[hosting] Rewriting /foo to http://127.0.0.1:5001/project-x123/europe-north1/foo for local Function europe-north1/foo
i  functions: Beginning execution of "europe-north1-foo"
>  **function was executed**
i  functions: Finished "europe-north1-foo" in 28.365847ms

i  hosting: 127.0.0.1 - - [25/Feb/2024:18:48:03 +0000] "GET /foo HTTP/1.1" 200 4507 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
[hosting] Rewriting /foo to http://127.0.0.1:5001/project-x123/europe-north1/foo for local Function europe-north1/foo
i  functions: Beginning execution of "europe-north1-foo"
>  **function was executed**
i  functions: Finished "europe-north1-foo" in 11.306171ms

我预计 Firebase 托管会遵守缓存控制标头,并且在使用模拟器时不会调用 Firebase 云函数。

firebase google-cloud-functions firebase-hosting firebase-tools
1个回答
0
投票

如何配置 Firebase 模拟器(托管)以遵守来自 Firebase 函数的响应时设置的缓存控制标头?

事实上,确实如此。您可以在问题中显示的响应中看到标题。标头肯定会发送到客户端。

问题是模拟器实际上并没有附加一个 CDN 来记住响应并从缓存中提供服务。对于本地模拟器来说这太过分了。

另请记住,Firebase 托管的生产 CDN 实际上是 Fastly,如文档中所述。

如果您认为添加模拟 CDN 值得添加(不过,这可能不值得 Firebase 付出努力),请在 GitHub 上针对模拟器提出问题,描述不匹配的内容。

© www.soinside.com 2019 - 2024. All rights reserved.