Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev server should send pre-compressed static files without Content-Encoding: gzip #12266

Open
4 tasks done
mz8i opened this issue Mar 2, 2023 · 8 comments
Open
4 tasks done
Labels
bug: upstream Bug in a dependency of Vite has workaround

Comments

@mz8i
Copy link

mz8i commented Mar 2, 2023

Description

I am serving some pre-compressed .csv.gzip files from the public directory.

The dev server automatically serves these with Content-Type: text/csv and Content-Encoding: gzip.

This seems somewhat overzealous, especially when the requested file explicitly has a .csv.gzip extension and the client code expects the content it receives to be compressed.

The problem is mostly that this doesn't seem to be configurable, which leads to inconsistencies when later deploying to sites without such a behaviour. Consider that GitHub Pages sends the same files as Content-Type: application/gzip which is definitely less unexpected.

For example, I ran into this issue because it turned out that my code was not decompressing the CSV files correctly, but I only realised this after deploying to a server because locally I didn't realise the browser was actually doing the decompression based on Content-Encoding. Now, I know that a lot of hosting services like GH Pages gzip content automatically and send it with Content-Encoding so that the compression is transparent to user code, but when the user specifically requests files that are hosted pre-compressed, the services usually don't interfere, and I think Vite dev server should act the same way.

I personally think this should not be the default behaviour, but at least should be possible to turn off.

Suggested solution

Change the default behaviour to not set Content-Encoding: gzip and Content-Type: [...] but rather send as Content-Type: application/gzip for files that are stored as already compressed.

Alternative

Expose a server configuration toggle to allow for turning the current behaviour off.

Additional context

No response

Validations

@xiaohk
Copy link
Contributor

xiaohk commented Jul 15, 2023

I was also tripped by this behavior. See this question on StackOverflow.

@amw
Copy link

amw commented Jan 16, 2024

Ran into the same issue. Our application uses JS fetch to get the file contents and expects them to be gzip data that is later extracted with Pako (JS gzip library). But since Vite added the header the browser already extracted the data.

@aiktb
Copy link

aiktb commented May 25, 2024

I have the same issue due to vite adding the following response header:
(The left side is the abnormal case for vite, the right side is the normal case)
image

This answer on Stackoverflow doesn't solve my problem because I can't change the filename.
I found a VERY hacky way to fix it:

const gzipFixPlugin = () => {
  const fixHeader = (server) => {
    server.middlewares.use((req, res, next) => {
      if (req.originalUrl?.includes(".dat.gz")) {
        res.setHeader("Content-Type", "application/x-gzip");
        // `res.removeHeader("Content-Encoding")` does not work
        res.setHeader("Content-Encoding", "invalid-value");
      }
      next();
    });
  };

  return {
    name: "gzip-fix-plugin",
    configureServer: fixHeader,
    // vite dev and vite preview use different server, so we need to configure both.
    configurePreviewServer: fixHeader,
  };
};

In the header of the middleware the Content-Encoding is undefined, and setting a value explicitly prevents it from being overwritten by "gzip", but I don't know how that works at all.

I'd appreciate it if someone could explain this.

@bluwy
Copy link
Member

bluwy commented May 27, 2024

Is the issue with .gzip or .gz? If it's the latter, there's an upstream issue for that lukeed/sirv#158. I'm not sure Vite is affecting it otherwise.

It would be nice to fix the issue upstream fix, before needing Vite to patch it.

@bluwy bluwy added bug: upstream Bug in a dependency of Vite has workaround and removed enhancement: pending triage labels May 27, 2024
@mz8i
Copy link
Author

mz8i commented May 27, 2024

It's been a while since I originally ran into this, but if my original description is to be trusted, I encountered this with .gzip. I can see the issue you linked mentions .gz only, but I presume this was simply the extension the issue poster was using and the behavior is the same for both extensions. Hopefully the proposed fix tackles both cases.

@bluwy
Copy link
Member

bluwy commented May 27, 2024

@jektvik
Copy link

jektvik commented Jun 12, 2024

Experiencing this with .gz. The CDN serves these files differently so the dev environment gets the content auto gunzipped by the browser but in production it's crashing.

@hi-ogawa
Copy link
Collaborator

FYI, I just attempted a PR on upstream to fixed this lukeed/sirv#161. Upstream fix is a little more involved, but if Vite needs to fix this for own usage, then it would be a one liner to comment out Content-Encoding since Vite doesn't use sirv's gzip: true, brotli: true options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug: upstream Bug in a dependency of Vite has workaround
Projects
None yet
Development

No branches or pull requests

7 participants