Search code examples
pythondjangostatic-files

Django WhiteNoise collectstatic repeatedly post-processes CSS files


I am successfully using WhiteNoise in my Django project.

My problem is when running the collectstatic command. WhiteNoise post-processes all my .css files, even when they have not changed. It does not unnecessarily post-process .js or .png files, only .css files.

This seems like a bug in WhiteNoise. Has anyone else seen this behaviour?

An example:

The first time I run collectstatic, all of the files are copied by django and post-processed by WhiteNoise:

Copying '<path...>.svg'
Copying '<path...>.js'
Copying '<path...>.css'
Copying '<path...>.txt'
... etc ...
Post-processed '<path...>.svg'
Post-processed '<path...>.js'
Post-processed '<path...>.css'
Post-processed '<path...>.txt'
... etc ...

77 static files copied to 'C:\<path...>\staticfiles', 77 post-processed.

This is all working correctly.

But if I immediately run collectstatic again (without modifying any files), WhiteNoise post-processes the .css files again:

Post-processed '<path...>.css'
Post-processed '<path...>.css'
... etc ...

0 static files copied to 'C:\<path...>\staticfiles', 77 unmodified, 13 post-processed.

This is an issue for me because I am considering using WhiteNoise in development as well as in production, to minimise the differences between my development and production environments. Waiting for WhiteNoise to post-process every .css file in the project (including any libraries) whenever I change any file is clearly too much to ask for a development environment.

This feels like a bug in WhiteNoise because it correctly notices that the .js and .svg files have not changed, but not the .css files. Has anyone else seen this behaviour?


Solution

  • This is a feature of Django, rather than a bug in WhiteNoise: CSS files can contain references to other static files (usually images) and the cache-busting mechanism causes the names of these image files to change whenever their contents change. So the processed output of the CSS file can change even if the original CSS file hasn't, just because one of the images it refers to has changed its content. This is why Django reprocesses the CSS files every time.

    You don't need to run collectstatic in development to pick up changes to your files however. WhiteNoise follows the standard Django behaviour of serving your unprocessed files directly when the DEBUG setting is True.