Stop saving in cache memory Flutter web Firebase hosting

4,168

Solution 1

Append a version number to your main.dart file. Every time you do changes that require the browser to clear cash, change the appended version number before you upload changes so the browser knows something has change and it needs to clear the cash. For example:

First Version:

<script src="main.dart.js?version=1" type="application/javascript"></script>

Next time you make changes:

<script src="main.dart.js?version=2" type="application/javascript"></script>

And so on. Check this link.

Solution 2

Another approach would be to prevent caching via HTTP headers. For Firebase Hosting that would be : https://firebase.google.com/docs/hosting/full-config#headers

I guess you could set up something like this inside the site entry in your firebase.json:

"headers": [
  {
    "source": "**",
    "headers": [
      {
        "key": "Cache-Control",
        "value": "no-cache"
      }
    ]
  }
]
Share:
4,168
Faslur Rajah
Author by

Faslur Rajah

Updated on December 16, 2022

Comments

  • Faslur Rajah
    Faslur Rajah over 1 year

    I designed a web app using the Flutter web. I hosted it with Firebase host. But It's not showing changes of my code after deploying to firebase. It's still showing my older version of web app. But I can overcome this problem by clearing cache memory of browser or ctrl+refresh. But I don't want to do this every time I deploy to firebase. How to stop saving my web app in cache memory?

    hosted link: https://frcovid19dashboard.web.app

  • Gustavo Contreiras
    Gustavo Contreiras almost 4 years
    This is not working for me. Is there another approach?
  • iqfareez
    iqfareez almost 3 years
    @GustavoContreiras Try stackoverflow.com/a/65110613/13617136
  • Manish
    Manish almost 3 years
    for those who are wondering where to add it, It took quite a minute too xD, add this into your firebase.json file in your fluutter project. Thanks @klydra
  • klydra
    klydra almost 3 years
    Added it to the post. Glad it helped @ManishKumar :D
  • Frederic
    Frederic over 2 years
    This didn't work for me.
  • CKP78
    CKP78 over 2 years
    In light of the discussion here stackoverflow.com/questions/49547 it might be worth replacing the line "value": "no-cache" from this answer with "value": "no-cache, no-store, must-revalidate".
  • ali
    ali over 2 years
    where do i put this? because when i put this and deploy into server it will return error 404 since it cannot detect the js file urlname.com/main.dart.js?version =1
  • Prototype
    Prototype almost 2 years
    It is still not working for me