Skip to content

Commit 4a641ec

Browse files
Check JavaScript files content type. (#1802)
A common problem with windows set ups is that the registry is misconfigured when resolving the content type for a JavaScript file. This check captures this sooner to explain why the toolbar doesn't show and attempts to tell the user what to change to make it work.
1 parent 47d4eed commit 4a641ec

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

debug_toolbar/apps.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
import mimetypes
23

34
from django.apps import AppConfig
45
from django.conf import settings
@@ -174,3 +175,34 @@ def check_panels(app_configs, **kwargs):
174175
)
175176
)
176177
return errors
178+
179+
180+
@register()
181+
def js_mimetype_check(app_configs, **kwargs):
182+
"""
183+
Check that JavaScript files are resolving to the correct content type.
184+
"""
185+
# Ideally application/javascript is returned, but text/javascript is
186+
# acceptable.
187+
javascript_types = {"application/javascript", "text/javascript"}
188+
check_failed = not set(mimetypes.guess_type("toolbar.js")).intersection(
189+
javascript_types
190+
)
191+
if check_failed:
192+
return [
193+
Warning(
194+
"JavaScript files are resolving to the wrong content type.",
195+
hint="The Django Debug Toolbar may not load properly while mimetypes are misconfigured. "
196+
"See the Django documentation for an explanation of why this occurs.\n"
197+
"https://docs.djangoproject.com/en/stable/ref/contrib/staticfiles/#static-file-development-view\n"
198+
"\n"
199+
"This typically occurs on Windows machines. The suggested solution is to modify "
200+
"HKEY_CLASSES_ROOT in the registry to specify the content type for JavaScript "
201+
"files.\n"
202+
"\n"
203+
"[HKEY_CLASSES_ROOT\\.js]\n"
204+
'"Content Type"="application/javascript"',
205+
id="debug_toolbar.W007",
206+
)
207+
]
208+
return []

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Pending
1111
``djdt.cookie.set``.
1212
* Converted ``StaticFilesPanel`` to no longer use a thread collector. Instead,
1313
it collects the used static files in a ``ContextVar``.
14+
* Added check ``debug_toolbar.W007`` to warn when JavaScript files are
15+
resolving to the wrong content type.
1416

1517
4.1.0 (2023-05-15)
1618
------------------

docs/checks.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ Django Debug Toolbar setup and configuration:
1818
configuration needs to have
1919
``django.template.loaders.app_directories.Loader`` included in
2020
``["OPTIONS"]["loaders"]`` or ``APP_DIRS`` set to ``True``.
21+
* **debug_toolbar.W007**: JavaScript files are resolving to the wrong content
22+
type. Refer to :external:ref:`Django's explanation of
23+
mimetypes on Windows <staticfiles-development-view>`.

tests/test_checks.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import unittest
3+
from unittest.mock import patch
34

45
import django
56
from django.conf import settings
@@ -227,3 +228,33 @@ def test_check_w006_valid(self):
227228
)
228229
def test_check_w006_valid_nested_loaders(self):
229230
self.assertEqual(run_checks(), [])
231+
232+
@patch("debug_toolbar.apps.mimetypes.guess_type")
233+
def test_check_w007_valid(self, mocked_guess_type):
234+
mocked_guess_type.return_value = ("text/javascript", None)
235+
self.assertEqual(run_checks(), [])
236+
mocked_guess_type.return_value = ("application/javascript", None)
237+
self.assertEqual(run_checks(), [])
238+
239+
@patch("debug_toolbar.apps.mimetypes.guess_type")
240+
def test_check_w007_invalid(self, mocked_guess_type):
241+
mocked_guess_type.return_value = ("text/plain", None)
242+
self.assertEqual(
243+
run_checks(),
244+
[
245+
Warning(
246+
"JavaScript files are resolving to the wrong content type.",
247+
hint="The Django Debug Toolbar may not load properly while mimetypes are misconfigured. "
248+
"See the Django documentation for an explanation of why this occurs.\n"
249+
"https://docs.djangoproject.com/en/stable/ref/contrib/staticfiles/#static-file-development-view\n"
250+
"\n"
251+
"This typically occurs on Windows machines. The suggested solution is to modify "
252+
"HKEY_CLASSES_ROOT in the registry to specify the content type for JavaScript "
253+
"files.\n"
254+
"\n"
255+
"[HKEY_CLASSES_ROOT\\.js]\n"
256+
'"Content Type"="application/javascript"',
257+
id="debug_toolbar.W007",
258+
)
259+
],
260+
)

0 commit comments

Comments
 (0)