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

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

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

        '.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)

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


Ah, here it is:


@OptimisticPeach : Thanks!

I independently ran into Serving WebAssembly Files with a Development Web Server – CuriousProg , which lso worked, and I believe is documenting the exact same issue.

@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:

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?

1 Like

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).