Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase hosting - force browser to reset cache on new deploys?

I have a site built with create-react-app and hosted on Firebase Hosting. What can I do to specify the browser cache needs to be updated after new deploys, and ideally only for pages, assets, and stylesheets that have been changed since the last deploy?

Is there a way to access the deploy id and include that (or any other unique identifier) in the headers so the browser can compare to what it has in local storage or cache and determine whether a hard refresh is necessary? I looked over the Firebase Deploying as well as Full config docs but there's no mention on how to access hosting metadata like the last deploy time.

Setting a Cache-Control value to max-age=[any number] doesn't seem ideal because it disregards when the next deployment will occur (which might be unknown to begin with unless there are regular schedules).

like image 812
Pat Needham Avatar asked Oct 10 '17 12:10

Pat Needham


People also ask

Is firebase hosting Slow?

We have found Firebase Hosting to be crazy slow on the first load after a new deployment. It is in the range of 3-25seconds to load a single asset, this can push our loading times very high after a deployment for our first user in each CDN region.

Does firebase hosting use CDN?

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.

Should I add .firebase to Gitignore?

gitignore ? stackoverflow.com/a/52131191: Quote, "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."


2 Answers

I figured it out. Had to update the firebase.json file according to this Github comment:

{   "hosting": {     "headers": [       { "source":"/service-worker.js", "headers": [{"key": "Cache-Control", "value": "no-cache"}] }     ]   } } 
like image 134
Pat Needham Avatar answered Sep 21 '22 14:09

Pat Needham


Please set "max-age=0" in your firebase.json.

    "headers": [         {             "source": "/service-worker.js",             "headers": [                 {                     "key": "Cache-Control",                     "value": "no-cache, no-store, must-revalidate"                 }             ]         },         {             "source": "**/*.@(jpg|jpeg|gif|png|svg|webp|js|css|eot|otf|ttf|ttc|woff|woff2|font.css)",             "headers": [                 {                     "key": "Cache-Control",                      "value": "max-age=0"                 }             ]         }     ] 
like image 45
Kei Avatar answered Sep 22 '22 14:09

Kei