Reputation: 2371
I'm unsure if this is even possible. I'm making requests to an external api and on each request from my app I pass the user's query into a custom 'query'
header in the Cloud Function (Typescript). ⤵
export const searchQuery = functions.https.onRequest(async (request, response) => {
// : Reads request query data from user
const query = request.headers.query;
...
I attempted to setup caching so that with each query it caches the result separately but it doesn't seem to be working. ~
( caching does work without vary header set however it's only caching the first search result )
( all requests are sent as GET
)
This is the block that sets the 'query'
header as a vary rule for caching (written in 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));
...
I have this setup on Cloud Functions w/ Firebase and not Cloud Functions with Firebase Hosting. I'm wondering if maybe there's a difference there but it seems there isn't.
In my firebase.json
I noticed that it is setup for Cloud Functions and not for Firebase Hosting. Perhaps I need to set it up as Firebase Hosting to define cache-control for headers there?
This is my firebase.json ⤵
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
}
Upvotes: 3
Views: 1042
Reputation: 2371
I contacted Firebase support regarding this and the individual helping me said that Firebase Hosting needs to be used in order to cache based on a header like described above.
I have not tested this yet so once I test it I will update this answer to confirm that it works.
There is an article suggested to me by the individual helping me from Firebase support for how to cache within a Cloud Function but I don't believe this makes use of a CDN.
Upvotes: 1