Skip to content

Commit 1918457

Browse files
Fix asset URL handling in development mode (#222)
* Fix asset URL handling in development mode Fix asset URL handling to exclude the django_bird prefix in development mode, while maintaining it in production mode. This ensures that static assets are correctly referenced in both environments: - In development: static/bird/modal.js - In production: static/django_bird/bird/modal.js * update comment * update tests
1 parent 7ae55d7 commit 1918457

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
2222

2323
- Normalize paths in asset manifest to prevent leaking system-specific information like Python version and installation paths
2424

25+
### Fixed
26+
27+
- Fixed asset URL handling in development mode to exclude the "django_bird/" prefix, matching the actual file paths in development environments. Production mode (DEBUG=False) still uses the prefix to maintain compatibility with collected static files.
28+
2529
## [0.17.1]
2630

2731
### Fixed

src/django_bird/staticfiles.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import final
1313
from typing import overload
1414

15+
from django.conf import settings
1516
from django.contrib.staticfiles import finders
1617
from django.contrib.staticfiles.finders import BaseFinder
1718
from django.contrib.staticfiles.storage import StaticFilesStorage
@@ -178,7 +179,11 @@ def __init__(self, *args: Any, prefix: str, **kwargs: Any):
178179
def url(self, name: str | None) -> str:
179180
if name is None:
180181
return super().url(name)
181-
if not name.startswith(f"{self.prefix}/"):
182+
# Only add prefix in production (when DEBUG is False)
183+
# In development, asset paths don't include the app label prefix
184+
# because they come directly from source directories and in production,
185+
# assets are collected to STATIC_ROOT/django_bird/
186+
if not settings.DEBUG and not name.startswith(f"{self.prefix}/"):
182187
name = f"{self.prefix}/{name}"
183188
return super().url(name)
184189

tests/templatetags/test_asset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def test_asset_tag_in_debug_mode(
480480
rendered = template.render({})
481481

482482
assert (
483-
f'<link rel="stylesheet" href="/static/django_bird/bird/{button_css.file.name}">'
483+
f'<link rel="stylesheet" href="/static/bird/{button_css.file.name}">'
484484
in rendered
485485
)
486486

tests/test_staticfiles.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,14 @@ def test_relative_path_nested(self, templates_dir):
198198

199199
assert asset.relative_path == Path("bird/nested/button.css")
200200

201-
def test_url(self, templates_dir):
201+
@pytest.mark.parametrize(
202+
"DEBUG,expected_prefix",
203+
[
204+
(False, "/static/django_bird"),
205+
(True, "/static"),
206+
],
207+
)
208+
def test_url(self, DEBUG, expected_prefix, templates_dir):
202209
button = TestComponent(
203210
name="button",
204211
content="<button>Click me</button>",
@@ -212,10 +219,11 @@ def test_url(self, templates_dir):
212219
component = Component.from_name(button.name)
213220
asset = component.get_asset(button_css.file.name)
214221

215-
assert (
216-
asset.url
217-
== f"/static/django_bird/{button_css.file.parent.name}/{button_css.file.name}"
218-
)
222+
with override_settings(DEBUG=DEBUG):
223+
assert (
224+
asset.url
225+
== f"{expected_prefix}/{button_css.file.parent.name}/{button_css.file.name}"
226+
)
219227

220228
def test_url_nonexistent(self, templates_dir):
221229
button = TestComponent(

0 commit comments

Comments
 (0)