Skip to content

Commit

Permalink
Check JavaScript files content type. (#1802)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tim-schilling authored Jul 4, 2023
1 parent 47d4eed commit 4a641ec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
32 changes: 32 additions & 0 deletions debug_toolbar/apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import mimetypes

from django.apps import AppConfig
from django.conf import settings
Expand Down Expand Up @@ -174,3 +175,34 @@ def check_panels(app_configs, **kwargs):
)
)
return errors


@register()
def js_mimetype_check(app_configs, **kwargs):
"""
Check that JavaScript files are resolving to the correct content type.
"""
# Ideally application/javascript is returned, but text/javascript is
# acceptable.
javascript_types = {"application/javascript", "text/javascript"}
check_failed = not set(mimetypes.guess_type("toolbar.js")).intersection(
javascript_types
)
if check_failed:
return [
Warning(
"JavaScript files are resolving to the wrong content type.",
hint="The Django Debug Toolbar may not load properly while mimetypes are misconfigured. "
"See the Django documentation for an explanation of why this occurs.\n"
"https://docs.djangoproject.com/en/stable/ref/contrib/staticfiles/#static-file-development-view\n"
"\n"
"This typically occurs on Windows machines. The suggested solution is to modify "
"HKEY_CLASSES_ROOT in the registry to specify the content type for JavaScript "
"files.\n"
"\n"
"[HKEY_CLASSES_ROOT\\.js]\n"
'"Content Type"="application/javascript"',
id="debug_toolbar.W007",
)
]
return []
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Pending
``djdt.cookie.set``.
* Converted ``StaticFilesPanel`` to no longer use a thread collector. Instead,
it collects the used static files in a ``ContextVar``.
* Added check ``debug_toolbar.W007`` to warn when JavaScript files are
resolving to the wrong content type.

4.1.0 (2023-05-15)
------------------
Expand Down
3 changes: 3 additions & 0 deletions docs/checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ Django Debug Toolbar setup and configuration:
configuration needs to have
``django.template.loaders.app_directories.Loader`` included in
``["OPTIONS"]["loaders"]`` or ``APP_DIRS`` set to ``True``.
* **debug_toolbar.W007**: JavaScript files are resolving to the wrong content
type. Refer to :external:ref:`Django's explanation of
mimetypes on Windows <staticfiles-development-view>`.
31 changes: 31 additions & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
from unittest.mock import patch

import django
from django.conf import settings
Expand Down Expand Up @@ -227,3 +228,33 @@ def test_check_w006_valid(self):
)
def test_check_w006_valid_nested_loaders(self):
self.assertEqual(run_checks(), [])

@patch("debug_toolbar.apps.mimetypes.guess_type")
def test_check_w007_valid(self, mocked_guess_type):
mocked_guess_type.return_value = ("text/javascript", None)
self.assertEqual(run_checks(), [])
mocked_guess_type.return_value = ("application/javascript", None)
self.assertEqual(run_checks(), [])

@patch("debug_toolbar.apps.mimetypes.guess_type")
def test_check_w007_invalid(self, mocked_guess_type):
mocked_guess_type.return_value = ("text/plain", None)
self.assertEqual(
run_checks(),
[
Warning(
"JavaScript files are resolving to the wrong content type.",
hint="The Django Debug Toolbar may not load properly while mimetypes are misconfigured. "
"See the Django documentation for an explanation of why this occurs.\n"
"https://docs.djangoproject.com/en/stable/ref/contrib/staticfiles/#static-file-development-view\n"
"\n"
"This typically occurs on Windows machines. The suggested solution is to modify "
"HKEY_CLASSES_ROOT in the registry to specify the content type for JavaScript "
"files.\n"
"\n"
"[HKEY_CLASSES_ROOT\\.js]\n"
'"Content Type"="application/javascript"',
id="debug_toolbar.W007",
)
],
)

0 comments on commit 4a641ec

Please sign in to comment.