Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question GZip just won't work & fallback only runs on fast connections. Why?

Discussion in 'WebGL' started by MKrohn, Oct 27, 2021.

  1. MKrohn

    MKrohn

    Joined:
    Apr 5, 2018
    Posts:
    23
    Hi everybody,

    I am at my wits end when it comes to making GZip compression on WebGL-builds work, especially on the (Apache) server side of things.

    I'm using the htaccess configuration as provided on this page https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html at the very bottom (obviously, I commented out the brotli part of it).

    Unfortunately, I'm always getting the following errors and the game does not start:

    Uncaught SyntaxError: Invalid or unexpected token

    output.loader.js:1 Uncaught ReferenceError: unityFramework is not defined
    at HTMLScriptElement.r.onload (output.loader.js:1)

    Screenshot:
    gzip-errors.png

    Things look better when I activate "Decompression Fallback" in Unity's WebGL publishing settings, or if I just don't use compression. However, in this case the game only works on rather fast internet connections. Slower connections (simulated or real) always result in this error after a minute or so of loading:

    GET http://[our.server.domain]/game/Build/output.wasm.unityweb net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)

    Screenshot:
    gzip-fallback-error.png

    The game we develop is for schools (with usually notoriously slow internet connections), so this is rather dramatic for us.

    Our webspace and server provider says GZip is enabled on our server, and that there is no timeout for downloading/streaming files.

    Unity version is 2020.3.3f1.
    Any help would be greatly appreciated, because I'm stuck and the projects release is due next week :( .
     
    Last edited: Oct 28, 2021
  2. MKrohn

    MKrohn

    Joined:
    Apr 5, 2018
    Posts:
    23
    Well, the first part of my question (gzip) is not as important as the second part is. The game not loading on slow connections is a huge problem for us.

    According to his discussion: https://github.com/angular/angular-cli/issues/7197 , the problem might be due to node.js timing out. Now, I'm embarrassed to not know (and not being able to find out) whether node.js is somehow part of Unity's webpack-functionality, or part of my (rented) web server, or not involved at all. Does anybody know?

    I seem to get a slightly better result with Unity 2021.2.0f1, where sometimes "only" the file '*.data.unityweb' fails to load, but '*wasm.unityweb' does load, in which case the game still runs. But most of time, loading the game still fails with 'net:ERR_CONTENT_LENGTH_MISMATCH 200' on slow connections, albeit with some more accompanying error messages:
    unity2021-wasm-errors.png

    Again, no problems on faster connections. Really seems to be some kind of timeout-issue. The data-package is 13.2 Mb in size, while the whole build is 22 Mb.

    Please, any ideas?
     
    Last edited: Oct 28, 2021
  3. MKrohn

    MKrohn

    Joined:
    Apr 5, 2018
    Posts:
    23
    Oh, I found someone nice enough to allow us to deploy our game on their server, just to see if the game would fare better there. And indeed: It works!

    So, the issue must be with our server, not with Unity, as it seems!

    I'll update when (if) I find out what the issue is.
     
  4. anthony_b_

    anthony_b_

    Unity Technologies

    Joined:
    Oct 27, 2020
    Posts:
    23
    Hello!

    Most web servers, when configured to serve gzip compressed files will still expect the files placed in the directory to server to be uncompressed. The webserver will then compress each file when it serves it.

    However, since the Unity WebGL build artifacts are large, this can be an inefficient load on the webserver (to compress the file on each request). So we can compress the artifacts as part of the Unity build, and this generates *.gz files when gzip compression is selected. Now, the webserver needs to be configured to expect statically compressed gzip files to serve. Therefore, the webserver shouldn't itself (re-)compress them as gzip, but it does need to add the gzip content-encoding to the served files. This is what the server configuration in the Unity docs at
    https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html configures.

    The Invalid or unexpected token error above is saying that statically compressed gzip files are used, but the webserver isn't correctly configured to add the zip content-encoding to those files. You should be able to verify this using the Network tab of the Developer Tools in your browser. If you look at the response, you should be able to check the content-encoding. So, check the webserver configuration: is the .htaccess file correct, and placed in the correct directory (next to the .gz files)? Check the server log files to see if there are any errors logged (e.g. parse errors of the .htaccess files).

    Your second post refers to node.js. Are you using Apache as a webserver, or node.js as a webserver?
     
  5. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Anybody figure out how to configure Vercel to properly serve gzip or brotli?
    They talk about compression here https://vercel.com/docs/concepts/edge-network/compression but I can't see how to configure it as per https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html

    For now I've disabled compression, which is ok for testing but not a good idea for production.

    Any vercel experts out there?

    Have a reliable deployment system that covers all these options for webgl would help a lot of people atm, I think.