2

I have compressed my react files with webpack's CompressionPlugin with gz format. Also I added following config in web.config for reading gz files in IIS:

<system.webServer>
  <staticContent>
        <remove fileExtension=".js.gz" />
        <remove fileExtension=".css.gz" />
        <remove fileExtension=".png.gz" />
        <remove fileExtension=".jpg.gz" />
        <remove fileExtension=".gif.gz" />
        <remove fileExtension=".svg.gz" />
        <remove fileExtension=".html.gz" />
        <remove fileExtension=".json.gz" />
        <mimeMap fileExtension=".js.gz" mimeType="application/javascript" />
        <mimeMap fileExtension=".css.gz" mimeType="text/css" />
        <mimeMap fileExtension=".png.gz" mimeType="image/png" />
        <mimeMap fileExtension=".jpg.gz" mimeType="image/jpeg" />
        <mimeMap fileExtension=".gif.gz" mimeType="image/gif" />
        <mimeMap fileExtension=".svg.gz" mimeType="image/svg+xml" />
        <mimeMap fileExtension=".html.gz" mimeType="text/html" />
        <mimeMap fileExtension=".json.gz" mimeType="application/json" />
      <!--<mimeMap fileExtension=".woff2" mimeType="font/woff2" />-->
    </staticContent>
      <rewrite>
          <outboundRules  rewriteBeforeCache="true">
              <rule name="Custom gzip file header">
                  <match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
                  <conditions>
                      <add input="{REQUEST_URI}" pattern="\.gz$" />
                  </conditions>
                  <action type="Rewrite" value="gzip"/>
              </rule>
          </outboundRules>
          <rules>
              <rule name="Rewrite gzip file">
                  <match url="(.*)"/>
                  <conditions>
                      <add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" />
                      <add input="{REQUEST_FILENAME}.gz" matchType="IsFile" />
                  </conditions>
                  <action type="Rewrite" url="{R:1}.gz" />
              </rule>
          </rules>
      </rewrite>
 </system.webServer>

Unfortunalety sometimes when I clear cache with ctrl + f5 in console I get:

Uncaught SyntaxError: Invalid or unexpected token : main.js.gz

Also here's the picture of Sources part of google chrome's developer tools:

enter image description here

Does anyone know that where is the problem? Thank you in advance.

Majid M.
  • 4,467
  • 1
  • 11
  • 29
  • Clear cache get this error? Does it mean that there's no request send to server but get this error? If the site has main.js file, will it report error when request for this file? – Bruce Zhang Dec 27 '21 at 06:02
  • I clear cache and refresh page by `ctrl + f5`. Obviously there is a request for getting the `main.js` and `vendor.js` again. But unfortunately sometimes I get the mentioned error. I don't know why sometimes client can't detect that the files which is sent by the server have `gz` format and should decompress them. – Majid M. Dec 27 '21 at 06:52
  • 1
    I ran into this issue with WordPress and gzipped HTML (index.html.gz), I resolved that by storing the original Accept-Encoding header in an extra one (HTTP_X_ORIGINAL_ACCEPT_ENCODING) so I can later on restore the Accept-Encoding header. It's here posted (careful, years old post): https://www.saotn.org/speed-up-wordpress-with-gzip-compression/ – Jan Reilink Dec 27 '21 at 13:07
  • 1
    Could be for several reasons. Maybe it's missing the wrong response header. 1) Add the gzip headers for files in a folder. Similar issue: https://stackoverflow.com/questions/69628186/uncaught-syntaxerror-invalid-or-unexpected-token-for-js-gz-file-compressed-an 2) Disable static compression in IIS. Else it will compress the already compressed files again. – Valderann Jan 02 '22 at 10:09
  • @Valderann Thank you for the comment! Actually I think its related to IIS settings for static compression which you mentioned. I should check it! – Majid M. Jan 03 '22 at 06:30
  • @MajidM. I have the same issue. How did you solve it? – Matt Feb 13 '23 at 18:12
  • 1
    @Matt It goes back to more than 1 year a go! I don't remember what was the solution exactly, but I know that the problem was related to 2 times compression! 1 time from `web.config` and another from other part of the application. – Majid M. Feb 14 '23 at 08:34
  • 1
    @MajidM. np, I found the answer here https://stackoverflow.com/a/63794100/968379 – Matt Feb 14 '23 at 08:57

0 Answers0