因此,我正在尝试针对Web应用程序使用Firebase托管和Google Cloud Run。
我在firebase托管上部署了一个静态SPA,并且有一个重写规则将所有以/ api开头的请求定向到我的Google Cloud Run服务。
我的firebase.json文件如下:
{
"hosting": {
"public": "dist","ignore": [
"firebase.json","**/.*","**/node_modules/**"
],"rewrites": [
{
"source": "**/api/**","run": {
"serviceId": "myservice-api","region": "europe-west1"
}
},{
"source": "**","destination": "/index.html"
}
]
}
}
起初,一切似乎都按预期工作,所有/ api请求均由我的Google Cloud Run服务处理。但是我开始注意到某些请求的异常行为,因为服务器返回的响应根本不是我期望的。 进一步查看它,我发现这些请求没有出现在我的Cloud Run服务的日志中。
因此,我检查了浏览器收到的响应标头,并注意到响应来自Firebase缓存而不是Cloud Run服务:
accept-ranges: bytes
access-control-allow-origin: *
content-length: 245
content-type: application/json
date: Wed,06 Nov 2019 11:24:32 GMT
server: Google Frontend
status: 200
vary: x-fh-requested-host,accept-encoding
x-cache: HIT
x-cache-hits: 5
x-served-by: cache-cdg20776-CDG
但是,由于Cloud Functions和Cloud Run服务动态生成内容,因此给定URL的内容可能会根据用户输入或用户身份等因素而有所不同。为了解决这个问题,默认情况下,后端代码处理的请求不会缓存在CDN上。
因此,我希望发送到/ api的请求都不会被缓存。 我定义重写规则的方式有问题吗?还是应该将其视为Firebase托管处理云运行路由的方式的问题?
更新
这是客户端请求的详细信息,如Chrome开发工具所示:
Request URL: https://myproject.web.app/api/users/me
Request Method: GET
Headers:
:authority: myproject.web.app
:method: GET
:path: /api/users/me
:scheme: https
accept: application/json;charset=UTF-8
accept-encoding: gzip,deflate,br
accept-language: en-US,en;q=0.9,fr;q=0.8
authorization: Bearer xxx
cookie: G_ENABLED_IDPS=google; G_AUTHUSER_H=0
referer: https://myproject.web.app/auth/login/
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/77.0.3865.120 Safari/537.36
请求返回JSON数据,这是由Cloud Run实际处理时后端返回的响应标头:
content-type: application/json
access-control-allow-origin: *
X-Cloud-Trace-Context: eeb4c0dbd22dd46f3c42fbe0b85b6420;o=1
Date: Wed,06 Nov 2019 11:24:26 GMT
Server: Google Frontend
Content-Length: 240
因此服务器没有设置任何特定的标头来指示应缓存响应。