【发布时间】:2020-05-23 17:52:38
【问题描述】:
我不确定这是否可能。我正在向外部 api 发出请求,并且在来自我的应用程序的每个请求中,我将用户的查询传递到 Cloud Function (Typescript) 中的自定义 'query' 标头中。 ⤵
export const searchQuery = functions.https.onRequest(async (request, response) => {
// : Reads request query data from user
const query = request.headers.query;
...
我尝试设置缓存,以便在每个查询中单独缓存结果,但它似乎不起作用。 ~
(缓存确实可以在没有设置不同标头的情况下工作,但它只缓存第一个搜索结果)
(所有请求都以GET 发送)
这是将 'query' 标头设置为缓存的可变规则的块(用 Typescript 编写)。 ⤵
...
return await admin.auth().verifyIdToken(tokenId) // : Authenticates response
.then(() => {
// : Set cache-control
console.log(request.headers);
response.set('Vary', 'Accept-Encoding, query');
response.set("Cache-Control", "public, s-maxage=600");
response.set("Access-Control-Allow-Origin", "*");
response.set("Access-Control-Allow-Methods", "GET");
// : Grab API search data
axio.get(urlAssembler).then(APIData => {
response.status(200).send(APIData.data);
}).catch(error => console.log(error));
})
.catch((err) => response.status(401).send(err));
...
我在 Cloud Functions w/ Firebase 而不是 Cloud Functions with Firebase Hosting 上进行了此设置。我想知道那里是否有区别,但似乎没有。
在我的firebase.json 中,我注意到它是为 Cloud Functions 而不是为 Firebase 托管设置的。也许我需要将其设置为 Firebase Hosting 来为那里的标头定义缓存控制?
这是我的 firebase.json ⤵
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
}
【问题讨论】:
标签: firebase google-cloud-functions firebase-hosting