Search Unity

[SOLVED] Unity 2020 WebGL Doesn't work Uncaught SyntaxError: Invalid or unexpected token

Discussion in 'WebGL' started by iwanPlays, Apr 21, 2020.

  1. iwanPlays

    iwanPlays

    Joined:
    Feb 28, 2017
    Posts:
    5
    Error in chrome:
    Code (csharp):
    1. Uncaught SyntaxError: Invalid or unexpected token
    2. entershadowweb4.loader.js:1 Uncaught ReferenceError: unityFramework is not defined
    3.     at HTMLScriptElement.n.onload (entershadowweb4.loader.js:1)
    4. entershadowweb4.loader.js:1 [UnityCache] 'http://192.168.56.1:8000/entershadowweb4/Build/entershadowweb4.data.gz' successfully downloaded and stored in the indexedDB cache
    Win10. 2020.2.0a7. Same with 2020.1.

    I'm testing a build using miniweb of https://iwanplays.itch.io/enter-shadow but I also created a fresh empty project with 2020.2.0a7 and got the same result. Optimized for speed, not size.

    4MB web built package: https://drive.google.com/file/d/1Kqij-8ti-K7pltZr1gr9dOdyl7zz2m-j/view?usp=sharing
     
    Don_Gums, d2clon and dog_funtom like this.
  2. iwanPlays

    iwanPlays

    Joined:
    Feb 28, 2017
    Posts:
    5
  3. truegamer2929

    truegamer2929

    Joined:
    May 8, 2020
    Posts:
    1
    Hey, I'm pretty new to unity and want to put my first game on itch.io.
    However, whenever I run the ZIPPED file in the unpublished draft, it says "invalid token '<'"
    PLEASE HELP!
     
  4. Tommy1977

    Tommy1977

    Joined:
    Mar 24, 2020
    Posts:
    13
    same problem invalid token "<"
     
  5. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    970
    disabling compression is a very bad caveman solution
     
    dog_funtom, mowax74 and karmatha like this.
  6. Jolan118

    Jolan118

    Joined:
    Oct 2, 2015
    Posts:
    2
    Any other solutions?
     
  7. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    827
    An error "Uncaught SyntaxError: Invalid or unexpected token" or ""invalid token '<'" suggests that the compression setting did not match with the server hosting configuration:

    either the server is set to host pre-compressed content (by enabling the Content-Encoding: gzip HTTP response header on the resources), but the files that have been uploaded to the server are not actually pre-compressed; or

    the server is not set to host pre-compressed content (no Content-Encoding: gzip HTTP response header is configured), but the files that have been uploaded have been pre-compressed.

    That is, the two things need to match: if one uploads uncompressed content, then "Content-Encoding: gzip" HTTP response header must *NOT* be present in the server configuration, and if one uploads precompressed content, then "Content-Encoding: gzip" HTTP response header *MUST* be present in the server configuration.

    Not familiar with how itch.io configures its hosting, but maybe there is a way to toggle the "Content-Encoding: gzip" in its configuration system?
     
  8. nauroman

    nauroman

    Joined:
    Nov 1, 2014
    Posts:
    12
    Enable decompression fallback

    https://docs.unity3d.com/2020.2/Documentation/Manual/webgl-deploying.html
     
    ttw1993, perplex_bcn, rsodre and 2 others like this.
  9. mvperrotta

    mvperrotta

    Joined:
    Nov 20, 2015
    Posts:
    1
    Thanks for this fallback solution!! This works for me. Had to get this up before the morning and I don't have access to our servers, so this was a great solution until we can make that change. It loads slowly for now, but at least it loads. And I'm assuming it loads at least a bit more quickly than skipping compression altogether.
     
  10. AgnosiaGames

    AgnosiaGames

    Joined:
    May 26, 2020
    Posts:
    56
    I added this code to "index.html";
    Code (JavaScript):
    1. <meta http-equiv="content-encoding" content="gzip" />
    And ".htaccess" file has this code;
    Code (JavaScript):
    1. <IfModule mod_mime.c>
    2.   AddEncoding gzip .unityweb
    3.   AddEncoding gzip .wasm
    4.   AddType application/wasm .wasm
    5. </IfModule>
    But it doesn't work. I have to put this code to another place? Where? And did you mean a code like this or i have to use a different code?
     
    Last edited: Nov 1, 2020
  11. AgnosiaGames

    AgnosiaGames

    Joined:
    May 26, 2020
    Posts:
    56
    "Disable Compression" is not a real solution. Why you wrote "[SOLVED]"? :)
     
  12. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    827
    I have never before seen that kind of http-equiv line. Do you have a reference about this? (my google turned up nothing) In my current understanding that cannot work because a) meta directives should only apply to the file they are in (in this case index.html), and b) the directive is inside the very content that is to be compressed, so a browser should successfully decompress the content first to get to know what the compression is? (a chicken-and-an-egg problem)

    This looks good to me Are you using Apache web server? Are there any errors in the log? Are mod_mime & mod_deflate enabled? Are .htaccess files enabled? Check out e.g. https://forum.unity.com/threads/cant-seem-to-get-apache-to-serve-unityweb-as-gzip.526540/

    "Disable Compression" is the real solution if the web hosting one is using already employs on-demand caching server-side compression. When hosting content on such a server, users should disable compression in Unity builds.
     
    De-Panther likes this.
  13. AgnosiaGames

    AgnosiaGames

    Joined:
    May 26, 2020
    Posts:
    56
    I meant if older versions of Unity doesn't have this problem, How this can be a real solution? It is a new problem on Unity 2020. Is this a bug? Or older versions of Unity have bug?? :D Sorry, i am just asking these questions to myself for many months. :D
     
    Last edited: Nov 1, 2020
  14. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    827
    The only difference between older Unity versions and newer versions is that we have changed the default settings: starting from 2020.1, software compression fallback is disabled by default; whereas in older versions it was enabled by default. That is what makes it look as if Unity 2020.1 has a "bug". If you enable software decompression fallback, you will get old behavior back.

    The core thing here is that nobody should be using software compression fallback. Sure it will have the effect of making the page load, but at great expense to startup times if the server configuration is not correct. That in turn will lead to people blaming Unity that Unity is slow to start up in the browser, when in reality the issue was that the hosting web server did not have the correct configuration.

    Hence we want to switch gears and push developers towards the right performant solution out of the box: get the server configuration right.

    Unfortunately the way that this change landed in Unity 2020.1, the startup experience missed good diagnostics messages to explain when server compression was misconfigured. We have just recently improved this, so next Unity point release should print out more helpful messages at startup if server compression configuration is not properly set up.
     
    insominx, tealm, DrJBN and 3 others like this.
  15. AgnosiaGames

    AgnosiaGames

    Joined:
    May 26, 2020
    Posts:
    56
    Key information is this. :)
     
    Pacosmico likes this.
  16. ATIliev

    ATIliev

    Joined:
    May 5, 2019
    Posts:
    2
    Can you point me to a good source or documentation for the right way for server configuration. Thanks for the post.
     
  17. AgnosiaGames

    AgnosiaGames

    Joined:
    May 26, 2020
    Posts:
    56
  18. Binary42

    Binary42

    Joined:
    Aug 15, 2013
    Posts:
    183
    It is maybe worth to add, how to check if compression is enabled on server side:

    gzip:
    $ curl -I -H 'Accept-Encoding: gzip' https://www.myDomain.asdf

    brotli:
    $ curl -I -H 'Accept-Encoding: br' https://www.myDomain.asdf

    The response will be something like this:

    HTTP/1.1 200 OK
    Server: Apache/2.4
    ETag: "ff5-5a044b137d5c0-gzip"
    Accept-Ranges: bytes
    Vary: Accept-Encoding
    Content-Encoding: gzip
    Content-Length: 1574
    Content-Type: text/html
     
  19. xshadowmintx

    xshadowmintx

    Joined:
    Nov 4, 2016
    Posts:
    40
    This is still broken with no useful error message.
     
  20. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    827
    The improved error messages have landed in to Unity 2021.1.0a4 in November 2020, with a backport request active for Unity 2020.2, but that has unfortunately not yet landed. If possible try checking out Unity 2021.1.0b4 Pre-Release where the improved error messages should exist.
     
  21. xshadowmintx

    xshadowmintx

    Joined:
    Nov 4, 2016
    Posts:
    40
    You have to be kidding. Rolling forward to the pre-alpha quality 2021 release is only going to cause more things to be broken.

    tldr; for anyone else who finds this thread:

    Yes, this is effectively a breaking change in the 2020+ versions of unity; if you're here, you should probably roll back to and stick with the 2019 LTS.
     
  22. SteeBono

    SteeBono

    Joined:
    Apr 4, 2015
    Posts:
    11
    I had the same problem on IIS.
    I solved this way:

    web.config in root folder

    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.     </system.webServer>
    9. </configuration>
    web.config in 'build' folder

    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.                 <mimeMap fileExtension=".framework.js.gz" mimeType="application/javascript" />
    8.                 <mimeMap fileExtension=".data.gz" mimeType="application/octet-stream" />
    9.                 <mimeMap fileExtension=".wasm.gz" mimeType="application/wasm" />
    10.             </staticContent>
    11.             <rewrite>
    12.               <outboundRules rewriteBeforeCache="true">
    13.                 <rule name="Custom gzip file header">
    14.                   <match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
    15.                   <conditions>
    16.                     <add input="{REQUEST_URI}" pattern="\.gz$" />
    17.                   </conditions>
    18.                   <action type="Rewrite" value="gzip"/>
    19.                 </rule>
    20.               </outboundRules>
    21.              
    22.               <rules>
    23.                 <rule name="Rewrite gzip file">
    24.                   <match url="(.*)"/>
    25.                   <conditions>
    26.                     <add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" />
    27.                     <add input="{REQUEST_FILENAME}.gz" matchType="IsFile" />
    28.                   </conditions>
    29.                   <action type="Rewrite" url="{R:1}.gz" />
    30.                 </rule>
    31.               </rules>
    32.             </rewrite>
    33.     </system.webServer>
    34. </configuration>
    Explanation

    Files compiled by Unity3D are already compressed in GZIP.
    IIS must only add the header 'Content-Encoding: gzip' for these files without compressing them again in gzip.

    I hope this can help.
     
    masterlsc likes this.
  23. dog_funtom

    dog_funtom

    Joined:
    Nov 16, 2013
    Posts:
    8
    A tip for users of GH Pages. In 2020.2.6 (maybe earlier too) you don't have to disable compression. Instead, enable decompression fallbacks and this error will go away.
     
    ValkeAllar likes this.
  24. naber78

    naber78

    Joined:
    Aug 25, 2020
    Posts:
    13
    2020.3 and still no good diagnostic message, took me some time to get to here.
     
  25. liaoxingxue

    liaoxingxue

    Joined:
    Apr 29, 2021
    Posts:
    1
    For nginx, DO NOT CONTAINS .gz IN index.html to refer files

    If the gzip_static directive is configured for nginx, for example, for the file abc.js, nginx will first try to find the file with the .gz suffix (abc.js.gz), if it does not exist, it will look for abc.js, which means can no longer include the .gz suffix in the URL path

    Obviously nginx in order to map the correct file mime type, so there is no need to map different js.gz, css.gz and wasm.gz for js, css or wasm files(Content-Type), which eliminates duplicate configuration
     
  26. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,656
    This worked for me, but I think a Compression Format has to be selected, instead of Disabled.
     
  27. insominx

    insominx

    Joined:
    Jan 23, 2012
    Posts:
    31
    For those using node.js and express, this worked for me (probably not the bespoke right way to do it):

    Code (JavaScript):
    1.  
    2. this.server.use(function (req, res, next) {
    3.         if(req.originalUrl.endsWith("wasm.gz")){
    4.             res.set("Content-Type", "application/wasm");
    5.         }
    6.  
    7.         if(req.originalUrl.endsWith(".gz")){
    8.             res.set("Content-Encoding", "gzip");
    9.         }
    10.  
    11.         next();
    12. });
    13.  
    Of note, it's easy to confuse Content-Type with Content-Encoding when then are quite different.
     
unityunity