Z
Z
zxmd2014-02-14 15:16:58
JavaScript
zxmd, 2014-02-14 15:16:58

How to implement the organization of the return of static content, taking into account versioning?

There is a project on django 1.4.3 in which there is a lot of static content (js, css, icons and other shalupon).
Now at release, the STATIC_URL path is changed to flush the entire cache in users' browsers. For example
current release: STATIC_URL = '/assets-01/'
in the following STATIC_URL = '/assets-02/'
and so on. It is clear that at the nginx level, this is all resolved and alias is made to the folder with statics.
And all this would be good, if not for one BUT. We have fairly frequent releases and hotfixes, sometimes several times a day. When changing the next change of STATIC_URL (if the release was related to the frontend), all users lose their cache, which:
a. Bad for the server
b. Bad for the client
. Not right at all
It is clear that you need to somehow throw off the cache only for the changed files, but I can’t figure out how to do it. Here we must take into account that the project is large and time is short. So some global changes are not suitable. We need some kind of comfortable crutch-bicycle option.
Yes, I'd love to hear how to do it the right way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery, 2014-02-14
@zxmd

Crutches do not need to be fenced. Django already has everything. For versions from 1.4.x to 1.6.x, the CachedStaticFilesStorage static store is provided specifically for this . How to connect - according to the link everything is described. And the meaning, in short, is this:
The collectstatic command creates copies of your static files with a hash added to the file names (the hash is calculated from the content of the file itself). Thus, if the content of the file changes, then its name also changes, so the browser is forced to receive a new static file from your server. If the file has not changed, we pull from the browser cache, as usual. If you use the static
template tag in the project itself(as it should be), and not the STATIC_URL variable, then you should not have problems connecting this static store.
Yes, there is one moment. In the newly created static files, all existing links to other statics (for example, there is a link to an image in the css file) are also changed. If you have broken links to statics in your files, collectstatic will fail with an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question