Skip to content

Commit

Permalink
Use settings.FORCE_SCRIPT_NAME correctly (#486)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Adam Johnson <[email protected]>
Co-authored-by: Mark Bakhit <[email protected]>
  • Loading branch information
3 people authored Oct 28, 2024
1 parent c42e93c commit 9494ff3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
11 changes: 11 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ Unreleased

Thanks to Per Myren for the detailed investigation and fix in `PR #612 <https://github.com/evansd/whitenoise/pull/612>`__.

* Use Django’s |FORCE_SCRIPT_NAME|__ setting correctly.
This reverts a change from version 5.3.0 that added a call to Django’s |get_script_prefix() method|__ outside of the request-response cycle.

.. |FORCE_SCRIPT_NAME| replace:: ``FORCE_SCRIPT_NAME``
__ https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-FORCE_SCRIPT_NAME

.. |get_script_prefix() method| replace:: ``get_script_prefix()`` method
__ https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.get_script_prefix

Thanks to Sarah Boyce in `PR #486 <https://github.com/evansd/whitenoise/pull/486>`__.

6.7.0 (2024-06-19)
------------------

Expand Down
6 changes: 4 additions & 2 deletions docs/django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,10 @@ arguments upper-cased with a 'WHITENOISE\_' prefix.
then ``WHITENOISE_STATIC_PREFIX`` will be ``/static/``.

If your application is not running at the root of the domain and
``FORCE_SCRIPT_NAME`` is set then this value will be removed from the
``STATIC_URL`` path first to give the correct prefix.
``FORCE_SCRIPT_NAME`` is set, then this value will be removed from the
``STATIC_URL`` path first, to give the correct prefix. For example, if
``STATIC_URL`` is ``'apple/static/`` and ``FORCE_SCRIPT_NAME`` is
``'/apple'``, then ``WHITENOISE_STATIC_PREFIX`` will be ``/static/``.

If your deployment is more complicated than this (for instance, if you are
using a CDN which is doing path rewriting) then you may need to configure
Expand Down
9 changes: 4 additions & 5 deletions src/whitenoise/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.contrib.staticfiles import finders
from django.contrib.staticfiles.storage import staticfiles_storage
from django.http import FileResponse
from django.urls import get_script_prefix

from whitenoise.base import WhiteNoise
from whitenoise.string_utils import ensure_leading_trailing_slash
Expand Down Expand Up @@ -94,10 +93,10 @@ def __init__(self, get_response=None, settings=settings):
self.static_prefix = settings.WHITENOISE_STATIC_PREFIX
except AttributeError:
self.static_prefix = urlparse(settings.STATIC_URL or "").path
script_prefix = get_script_prefix().rstrip("/")
if script_prefix:
if self.static_prefix.startswith(script_prefix):
self.static_prefix = self.static_prefix[len(script_prefix) :]
if settings.FORCE_SCRIPT_NAME:
script_name = settings.FORCE_SCRIPT_NAME.rstrip("/")
if self.static_prefix.startswith(script_name):
self.static_prefix = self.static_prefix[len(script_name) :]
self.static_prefix = ensure_leading_trailing_slash(self.static_prefix)

self.static_root = settings.STATIC_ROOT
Expand Down
8 changes: 8 additions & 0 deletions tests/test_django_whitenoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,11 @@ def test_relative_static_url(server, static_files, _collect_static):
url = storage.staticfiles_storage.url(static_files.js_path)
response = server.get(url)
assert response.content == static_files.js_content


@override_settings(FORCE_SCRIPT_NAME="/subdir", STATIC_URL="static/")
def test_force_script_name(server, static_files, _collect_static):
url = storage.staticfiles_storage.url(static_files.js_path)
assert url.startswith("/subdir/static/")
response = server.get(url)
assert response.content == static_files.js_content

0 comments on commit 9494ff3

Please sign in to comment.