Better way to deploy compressed assets - Django Compressor

495 views Asked by At

So I have a tricky problem that I am not able find a better solution for.

I am using django-compressor which does a great job in compressing/minifying the files. The problem I am facing is during deployment. For some time, I have been just using compress tag on the production and I simply do a curl on all pages to make sure it regenerates the correct CSS/JS. So that way, new users don't have to feel the brunt of the compression process.

I know it has a management command that it can pre-compress the files, which then can be pushed to CDN and perhaps preload them behind the scenes on the landing/login pages.

Here is the problem, in some of the javascript code, i am using context vars and django tags [both custom and native].

var first = '{% some_tag some_context_var %}';
var name = '{{some_context_var}}';

This causes an error since during compression it tries to execute that tag which obviously doesn't have the values. It does come with COMPRESS_OFFLINE_CONTEXT but its not dynamic.

Did anyone encounter this issue before. How do you do your compression if the code is mixed in with some django tags/context variables?

1

There are 1 answers

0
Saikiran Yerram On

Found a way to resolve this.

Basically moved the variables into their own script blocks outside of the compressor and then use those variables inside the compression code. e.g.

 <script type="text/javascript">
    var some_var = '{% something ctx_var %}';
 </script>

 {% compress js %}
     //before it was do_something_function('{% something ctx_var %}') 
     do_something_function(some_var);