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

Incorrect MIME type for templated javascript module imports #829

Closed
jbsetton opened this issue Feb 17, 2020 · 4 comments
Closed

Incorrect MIME type for templated javascript module imports #829

jbsetton opened this issue Feb 17, 2020 · 4 comments

Comments

@jbsetton
Copy link

Greetings all,
I cannot seem to find where one explicitly defines mime types for imports. My browser is blocking all javascript module imports due to the server serving them as (“text/plain”). I realize this is a simple configuration issue, but I cannot seem to find where to set it. Any assistance provided is greatly appreciated. The console error shown below with my python 'app'.
I am running Win10 x64, Firefox 73.0 (64-bit), Python 3.7, uvicorn 0.11.2, and starlette 0.13.1.
I also posted this in the uvicorn issues.

image

from starlette.applications import Starlette
from starlette.staticfiles import StaticFiles
from starlette.responses import HTMLResponse
from starlette.templating import Jinja2Templates
import uvicorn
from random import randint

port = randint(49152,65535)

templates = Jinja2Templates(directory='templates')

app = Starlette(debug=True)
app.mount('/static', StaticFiles(directory='statics'), name='static')


@app.route('/')
async def homepage(request):
    template = "index.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context)


@app.route('/error')
async def error(request):
    """
    An example error. Switch the `debug` setting to see either tracebacks or 500 pages.
    """
    raise RuntimeError("Oh no")


@app.exception_handler(404)
async def not_found(request, exc):
    """
    Return an HTTP 404 page.
    """
    template = "404.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context, status_code=404)


@app.exception_handler(500)
async def server_error(request, exc):
    """
    Return an HTTP 500 page.
    """
    template = "500.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context, status_code=500)


if __name__ == "__main__":
    uvicorn.run("app-567:app", host='0.0.0.0', port=port, access_log=True, http='h11', loop='asyncio', reload=True)
@jbsetton
Copy link
Author

I think the mimetypes standard library on windows doesn't properly locate the correct dictionary of all known types resulting in some files being mischaracterized as ("text/plain"). To get around this issue, I forcibly imported, initiated and added keys to mimetypes

import mimetypes
mimetypes.init()

and here:

@app.route('/')
async def homepage(request):
	mimetypes.add_type('application/javascript', '.js')
	mimetypes.add_type('text/css', '.css')
	mimetypes.add_type('image/svg+xml', '.svg')
	template = "index.html"
	context = {"request": request}
	return templates.TemplateResponse(template, context, media_type='text/html')

And the resulting web app correctly serves the files as the appropriate MIME content type.

@tomchristie
Copy link
Member

I think the mimetypes standard library on windows doesn't properly locate the correct dictionary of all known types resulting in some files being mischaracterized as ("text/plain"). To get around this issue, I forcibly imported, initiated and added keys to mimetypes

Okay, so a useful thing to do here would be to start by confirming that.

Want to try opening up python in a console, and just using...

from mimetypes import guess_type

print(guess_type("something.js"))
print(guess_type("something.css"))
print(guess_type("something.svg"))

If there is an issue with mimetypes support being pretty unreliable, then we could take a look at including some defaults for basic HTML related content.

@Xevion
Copy link

Xevion commented Jun 28, 2020

While looking for solutions to a related problem concerning content types and the mimetypes module, I found this issue.
This issue from the Flask web framework details a investigation to why the mimetypes module was being unreliable. Very interesting.

@carc1n0gen
Copy link

carc1n0gen commented Jun 29, 2020

If on windows, it's a windows registry issue. See the issue linked by @Xevion above me for more information about the registry. On windows, python relies fully on the registry for the mime types

EDIT additional info:

I had a similar issue at one point for just css files. I had to edit the windows registry key HKEY_CLASSES_ROOT/.css/Content Type back to text/css because the VIsualStudio (Not visual studio code) installer had messed it up. I only blame VisualStudio because I work on flask apps every day, and it happened right after installing VisualStudio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants