I have recently implemented SSR with Cloud Functions and Firebase Hosting.
When the JS bundle is built it receives a cache bursting suffix (main.1.js
).
Inside my function I have the following piece of code for caching the results of the Cloud Function
res.set('Cache-Control', 'public, max-age=300, s-maxage=300');
During deploy, I deploy hosting first and then cloud function
firebase deploy --only hosting:production && gcloud functions deploy ssr --runtime nodejs8 --trigger-http --source dist/server
The firebase hosting deployment replaces main.1.js
with main.2.js
.
Due to the cache bursting the file is now different (main.2.js
) but because the cloud function is cached for another 5 minutes - I get errors when I visit the website (because main.1.js
which is referenced in the cached version of the function, is no longer available).
How would you fix such an issue? Can I have two active deployments and activate one after another?
As Frank suggested, you should definitely add the . firebase directory to your . gitignore or equivalent file, since it contains information that's not strictly part of your project, and is likely not applicable for everyone sharing and contributing to your project source code.
Firebase Hosting uses a powerful global CDN to make your site as fast as possible. Any requested static content is automatically cached on the CDN. If you redeploy your site's content, Firebase Hosting automatically clears all your cached static content across the CDN until the next request.
The cache control header public, max-age=300, s-maxage=300
tells any party handling a request (mainly the users's browser and Google's CDN server but can also be e.g. a proxy the user is using) how to cache the request. With your configuration both will cache the file for 5 minutes. You cannot change this behavior as there is no way to invalidate the CDN server's cache and the browser does not know about your deployment either and even if it would get a notification and reload it would get the same outdated file from the CDN.
I don't fully understand your use case but here are possible solutions:
main.x.js
for at least the cache duration. You can use Cloud Storage to upload the file on deployment.main.1.js
gives a 404, increment the number and try main.2.js
main.js
main.js
to the cloud function's response body. By doing so, you ensure that the cloud functions response and the content of main.x.js
get cached together and reloaded togetherIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With