Skip to content

Commit 1cea8b6

Browse files
Fix component caching when DEBUG=True to properly track assets (#96)
1 parent ab831e2 commit 1cea8b6

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

CHANGELOG.md

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

1919
## [Unreleased]
2020

21+
### Fixed
22+
23+
- **Internal**: Fixed component caching behavior to properly track assets. Components are now always cached for asset tracking, while still providing fresh templates in `DEBUG` mode.
24+
2125
## [0.8.1]
2226

2327
### Fixed

src/django_bird/components.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def discover_components(self) -> None:
9191
component_path.relative_to(component_dir).with_suffix("")
9292
)
9393
try:
94-
self.get_component(component_name)
94+
component = Component.from_name(component_name)
95+
self._components[component_name] = component
9596
except TemplateDoesNotExist:
9697
continue
9798

@@ -101,12 +102,14 @@ def clear(self) -> None:
101102

102103
def get_component(self, name: str) -> Component:
103104
try:
104-
return self._components[name]
105-
except KeyError:
106-
component = Component.from_name(name)
107105
if not settings.DEBUG:
108-
self._components[name] = component
109-
return component
106+
return self._components[name]
107+
except KeyError:
108+
pass
109+
110+
component = Component.from_name(name)
111+
self._components[name] = component
112+
return component
110113

111114
def get_assets(self, asset_type: AssetType) -> list[Asset]:
112115
assets: list[Asset] = []

tests/test_components.py

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,56 @@ def test_missing_asset_file(self, templates_dir):
396396

397397

398398
class TestComponentRegistryCaching:
399+
def test_debug_mode_caching(self, templates_dir):
400+
component = TestComponent(
401+
name="button", content="<button>Original</button>"
402+
).create(templates_dir)
403+
404+
with override_settings(DEBUG=True):
405+
first = components.get_component("button")
406+
407+
assert "button" in components._components
408+
assert "Original" in first.template.template.source
409+
410+
component.file.write_text("<button>Updated</button>")
411+
second = components.get_component("button")
412+
413+
assert second is not first
414+
assert "Updated" in second.template.template.source
415+
416+
def test_production_mode_caching(self, templates_dir):
417+
component = TestComponent(
418+
name="button", content="<button>Original</button>"
419+
).create(templates_dir)
420+
421+
with override_settings(DEBUG=False):
422+
first = components.get_component("button")
423+
424+
component.file.write_text("<button>Updated</button>")
425+
second = components.get_component("button")
426+
427+
assert second is first
428+
assert "Original" in second.template.template.source
429+
430+
@pytest.mark.parametrize("debug", [True, False])
431+
def test_asset_tracking(self, debug, templates_dir):
432+
button = TestComponent(
433+
name="button", content="<button>Click me</button>"
434+
).create(templates_dir)
435+
436+
button_css = TestAsset(
437+
component=button,
438+
content=".button { color: red; }",
439+
asset_type=AssetType.CSS,
440+
).create()
441+
442+
with override_settings(DEBUG=debug):
443+
components.get_component("button")
444+
css_assets = components.get_assets(AssetType.CSS)
445+
446+
assert len(css_assets) == 1
447+
assert Asset(button_css.file, button_css.asset_type) in css_assets
448+
399449
def test_lru_cache_limit(self, templates_dir):
400450
small_registry = ComponentRegistry(maxsize=2)
401451

@@ -412,19 +462,6 @@ def test_lru_cache_limit(self, templates_dir):
412462
assert "button1" in small_registry._components
413463
assert "button2" in small_registry._components
414464

415-
def test_debug_mode_caching(self, templates_dir):
416-
TestComponent(name="button", content="<button>Cache Me</button>").create(
417-
templates_dir
418-
)
419-
420-
with override_settings(DEBUG=True):
421-
components.get_component("button")
422-
assert "button" not in components._components
423-
424-
with override_settings(DEBUG=False):
425-
components.get_component("button")
426-
assert "button" in components._components
427-
428465
def test_cache_clear(self, templates_dir):
429466
TestComponent(name="button", content="<button>Clear Me</button>").create(
430467
templates_dir

0 commit comments

Comments
 (0)