Search Unity

WebAssembly streaming instantiation - issue

Discussion in 'Web' started by MindBuild, Jun 18, 2019.

  1. MindBuild

    MindBuild

    Joined:
    Oct 7, 2018
    Posts:
    22
    Hello all,

    I have tried to find information in the forums, but haven't had any success. I have implemented webasm streaming and keep getting this error:

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

    I have attached screenshots of the current server config. Not sure what I am missing. Any help would be greatly appreciated.
     

    Attached Files:

  2. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    I know very little about server configurations (so use with caution :D), but this is what we use for a htaccess file for streaming:


    Options +FollowSymLinks
    RewriteEngine on
    <Files "*.wasm">
    AddType application/wasm .wasm
    AddEncoding br wasm
    </Files>
    <Files "*.unityweb">
    AddEncoding br unityweb
    </Files>


    The following didn't work on our server:


    Options +FollowSymLinks
    RewriteEngine on
    AddType application/wasm .wasm
    AddEncoding br wasm
    AddEncoding br unityweb


    The server would add br twice to the response header for the file "[build name].wasm.framework.unityweb". I guess we could rename the file after build (to perhaps just "[build name].framework.unityweb"), but it was just easier to adjust the htaccess file. :p
     
    alexu likes this.
  3. MindBuild

    MindBuild

    Joined:
    Oct 7, 2018
    Posts:
    22
    Thanks Kognito! Unfortunately that didn't work :/.

    Will keep testing and doing more research.
     
  4. MindBuild

    MindBuild

    Joined:
    Oct 7, 2018
    Posts:
    22
    Hello all,


    I managed to fix the mime type issue, just needed to add the correct mime type to the mime.types. However, I can’t seem to figure out how to fix this error:

    wasm streaming compile failed: CompileError: wasm validation error: at offset 4: failed to match magic number


    I have tried to hire a developer to help me out, but haven’t had any luck.


    I am using a nginx server and would appreciate any help. Happy to pay to get this working.


    Anyone got any ideas?
     
  5. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    You need to make sure the wasm file also has the correct Content-Encoding header applied. e.g. gzip or br (Brotli) because this file is compressed (assuming you didn't explicitly disable compression in the build settings) and the browser doesn't recognize it as a valid wasm file.

    Alternatively for testing you can open up your 'wasm' file in something like 7-Zip or WinRar and extract the larger 'wasm' within - then replace the server version to confirm it is at least loading correctly when it isn't compressed.
     
    MindBuild likes this.
  6. MindBuild

    MindBuild

    Joined:
    Oct 7, 2018
    Posts:
    22
    Thank you JJJohan,

    I will try to do that. Compression is not disabled I am using Brotli for the builds and have added content-encoding heading in the web config.

    Code (JavaScript):
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <configuration>
    3.     <system.webServer>
    4.             <staticContent>
    5.                     <remove fileExtension=".unityweb" />
    6.                     <mimeMap fileExtension=".unityweb" mimeType="application/octet-stream" />
    7.             </staticContent>
    8.             <rewrite>
    9.                     <outboundRules>
    10.                         <rule name="Append br Content-Encoding header">
    11.                             <match serverVariable="RESPONSE_Content-Encoding" pattern=".*" />
    12.                             <conditions>
    13.                                     <add input="{REQUEST_FILENAME}" pattern="\.unityweb$" />
    14.                             </conditions>
    15.                             <action type="Rewrite" value="br" />
    16.                         </rule>
    17.                     </outboundRules>
    18.             </rewrite>
    19.     </system.webServer>
    20. </configuration>
    Does everything look right?

    Thanks JJJohan!
     
  7. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    I use Nginx myself for some other things but haven't had a chance to try a Unity WebGL build with streaming instantiation yet so I can't confirm the settings exactly. If I've got some time I'll try after work today and let you know.

    Looking at your config it looks like you should add another condition for the '.wasm' file type in your outboundRules list.
    Code (JavaScript):
    1. <outboundRules>
    2.     <rule name="Append br Content-Encoding header">
    3.         <match serverVariable="RESPONSE_Content-Encoding" pattern=".*" />
    4.         <conditions>
    5.                 <add input="{REQUEST_FILENAME}" pattern="\.unityweb$" />
    6.                 <add input="{REQUEST_FILENAME}" pattern="\.wasm$" /> // <-- Add this line
    7.         </conditions>
    8.         <action type="Rewrite" value="br" />
    9.     </rule>
    10. </outboundRules>
     
  8. MindBuild

    MindBuild

    Joined:
    Oct 7, 2018
    Posts:
    22
    Thank you JJJohan, I added that to the web.config still having the same compile issue.

    Thanks for your help.
     
    Last edited: Jul 8, 2019
  9. metaphysics

    metaphysics

    Joined:
    Nov 25, 2014
    Posts:
    2
    Has anyone had any luck with this. I have made the changes to get IIS to respond with the proper content-encoding header, which gets you past the "magic number" error, but then you run into the error "net::ERR_CONTENT_DECODING_FAILED" when it is trying to download the unityweb or wasm files
     
  10. MindBuild

    MindBuild

    Joined:
    Oct 7, 2018
    Posts:
    22
    Thank you JJJohan,

    he managed to save the day. Got it working, just needed to make sure the proper content-encoding was working.

    @metaphysics is this a problem that occurs when you haven't enabled instantiation?
     
  11. HarpSeal1

    HarpSeal1

    Joined:
    Oct 23, 2013
    Posts:
    35
    wasm streaming compile failed: CompileError: wasm validation error: at offset 4: failed to match magic number

    *incoherent screeching* I had this error too in IIS, it took me 8 hours to figure out the issue so I hope this saves someone a lot of time! Turns out you can't add a second condition for wasm, adding the second rule breaks the entire gzip configuration and you'll get the warning about using gzip compression in your browser console. Basically you have to add a new rule for it like this and you could just copy and paste this entire config, should work.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <system.webServer>
    <staticContent>
    <remove fileExtension=".unityweb" />
    <mimeMap fileExtension=".unityweb" mimeType="application/octet-stream" />
    <mimeMap fileExtension=".wasm" mimeType="application/wasm" />
    </staticContent>
    <rewrite>
    <outboundRules>
    <rule name="Append gzip Content-Encoding header">
    <match serverVariable="RESPONSE_Content-Encoding" pattern=".*" />
    <conditions>
    <add input="{REQUEST_FILENAME}" pattern="\.unityweb$" />
    </conditions>
    <action type="Rewrite" value="gzip" />
    </rule>
    <rule name="Add gzip encoding header to wasm">
    <match serverVariable="RESPONSE_Content-Encoding" pattern=".*" />
    <conditions>
    <add input="{REQUEST_FILENAME}" pattern="\.wasm$" />
    </conditions>
    <action type="Rewrite" value="gzip" />
    </rule>
    </outboundRules>
    </rewrite>
    </system.webServer>
    </configuration>
     
  12. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    In this day and age, you're going to have a horrible time running your own static web host. You can set content-type and content-encoding with S3 and server out of CloudFront with its default HTTPS certificate, and things will be a-okay.
     
  13. alexu

    alexu

    Joined:
    Jun 19, 2014
    Posts:
    8
    This is exactly the problem I had! Thanks for the solution.