Giter Site home page Giter Site logo

Comments (6)

djbingham avatar djbingham commented on August 15, 2024 2

I think I've hit the same issue as the OP (or very similar).

My goal is to have Nginx serve either a gzipped or uncompressed file for each request, depending on whether the uncompressed size exceeds a threshold. To achieve this, I've setup the Webpack Compression Plugin with a threshold for file size (as per the README example) and configured Nginx to check for a ".gz" extended version of each requested file.

WebPack configuration:

new CompressionPlugin({
	asset: "[path].gz[query]",
	algorithm: "gzip",
	test: /\.js$|\.css$|\.html$/,
	threshold: 10240,
	minRatio: 0.8
})

Nginx configuration:

location / {
	root /srv/app;
	try_files $uri.gz $uri =404;
}

Expected result:
Nginx serves gzipped JS files with Content-Type: application/javascript and Content-Encoding: gzip.

Actual result:
Browser received gzipped files with Content-Type: application/x-gzip and no Content-Encoding value.

I'm new to compressing assets for the web and don't know whether it's possible to set file meta-data that Nginx (or other web servers) will parse into Content-Type and Content-Encoding values, but if it is I think it should be done by this plugin. If not, I'll just have to use a smarter Nginx configuration.

UPDATE @11:25 UTC Friday 2nd June

So, it turns out there is a much simpler solution available with Nginx, using the http_gzip_static_module. According to the docs, that module is not built by default. However, it is included in the official Nginx Docker image.

location / {
	gzip_static on;
	rewrite ^/?$ /index.html break;
	root /srv/app;
}

gzip_static on; seems to result in correct Content-Type and Content-Encoding being passed to the client. It also handles checking for a compressd file (with ".gz" extension appended) and falling back to the uncompressed file, if available.

from compression-webpack-plugin.

enzolutions avatar enzolutions commented on August 15, 2024 1

Hi, guys great job BTW.

I have the same question, how I could include gzip files in my HTML or must be normal and I have to include some extra configuration in muy server Apache, Nginx, etc?

from compression-webpack-plugin.

michael-ciniawsky avatar michael-ciniawsky commented on August 15, 2024

Can someone please explain with some code what's the goal here, normally content-encoding etc. is configured by the webserver

from compression-webpack-plugin.

willin avatar willin commented on August 15, 2024

mark

from compression-webpack-plugin.

michael-ciniawsky avatar michael-ciniawsky commented on August 15, 2024

This is a config issue with the webserver in use and unrelated to this plugin as it simply compresses the assets for further usage. Please consider the docs of your webserver

from compression-webpack-plugin.

rupamkhaitan avatar rupamkhaitan commented on August 15, 2024

You dont need to define in the location
location / {
gzip_static on;
rewrite ^/?$ /index.html break;
root /srv/app;
}

In the main /etc/nginx/nginx.conf

just enable inside

http{

#enable compression of data
gzip on;
gzip_types text/css text/plain application/xml application/javascript application/json application/octet-stream image/gif image/png image/jpeg image/jpg;
gzip_proxied no-cache no-store private expired auth;
#The default is 20 bytes (here adjusted to 1000 if needed)
#gzip_min_length 1000;
gunzip on;
}

And see it will compress the files and send

from compression-webpack-plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.