Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Brotli/Gzip Broken On WebGL?

Discussion in 'Web' started by vriog, Jun 30, 2022.

  1. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    I've tried to get compression working on both Apache and Nginx on Debian but can't get it to work. The only way it works is with "decompression fallback" enabled. All the files are being served by my server with Content-Encoding: gzip, br headers.

    I'm using Unity 2022.1.7f1. Testing on Safari/Chromium/Firefox on a Macbook with the latest OS X. Does anyone actually have gzip and brotli working on an nginx/apache server without going through decompression fallback?
     
    cr3am likes this.
  2. kintovt

    kintovt

    Joined:
    Jan 5, 2017
    Posts:
    13
    cr3am likes this.
  3. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    Thank you very much for adding your experience. I was hoping I was the only one with this problem, someone had a fix. I've been trying to get this to work for weeks, but now it's time to ship my game and I want players to have the fastest possible loading time.
     
    kintovt and cr3am like this.
  4. cr3am

    cr3am

    Joined:
    Jul 6, 2022
    Posts:
    2
    Yes I’ve been going crazy struggling with this problem and cannot find a freaking solution I’m hosting my webgl game on a linode VPS, tried Nginx on Ubuntu, Nginx on CentOs, can’t get compression ever working, and my games 100 MB+ so fast load times and fast brotli decompression is a must :mad:
     
    vriog likes this.
  5. kintovt

    kintovt

    Joined:
    Jan 5, 2017
    Posts:
    13
    I was trying nginx+ubuntu and nginx+centos too, it's a miracle, but for some reason ubuntu+nginx works now, I didn't change my last conf, it just started to work on the next day after I gave up on it.
     
  6. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    Which version of Ubuntu and nginx ? Ubuntu 22.04 LTS + nginx 1.18.0? Also, did you use the conf file here, or did you make any changes to it: https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html
     
  7. kintovt

    kintovt

    Joined:
    Jan 5, 2017
    Posts:
    13
    Ubuntu 22, nginx 1.22
    Have "brotli_static on;" in my /etc/nginx/nginx.conf
    And site config uses https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html example.

    Idk what exactly must be done for the clean setting, because tried different guides and playing with configs for some time.
    The main point is that you need to enable external nginx repo, install nginx from it, compile and install brotli module from the source. There few tutorials in the internet how to do that with ubuntu. Non of them completely worked for me, but somehow job was done.
     
  8. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    Very interesting, that's exactly what I did as well (compiling and installing brotli module from source) but with no success.
     
  9. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    Hi all, I’m still having trouble wit this issue. Today I tried Nginx CentOS again from scratch, couldn’t get it worked . Does anyone have any more suggestions on what OS/Server combo might work? Other than resorting to Windows+IIS..
     
  10. kintovt

    kintovt

    Joined:
    Jan 5, 2017
    Posts:
    13
    D
    Looks like there is no correct nginx+brotly repo/package combination for Centos at the moment. Did you try to make things work from scratch again using Ubuntu? I had troubles when installing nginx and some of nginx-* packages from the default repo and when enabling brotli added external repo and installed some other packages from it. It is good to have all the Nginx-brotli things installed from one trustable repo.
     
  11. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    Yes I just re-built my server yesterday, with Ubuntu 18.04, followed the instructions to server brotli and it still doesn't work for the Unity build.

    Getting the errors still:

    You can reduce startup time if you configure your web server to add "Content-Encoding: br" response header when serving "Build/d74ca168565please7fe1a96effafeb.js.unityweb" file.

    You can reduce startup time if you configure your web server to add "Content-Encoding: br" response header when serving "Build/6b99c05795ffix11ea765efcbab5f.wasm.unityweb" file.

    You can reduce startup time if you configure your web server to add "Content-Encoding: br" response header when serving "Build/25ad12ec0fthisproblemunitydevsccff51cf67.data.unityweb" file.
     
  12. cybernetsurfer7

    cybernetsurfer7

    Joined:
    May 21, 2015
    Posts:
    4
    Hey y'all. I've run into a very similar issue. I'm using a docker compose script to stand up a local nginx server. I've got the same configurations from the link to unity's page (https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html) set up. I'm using gzip instead of brotli as I'm not doing https locally. Similarly, I have decompression fallback off. I can't seem to get it to work. The `loader.js` file makes an attempt to fetch the `.data.gz` file but then fails immediately even though the file download starts as expected and completes without error. I'm beginning to wonder if this is an issue in the `loader.js` being built with unity 2022? I'm going to try a 2021 version and see if the same issue persists.
     
    vriog likes this.
  13. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    Thanks for commenting on this, let me know if you were able to eventually get it to work!
     
  14. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    Can anyone from Unity comment on this?
     
  15. danb1

    danb1

    Joined:
    Jun 14, 2019
    Posts:
    24
    I use an Apache 2.4 server and it works with with the htaccess file attached below, with the mime_module enabled in the config file. One of the things that helped me debugging this was using the "Network" tab in the browser's developer tools to see what type the browser was seeing each file getting downloaded as. I was never able to get the htaccess file that Unity provides to work (from this page).

    Code (JavaScript):
    1. # This configuration file should be uploaded to the server as "<Application Folder>/Build/.htaccess"
    2. # This configuration has been tested with Unity 2020.1 builds, hosted on Apache/2.4
    3. # NOTE: "mod_mime" Apache module must be enabled for this configuration to work.
    4.  
    5. # The following lines are required for builds without decompression fallback, compressed with gzip
    6.  
    7.  
    8. <IfModule mod_mime.c>
    9.  
    10. <FilesMatch "[^.]+\.data.gz$">
    11.   Header set Content-Type "application/octet-stream"
    12.   Header set Content-Encoding "gzip"
    13. </FilesMatch>
    14.  
    15. <FilesMatch "[^.]+\.js.gz$">
    16.   Header set Content-Type "application/javascript"
    17.   Header set Content-Encoding "gzip"
    18. </FilesMatch>
    19.  
    20. <FilesMatch "[^.]+\.wasm.gz$">
    21.   Header set Content-Type "application/wasm"
    22.   Header set Content-Encoding "gzip"
    23. </FilesMatch>
    24.  
    25. <FilesMatch "[^.]+\.gz$">
    26.   Header set Content-Encoding "gzip"
    27. </FilesMatch>
    28.  
    29. <FilesMatch "[^.]+\.wasm$">
    30.   # Header set Content-Encoding "gzip"
    31.   Header set Content-Type "application/wasm"
    32. </FilesMatch>
    33.  
    34. </IfModule>
    35.  
     
    gtk2k likes this.
  16. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    229
    Added support for Brotli

    Code (CSharp):
    1. <IfModule mod_headers.c>
    2. <IfModule mod_env.c>
    3.  
    4. <FilesMatch "\.data\.(gz|br)$">
    5.     Header set Content-Type "application/octet-stream"
    6. </FilesMatch>
    7.  
    8. <FilesMatch "\.js\.(gz|br)$">
    9.     Header set Content-Type "application/javascript"
    10. </FilesMatch>
    11.  
    12. <FilesMatch "\.wasm\.(gz|br)$">
    13.     Header set Content-Type "application/wasm"
    14. </FilesMatch>
    15.  
    16.  
    17. <FilesMatch "\.gz$">
    18.     Header set Content-Encoding "gzip"
    19. </FilesMatch>
    20.  
    21.  
    22. <FilesMatch "\.br$">
    23.     Header set Content-Encoding "br"
    24.     # Disable automatic gzip compression as it conflicts with Brotli.
    25.     SetEnv no-gzip
    26. </FilesMatch>
    27.  
    28.  
    29. <FilesMatch "\.wasm$">
    30.     Header set Content-Type "application/wasm"
    31. </FilesMatch>
    32.  
    33. </IfModule>
    34. </IfModule>
     
    Last edited: Feb 4, 2023
  17. vriog

    vriog

    Joined:
    Dec 6, 2014
    Posts:
    31
    You guys are awesome, it works!! Now if only we had this for nginx!
     
  18. alininavcisi

    alininavcisi

    Joined:
    Mar 10, 2015
    Posts:
    8
    Hello @iovr It works also for nginx. Problem is server configuration manual in Unity official tutorials.

    - First of all, maybe you tried, you DON'T HAVE TO install brotli module on nginx. Chrome and other popular browsers already have brotli decompression algorithm. Your server must only point "hey chrome, that file compressed with brotli, please decompress this file with brotli first to see whats inside"

    - In https://docs.unity3d.com/Manual/webgl-server-configuration-code-samples.html you have to put location codes into /etc/nginx/sites-available/[yoursite] file so when you open the game page, ngingx will tag that js.br files as "content-encoding: br" and our browsers will understand that "I need to decode them with brotli algorithm"
     
  19. sonolil

    sonolil

    Joined:
    Mar 15, 2015
    Posts:
    14
    Oh it was octet-stream for data.br. That wasn't in the docs... https://docs.unity3d.com/Manual/webgl-deploying.html

    Yes, browsers today already can natively decompress brotli. The problem is serving them with the wrong content-type. It was the same for me in node.js, I'm pretty sure nginx has its own ways of resolving it. What @forestrf did should be the blueprint to solve any issue concerned with those silly mime-type errors in Unity webgl. As long as you have dominion over the server-side code..
     
    forestrf likes this.