[Solved] Cargo web + "incorrect response MIME type" errors


#1

I have the following situation:

  1. cargo web start , load in browser = works fine

  2. cargo deploy; cd target/deploy; python -m SimpleHTTPServer , load in browser =

Error loading Rust wasm module 'local_dev': TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
  1. This is
google chrome
Fedora 27
cargo web 0.6.23
rustc 1.32.0-nightly
  1. I tried googling a bit, but could not find anything related to cargo + web; all the errors seemts to be related to manually written WASM and web servers returning bad mime types

#2

Oh, I experienced this as well, if you want to use SimpleHTTPServer then I found this python script which seems to work well. It’s not related to cargo web, as it produces a wasm module, and uses its own server which corrects for the non-universal mime-typing.

# -*- coding: utf-8 -*-
#test on python 3.4 ,python of lower version  has different module organization.
import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver

PORT = 8080

Handler = http.server.SimpleHTTPRequestHandler

Handler.extensions_map={
        '.manifest': 'text/cache-manifest',
	'.html': 'text/html',
        '.png': 'image/png',
	'.jpg': 'image/jpg',
	'.svg':	'image/svg+xml',
	'.css':	'text/css',
	'.js':	'application/x-javascript',
    '.wasm': 'application/wasm',
	'': 'application/octet-stream', # Default
    }

httpd = socketserver.TCPServer(("", PORT), Handler)

print("serving at port", PORT)
httpd.serve_forever()

Note however that this does work properly on username.github.io like here: optimisticpeach.github.io


Edit:

Ah, here it is:


#3

@OptimisticPeach : Thanks!

I independently ran into https://curiousprog.com/2018/10/08/serving-webassembly-files-with-a-development-web-server/ , which lso worked, and I believe is documenting the exact same issue.


#4

@OptimisticPeach : Have ou by any chance run into this exact problem with uploading to AWS S3 and know a fix for that too? :slight_smile:


#5

Ah, no I’ve never worked with the web (Besides the calculator on my GitHub page), so I wouldn’t know much about AWS, this is only the second project I’ve done in the web. Sorry for the letdown. But it is a problem with the mime type configuration on the sever so perhaps consult with the AWS documentation?


#6

For anyone running into this via search. Here is the solution that worked for me:

aws s3 cp website/ s3://$WEBSITE_BUCKET --recursive --exclude "*.wasm"
echo "reuploading wasm"
aws s3 cp website/ s3://$WEBSITE_BUCKET --recursive  \
  --content-type "application/wasm" \
  --exclude "*" --include "*.wasm" 

The first cp uploads everything except for *.wasm, with AWS/S3 guessing the content-type.
the second cp uploads the *.wasm, with content-type forced to “application/wasm”

Due to caching issues, it’s important that the first line excludes wasm (we don’t want s3 to push/cache files with wrong content-type).