Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

nginx - django-compressor writing new files in collect_static/CACHE on every request

I've a django website set up using django-compressor + memcached. Not sure when it started, but I'm finding new css and js files in .../collect_static/CACHE/css and .../collect_static/CACHE/js every minute, like output.2fde5b60eff0.css.

I use django.contrib.staticfiles.storage.ManifestStaticFilesStorage.

I have no clue if this is normal, or happening because of some misconfiguration. But in every few days, I need to clean the server because of this.

Any suggestions what is going on here?

Update: It seems to be happening because of template variables inside css and js code, as per this answer, but as I've a lot of such variables, I still don't know how to fix this.

question from:https://stackoverflow.com/questions/66067291/django-compressor-writing-new-files-in-collect-static-cache-on-every-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, so I found the underlying reason. It is not actually the presence of template variables like {{context_data_var}} within compressed code.

It is the presence of any such variables the values of which change on each request. I had two such instances:

  1. Storage keys for the third party storage service (such as Google or Amazon)
  2. csrf tokens used in various ajax requests

For 1. above, I simply moved such code outside compress.

For 2., the solution is slightly involved. I had to move away from using {{csrf_token}}. Django explains it in detail here. We need to use the csrftoken cookie instead of the variable {{csrf_token}}, and django sets this cookie if there is at least one {% csrf_token %} in the template. I had one luckily in my base template, so the cookie was already getting set for me. I also had the getCookie() function defined for all pages.

Thus, I was able to get rid of the issue explained in my question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...