Search code examples
javascriptdjangominimize

Minimize JS at runtime or generate static?


My question could be perceived opinion based, but I am more needing for advantages and incovenients of both way to minimize JS script in a django app.

Is it better to minimize JS scripts at rendering time with python library such SlimIt or Django-pipeline to minimize all app's scripts and save them in files?

What strategy django experts are using? Is there some cases where one or other strategy is more adapted?

Other question: Can JS scripts with template tags be minimized correctly?


Solution

  • In my experience it doesn't matter all that much. This is down to the fact that both methods behave in a very similar way, as minification at runtime caches the results. Either in the file system in your static directory or in a cache such as memcached.

    One downside to doing this at runtime though is that the first request will be slower, as the file or cache hasn't been populated yet. And the downside to generating everything manually is exactly that, you have to generate everything manually when you make changes.

    My workflow involves having all static files generated at runtime during development and having them all minified and saved during production. Django pipeline does a lot of this out of the box.