Skip to content

Deduplicate staticfiles #2155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions debug_toolbar/panels/staticfiles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import uuid
from contextvars import ContextVar
from dataclasses import dataclass
from os.path import join, normpath

from django.contrib.staticfiles import finders, storage
Expand All @@ -10,14 +11,14 @@
from debug_toolbar import panels


@dataclass(eq=True, frozen=True)
class StaticFile:
"""
Representing the different properties of a static file.
"""

def __init__(self, *, path, url):
self.path = path
self._url = url
path: str
url: str

def __str__(self):
return self.path
Expand Down Expand Up @@ -72,7 +73,7 @@ def title(self):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.num_found = 0
self.used_paths = []
self.used_paths = set()
self.request_id = str(uuid.uuid4())

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

def enable_instrumentation(self):
self.ctx_token = request_id_context_var.set(self.request_id)
Expand All @@ -112,7 +113,7 @@ def generate_stats(self, request, response):
{
"num_found": self.num_found,
"num_used": len(self.used_paths),
"staticfiles": self.used_paths,
"staticfiles": sorted(self.used_paths),
"staticfiles_apps": self.get_staticfiles_apps(),
"staticfiles_dirs": self.get_staticfiles_dirs(),
"staticfiles_finders": self.get_staticfiles_finders(),
Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Pending
* Extend example app to contain an async version.
* Added ``debug_toolbar.store.DatabaseStore`` for persistent debug data
storage.
* Deduplicated static files in the staticfiles panel.

5.2.0 (2025-04-29)
------------------
Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ biome
checkbox
contrib
csp
deduplicated
dicts
django
fallbacks
Expand Down
4 changes: 3 additions & 1 deletion tests/templates/staticfiles/path.html
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{% load static %}{% static path %}
{% load static %}
{# A single file used twice #}
{% static path %}{% static path %}