1

I'm managing static files (JS, CSS, images, etc) in my Django application using staticfiles. This works fine, but I'd like to start dynamically serving pre-compressed sources when the user's browser is capable.

I went through the linked tutorial and, in production (on Apache) this works fine. I can include files using

<script src="/static/js/my-site"></script>

and it will load my-site.js in older browsers and my-site.js.gz when GZip encoding is supported. Great! But: this breaks local development using runserver. Of course, the staticfiles default view has no idea how to turn /js/my-site into /js/my-site.js (or .gz). To get runserver working, I need to specify the extension, which breaks content negotiation.

Is there a better way to configure Apache, so that I can always request .js (or .css, etc) and get served the compressed version transparently? Or can I tell Django how to find the requested resource without specifying an extension? I wouldn't think I'm the only one trying to do this...

Coderer
  • 25,844
  • 28
  • 99
  • 154
  • For reference, I'm trying [an alternative solution](http://stackoverflow.com/questions/9076752/how-to-force-apache-to-use-manually-pre-compressed-gz-file-of-css-and-js-files) using `mod_rewrite` but for the life of me I can't get it to actually serve the gzip'd files. Maybe I need a separate question about `mod_rewrite` debugging? – Coderer Jun 02 '16 at 10:14

1 Answers1

1

there is no simple resolution. mostly because you are using something that was designed for the apache web server only (afaik).

i my opinion there are 3 solutions:

  1. keep source .{js,css} files in separate directory, in development you can serve them from source dir or compressed one - simple, transparent and you can hide your uncompressed and non-obfuscated sources far from the reach
  2. compress files with the .min.{js,css} ending - no need for separate directory, you can hide sources in apache (mod_rewrite)
  3. write your own small middleware that will be simulating what apache does (it is few lines to select and rewrite path, you can even have different behavior depending on DEBUG config var)
  4. use some dynamic solution e.g Django Compressor which will compile those files on-demand

(I'm using option 4 :) )

Jerzyk
  • 3,662
  • 23
  • 40
  • Some of these don't fully address the issue. I need *something* in the HTML that will load uncompressed scripts from browsers that don't support gzip encoding, or the compressed versions if possible. The example in the OP works great for that, when served from Apache. #1 / #4 don't provide this. #3 could work but learning to write middleware isn't a priority right now. For #2, as I commented above I'm having trouble with mod_rewrite, but it's probably the most promising path in the long run. – Coderer Jun 08 '16 at 08:30
  • imo if browser doesn't support compression - you do not have headers in the request, so you should not force gzipped versions on them – Jerzyk Jun 08 '16 at 11:54
  • Exactly -- that's why #1 and #4 aren't a complete solution, you still need something to examine the headers and decide which version to serve, even though the HTML is only asking for one named resource. – Coderer Jun 08 '16 at 11:58