Repository: jazzband/django-debug-toolbar

Issue:
Create a system check that determines if mimetypes issue exists
Our docs call out the troublsome mimetype issue, but it's pretty difficult to parse out whether it needs to be addressed for a beginner. Ideally we'd tell people when this is a problem and what to do exactly rather than rely on them to search our docs for "mimetype" when they hit a hurdle.

I suspect we should be able to load the `mimetypes` library and inspect what's returned for `.js`.

We should also look to improve the docs by indicating what the steps are or better highlight what to search for them. https://stackoverflow.com/a/64055514/1637351 shows a good example of what to do.


Discussion:

Resolving patch:
diff --git a/debug_toolbar/apps.py b/debug_toolbar/apps.py
--- a/debug_toolbar/apps.py
+++ b/debug_toolbar/apps.py
@@ -1,4 +1,5 @@
 import inspect
+import mimetypes
 
 from django.apps import AppConfig
 from django.conf import settings
@@ -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 []


Test patch:
diff --git a/tests/test_checks.py b/tests/test_checks.py
--- a/tests/test_checks.py
+++ b/tests/test_checks.py
@@ -1,5 +1,6 @@
 import os
 import unittest
+from unittest.mock import patch
 
 import django
 from django.conf import settings
@@ -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",
+                )
+            ],
+        )