Skip to content
Snippets Groups Projects
Commit 2b85a438 authored by Antoine Lambert's avatar Antoine Lambert
Browse files

swh-web: serve compressed assets in development mode

parent 499b1c8e
No related branches found
Tags v0.0.124
No related merge requests found
......@@ -2,16 +2,16 @@ run-django-webpack-devserver:
bash -c "trap 'trap - SIGINT SIGTERM ERR; kill %1' SIGINT SIGTERM ERR; npm run start-dev & cd swh/web && python3 manage.py runserver"
run-django-webpack-dev:
npm run build-dev && cd swh/web && python3 manage.py runserver
npm run build-dev && cd swh/web && python3 manage.py runserver --nostatic
run-django-webpack-prod:
npm run build && cd swh/web && python3 manage.py runserver --settings=swh.web.settings.production
npm run build && cd swh/web && python3 manage.py runserver --nostatic --settings=swh.web.settings.production
run-django-server-dev:
cd swh/web && python3 manage.py runserver
cd swh/web && python3 manage.py runserver --nostatic
run-django-server-prod:
cd swh/web && python3 manage.py runserver --settings=swh.web.settings.production
cd swh/web && python3 manage.py runserver --nostatic --settings=swh.web.settings.production
run-gunicorn-server:
gunicorn3 -b 127.0.0.1:5004 swh.web.wsgi
......
......@@ -55,6 +55,13 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware'
]
# Compress all assets (static ones and dynamically generated html)
# served by django in a local development environement context.
# In a production environment, assets compression will be directly
# handled by web servers like apache or nginx.
if swh_web_config['debug']:
MIDDLEWARE.insert(0, 'django.middleware.gzip.GZipMiddleware')
ROOT_URLCONF = 'swh.web.urls'
TEMPLATES = [
......
......@@ -20,7 +20,11 @@ if os.environ['DJANGO_SETTINGS_MODULE'] == 'swh.web.settings.production':
from .common import REST_FRAMEWORK
# activate per-site caching
MIDDLEWARE.insert(0, 'django.middleware.cache.UpdateCacheMiddleware')
if 'GZip' in MIDDLEWARE[0]:
MIDDLEWARE.insert(1, 'django.middleware.cache.UpdateCacheMiddleware')
else:
MIDDLEWARE.insert(0, 'django.middleware.cache.UpdateCacheMiddleware')
MIDDLEWARE += ['swh.web.common.middlewares.HtmlMinifyMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware']
......
......@@ -3,10 +3,12 @@
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
from django.conf import settings
from django.conf.urls import (
url, include, handler400, handler403, handler404, handler500
)
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib.staticfiles.views import serve
from django.shortcuts import render
from django.views.generic.base import RedirectView
......@@ -32,7 +34,12 @@ urlpatterns = [
url(r'^jsreverse/$', urls_js, name='js_reverse')
]
urlpatterns += staticfiles_urlpatterns()
# enable to serve compressed assets through django development server
if settings.DEBUG:
static_pattern = r'^%s(?P<path>.*)$' % settings.STATIC_URL[1:]
urlpatterns.append(url(static_pattern, serve))
else:
urlpatterns += staticfiles_urlpatterns()
handler400 = swh_handle400 # noqa
handler403 = swh_handle403 # noqa
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment