Skip to content

Commit c80f64b

Browse files
committed
Deduplicate staticfiles
1 parent 9ee6c20 commit c80f64b

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-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
------------------

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)