Skip to content

Commit afd5e7e

Browse files
authored
Deduplicate staticfiles (#2155)
Currently, the staticfiles panel contains one entry per static() call. That's not the same thing as used staticfiles, since assets can be deduplicated in multiple ways, e.g. through forms.Media or because the browser actually understands that it should load <script type="module"> once only. I think it makes more sense to show a deduplicated and sorted list of static files used. Refs matthiask/django-prose-editor#48
1 parent 9ee6c20 commit afd5e7e

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

debug_toolbar/panels/staticfiles.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import contextlib
22
import uuid
33
from contextvars import ContextVar
4+
from dataclasses import dataclass
45
from os.path import join, normpath
56

67
from django.contrib.staticfiles import finders, storage
@@ -10,14 +11,14 @@
1011
from debug_toolbar import panels
1112

1213

14+
@dataclass(eq=True, frozen=True)
1315
class StaticFile:
1416
"""
1517
Representing the different properties of a static file.
1618
"""
1719

18-
def __init__(self, *, path, url):
19-
self.path = path
20-
self._url = url
20+
path: str
21+
url: str
2122

2223
def __str__(self):
2324
return self.path
@@ -72,7 +73,7 @@ def title(self):
7273
def __init__(self, *args, **kwargs):
7374
super().__init__(*args, **kwargs)
7475
self.num_found = 0
75-
self.used_paths = []
76+
self.used_paths = set()
7677
self.request_id = str(uuid.uuid4())
7778

7879
@classmethod
@@ -88,7 +89,7 @@ def _store_static_files_signal_handler(self, sender, staticfile, **kwargs):
8889
# concurrent connections and we want to avoid storing of same
8990
# staticfile from other connections as well.
9091
if request_id_context_var.get() == self.request_id:
91-
self.used_paths.append(staticfile)
92+
self.used_paths.add(staticfile)
9293

9394
def enable_instrumentation(self):
9495
self.ctx_token = request_id_context_var.set(self.request_id)
@@ -112,7 +113,7 @@ def generate_stats(self, request, response):
112113
{
113114
"num_found": self.num_found,
114115
"num_used": len(self.used_paths),
115-
"staticfiles": self.used_paths,
116+
"staticfiles": sorted(self.used_paths),
116117
"staticfiles_apps": self.get_staticfiles_apps(),
117118
"staticfiles_dirs": self.get_staticfiles_dirs(),
118119
"staticfiles_finders": self.get_staticfiles_finders(),

docs/changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Pending
3030
* Extend example app to contain an async version.
3131
* Added ``debug_toolbar.store.DatabaseStore`` for persistent debug data
3232
storage.
33+
* Deduplicated static files in the staticfiles panel.
3334

3435
5.2.0 (2025-04-29)
3536
------------------

docs/spelling_wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ biome
1717
checkbox
1818
contrib
1919
csp
20+
deduplicated
2021
dicts
2122
django
2223
fallbacks

tests/templates/staticfiles/path.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{% load static %}{% static path %}
1+
{% load static %}
2+
{# A single file used twice #}
3+
{% static path %}{% static path %}

0 commit comments

Comments
 (0)