Amos P
Amos P

Reputation: 45

Compressed CSS files generated by django-compressor/libsass are not served on the first launch of the server, but are served on subsequent launches

I'm working on a Django app for the first time and I had reached a stage where I was feeling comfortable with the app's functioning in the development environment. To deploy I had to set DEBUG=False which brought some challenges. I learnt that the production servers won't serve static files the same way the development server does. I learnt the use of the py manage.py collectstatic command. I set up WhiteNoise, and upto this point I had no issues running the app in the development server with DEBUG=False. The last hurdle that remained was to serve scss files, as css. For this I used the django-libsass module. I setup the django-compressor, adding 'compressor' to list of INSTALLED_APPS, setting STATICFILES_FINDERS and COMPRESS_PRECOMPILERS as follows:

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
]

COMPRESS_PRECOMPILERS = (
    ('text/x-scss', 'django_libsass.SassCompiler'),
)

Finally, I updated the template with the required {% compress css %} tags. On running collectstatic I notice that the files are generated in the appropriate folder /static/CACHE/css/output.RANDOM_NUM.css. This is where the odd issue happens. The first time I run the development server, the CSS file isn't served:

WARNING - Not Found: /static/CACHE/css/output.RANDOM_NUMBER.css

The request returns a 404. This is despite me being able to verify that that file is indeed available at that path. I then shut down the server and relaunch it, and this time the page is rendered accurately and the file is served. Can anyone tell me why this is happening..?

Also, this is not an issue with the other static files which are not impacted by the django-compressor/django-libsass modules. They are served in the first launch of the server itself.

This is not a one time issue. I repeat the process again and again. 1. I delete the 'static' folder and its contents. 2. I run py manage.py collectstatic, and 3. I run py manage.py runserver. CSS not served. 4. ctrl + c and run py manage.py runserver. CSS is served. The issue repeats itself

Another thing to note is that, running py manage.py runserver --insecure does not face this issue, though I guess that is a completely different mechanism.

I'm wondering if I should ignore this, or not. The server always serves the CSS files in the second run. But the persistent nature of the issue makes me wonder if I should sort this out before deploying the app. I plan to deploy on Heroku.

Upvotes: 2

Views: 403

Answers (0)

Related Questions