ring-clojure / ring

Clojure HTTP server abstraction
MIT License
3.75k stars 519 forks source link

how to add wasm(WebAssembly)and etc content-type? #450

Closed PlumpMath closed 2 years ago

PlumpMath commented 2 years ago

I'm using ring.middleware.cors with unity3d wasm.

error: wasm streaming compile failed: TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.

warning: HTTP Response Header "Content-Type" configured incorrectly on the server for file Build/MyGame.wasm , should be "application/wasm". Startup time performance will suffer.

(def ring-middle (-> ROUTE/my-route RING-MIDDLEWARE-JSON/wrap-json-body RING-MIDDLEWARE-JSON/wrap-json-response (RING-MIDDLEWARE-DEFAULTS/wrap-defaults RING-MIDDLEWARE-DEFAULTS/site-defaults) (RING-MIDDLEWARE-CORS/wrap-cors :access-control-allow-credentials "true" :access-control-allow-origin [#".*"] :access-control-allow-headers #{"accept" "accept-encoding" "accept-language" "authorization" "content-type" "origin"} ;; I want to add mime-types. "content-type:... "application/wasm br gz javascript" ... etc :access-control-allow-methods [:get :put :post :delete :options]) ))

weavejester commented 2 years ago

Take a look at the ring.middleware.content-type/wrap-content-type middleware function. You can customize it by supplying your own mimetype map.

PlumpMath commented 2 years ago

Thanks for the hint. But it doesn't work. When loading js from the html side, the address… Do I have to wrap the route by pulling out unity in advance? And when I go to the map and set content-type as the key and application/wasm as the value, the result is the same. The webgl page itself doesn't use index.html but loads unity webgl js from hiccup so I'm not sure what to do anymore. So I wonder if this suspicion is correct... (Actually, the program runs without problems. I just want to make sure I catch errors and warnings.)

weavejester commented 2 years ago

Can you show me what you're doing when you say it doesn't work?

Also, how are you serving the wasm files? I assume as a public resource?

PlumpMath commented 2 years ago

I can't upload the project as it is because will work, so I'll make a simple example and upload it. Please understand that it may be late. thank you.

PlumpMath commented 2 years ago

It's so late, I'm sorry. work is too busy; I was busy with other things.

After a few months of work end it, and as soon as possible I was thinking of uploading example at this rest time.

For Unity webgl compilation, there are two ways to compress wasm once (br and gzip method). This is an uncompressed build.

Please take a look, and I hope you catch the error by adding content-type: . thank you. @weavejester

weavejester commented 2 years ago

I've been waiting for you to upload the example. There doesn't seem to be one linked or attached to the issue.

PlumpMath commented 2 years ago

hmm i'm solve! sorry~. ;;

unity webgl build gzip & compression fallback? (check) That way I don't have to deal with it.

And I'm not sure if this has any effect... https://gist.github.com/martintrojer/841d6712a432380fe97c

Later, not unity wasm, other c++ or rust or godot (tools I want to deal with) If a problem occurs in ... (I hope do not think again, but the web is too difficult for me...) thank you