Warn and fix missing mime types (#1050)

This commit is contained in:
Mark Bakhit 2023-06-14 23:15:37 -07:00 committed by GitHub
parent a3d79aa8f5
commit bd60e6e7e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View file

@ -23,6 +23,10 @@ more info, see the :ref:`Contributor Guide <Creating a Changelog Entry>`.
Unreleased
----------
**Changed**
- :pull:`1050` - Warn and attempt to fix missing mime types, which can result in ``reactpy.run`` not working as expected.
**Fixed**
- :issue:`930` - better traceback for JSON serialization errors (via :pull:`1008`)

View file

@ -0,0 +1,22 @@
import mimetypes
from logging import getLogger
_logger = getLogger(__name__)
# Fix for missing mime types due to OS corruption/misconfiguration
# Example: https://github.com/encode/starlette/issues/829
if not mimetypes.inited:
mimetypes.init()
for extension, mime_type in {
".js": "application/javascript",
".css": "text/css",
".json": "application/json",
}.items():
if not mimetypes.types_map.get(extension): # pragma: no cover
_logger.warning(
"Mime type '%s = %s' is missing. Please research how to "
"fix missing mime types on your operating system.",
extension,
mime_type,
)
mimetypes.add_type(mime_type, extension)