question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to gzip compress CSS and JS

See original GitHub issue

I tried to find a way to compress files and version them but couldn’t find a way to do this.

Basically, I want to do something like this,

mix.compress(['path/to/file1.css', 'path/to/file2.js'], 'destination/path/');

Result:

'destination/path/file1.css.gz'
'destination/path/file2.js.gz'

Could someone please point me in the right direction? Thanks!

PS Thank you so much for your awesome work @JeffreyWay

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
ankurk91commented, Mar 25, 2017

@mrajabtech Most modern web servers has ability to serve gzip content on the fly. Apache has mod_gzip and mod_deflate while nginx can also do that.

We should not serve *.gz content unless browser request for it, and web-servers can take care of that automatically.

Is there any other good reason that Laravel Mix should generate *.gz files ?

10reactions
populovcommented, Sep 13, 2017

@ankurk91 Reason is simple: if you have 1000 requests to *.js file with gzip enabled, it will be compressed by nginx 1000 times. Same file with exactly same output. if we have *.js.gz file and gzip_static enabled, nginx will just output that file to user. It not only saves CPU (and you can rent less powerful servers and pay less), but also significantly reduces TTFB (Time to first byte) - user doesn’t wait for compression to complete.

I used this:

npm install --save compression-webpack-plugin

webpack.mix.js

const CompressionPlugin = require('compression-webpack-plugin');

...

  plugins: [
    new LiveReloadPlugin(),
    new webpack.ProvidePlugin({
      jQuery: 'jquery',
    }),
    new CompressionPlugin({
      asset: "[path].gz[query]",
      algorithm: "gzip",
      test: /\.js$|\.css$|\.html$|\.svg$/,
      threshold: 10240,
      minRatio: 0.8
    })
  ],
});

mix.js('resources/assets/js/app.js', 'public/assets/js')
  .sass('resources/assets/sass/app.scss', 'public/assets/styles')
  .copyDirectory('resources/assets/img', 'public/assets/img')
  .copyDirectory('resources/assets/fonts', 'public/assets/fonts')

and it works well for .js & .css files, but it ignores, for example, .svg files, copied from resources to public folder. Any Idea, how to gzip them?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I gzip my JavaScript and CSS files? - Stack Overflow
Restart the nginx with command /etc/init.d/nginx reload . It will compress the JS and CSS files. Share.
Read more >
The Difference Between Minification and Gzipping | CSS-Tricks
Gzipping is far more effective. Doing both is ideal. ... Gzipping reduces the file size about five times as much as minifying does....
Read more >
How to activate Gzip Compression CSS and JS for better ...
Step 1: Enable gzip for PHP · Step2: Create a PHP Compressor · Step 3: Redirect requests for CSS and JavaScript to PHP....
Read more >
How to Enable GZIP Compression for Faster Web Pages
GZIP is the current standard for file compression on the web. ... This will compress all HTML, CSS, JavaScript, XML, and font files....
Read more >
How to manually gzip CSS and JS files and serve them with ...
How to manually gzip CSS and JS files and serve them with Apache ... They will be compressed into new files with a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found