Skip to content

Commit 911d6a0

Browse files
dragoneeclaude
andcommitted
fix: reload asset tag files in DEBUG mode when modified
Add mtime tracking to asset tag file cache to automatically reload files during development when they change. In production, maintain permanent in-memory cache for optimal performance. Changes: - Track file mtime and path in cache structure alongside content - Check mtime in DEBUG mode and invalidate cache when file changes - Skip mtime checks in production for better performance - Extract _empty_cache_entry helper to avoid duplication - Early return in production mode for faster cache hits This fixes the issue where rebuilt asset files weren't reflected without restarting the Django server during development. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent de7d950 commit 911d6a0

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

tasks/apps/common/templatetags/render_assets.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
register = template.Library()
88

99
# In-memory caches for CSS and JS tag files
10+
# Structure: {entry_point: {'content': str, 'mtime': float, 'path': Path}}
1011
CSS_FILES = {}
1112
JS_FILES = {}
1213

@@ -29,20 +30,26 @@ def get_potential_staticfile_paths(filename):
2930
for static_dir in settings.STATICFILES_DIRS:
3031
yield Path(static_dir) / filename
3132

33+
def _empty_cache_entry():
34+
return {'content': '', 'mtime': None, 'path': None}
3235

3336
def read_cached_staticfile(entry_point, file_type, cache):
3437
"""Reads and caches static tag files.
3538
39+
In DEBUG mode, checks file mtime and reloads if changed.
40+
In production, uses permanent in-memory cache.
41+
3642
Args:
3743
entry_point - Name of the entry point (e.g., 'app', 'hello_world_mount')
3844
file_type - Type of file ('css' or 'js')
3945
cache - Dictionary to use for caching
4046
Returns:
4147
HTML content from the cached file or empty string if not found.
4248
"""
43-
# Check if already cached
44-
if entry_point in cache:
45-
return cache[entry_point]
49+
50+
# In production, use permanent in-memory cache
51+
if not settings.DEBUG and entry_point in cache:
52+
return cache[entry_point]['content']
4653

4754
filename = f'{entry_point}-{file_type}-tags.html'
4855

@@ -54,17 +61,38 @@ def read_cached_staticfile(entry_point, file_type, cache):
5461

5562
if html_file_path is None:
5663
# No file found, cache empty string
57-
cache[entry_point] = ''
64+
if entry_point not in cache:
65+
cache[entry_point] = _empty_cache_entry()
5866
return ''
5967

6068
try:
69+
# In DEBUG mode, check if file has been modified
70+
if settings.DEBUG and entry_point in cache:
71+
cached_mtime = cache[entry_point].get('mtime')
72+
current_mtime = html_file_path.stat().st_mtime
73+
74+
# If mtime changed, invalidate cache
75+
if cached_mtime is not None and cached_mtime != current_mtime:
76+
del cache[entry_point]
77+
78+
# Check if already cached (and still valid)
79+
if entry_point in cache:
80+
return cache[entry_point]['content']
81+
82+
# Read and cache the content
6183
content = html_file_path.read_text()
62-
# Cache the content
63-
cache[entry_point] = content
84+
mtime = html_file_path.stat().st_mtime if settings.DEBUG else None
85+
86+
cache[entry_point] = {
87+
'content': content,
88+
'mtime': mtime,
89+
'path': html_file_path
90+
}
6491
return content
6592
except (FileNotFoundError, OSError):
6693
# Cache empty string to avoid repeated file lookups
67-
cache[entry_point] = ''
94+
if entry_point not in cache:
95+
cache[entry_point] = _empty_cache_entry()
6896
return ''
6997

7098

0 commit comments

Comments
 (0)