Skip to content
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

Check JavaScript files content type. #1802

Merged
merged 3 commits into from
Jul 4, 2023
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
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",
)
],
)