From d04c6b33041e89342559eee4dca80a5722a0b531 Mon Sep 17 00:00:00 2001 From: Nicholas FitzRoy-Dale Date: Mon, 5 Aug 2024 22:35:25 +1000 Subject: [PATCH] Work around Windows 10 mime type bug for javascript --- rime/rimeserver.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/rime/rimeserver.py b/rime/rimeserver.py index d8e3965..816b4f3 100644 --- a/rime/rimeserver.py +++ b/rime/rimeserver.py @@ -73,6 +73,11 @@ async def run_cmd_in_background_rime(): return result +STATIC_MIME_TYPES = { + '.js': 'application/javascript', +} + + class ZippedStaticFiles: def __init__(self, zip_pathname): self.zf = zipfile.ZipFile(zip_pathname, 'r') @@ -81,7 +86,14 @@ def __call__(self, path): if path == '': path = 'index.html' - mime_type = mimetypes.guess_type(path)[0] + # Windows 10 (may?) incorrectly identify JavaScript as text/plain. See https://bugs.python.org/issue43975 + for ext, candidate_mime_type in STATIC_MIME_TYPES.items(): + if path.endswith(ext): + mime_type = candidate_mime_type + break + else: + mime_type = mimetypes.guess_type(path)[0] + return StreamingResponse(self.zf.open(path), media_type=mime_type)