Using JavaScript to invalidate browser cache

I am doing a project in Django and using djangos-css ( http://github.com/dziegler/django-css ) and Sass ( http://sass-lang.com/ ). Sass files are used in development using django-css. I want to write a JavaScript procedure that will retrieve CSS assets every two seconds. The purpose of this is so that the designer can edit the Sass files, delete the save, and immediately see the result in the browser without switching applications or updating updates.

Basically, I need a way for JavaScript to get the browser to reload certain files without refreshing the page. Is it possible?

+3
source share
2 answers

The easiest way is usually to add a unique url to the url, I often use timestamp

var timestamp = (new Date()).getTime();
url += '?time=' + timestamp;

Just be careful if your requests already have parameters, then you need to add &time=' + timstamp.

The browser cannot cache the request because each request looks unique.

+1
source

Not all caches will cache content using the query string.

Steve Souders recommends: "... avoid requesting cached resources." He found that in this case 5-20% of requests would not be cached. Query strings, in particular, do not work at all with some CDNs for cache invalidation.

The best way is to create MD5 with a javascript file name for each release, for example.

{path to script file}/filename-{md5 hash}.js

+2

Source: https://habr.com/ru/post/1730696/


All Articles