Laravel-mix: Postprocess gzip for performance

Created on 15 Apr 2017  路  8Comments  路  Source: JeffreyWay/laravel-mix

  • Laravel Mix Version: 0.10.0
  • Node Version: 6.10.2
  • NPM Version: 3.10.10
  • OS: Ubuntu

Description:

Utilizing the gzip_static module in nginx can reduce the load on the webserver. Instead of compressing script, css and other compressibles on-the-fly for each request, these can be provided pre-compressed and nginx will just use those.

It may be possible to achieve the same behaviour in apache, but I don't use it so I am not aware of it.

It is probably not necessary to do this in development, so it could be integrated with production flag and a optional setting in the mix config file to enable it.

I understand that mix is used on non-linux platforms so an equivalent processing step built in would be nicer since mix already has access to all the files which it has processed.

Of course, these files would have to be deleted when running npm run dev or similar commands so that will also need to be handled

Steps To Reproduce:

On linux, a single liner can achieve this behaviour as below on existing files:
find public/ -name '*.js' -or -name '*.css' -or -name '*.svg' -or -name '*.eot' -or -name '*.ttf' -or -name '*.woff' -or -name '*.woff2' | xargs gzip -6 -k

Most helpful comment

Following up both this issue and #619 this sounds indeed like a good feature and the justification behind them is valid. This would definitively be useful as part of Laravel Mix.

All 8 comments

I'm not fully sure what your question is here? If people want to optimize their assets with Nginx, that's fine of course. But not sure how that affects Mix.

Reason is described in #619
nginx's _gzip_static_ relies on pre-compressed version of file: ex. if app.js.gz exists, it serves it instead of app.js (doesn't perform compression itself) and saves CPU and TTFB dramatically. But someone should create that .gz file. Bundler is ideal candidate for that.

Following up both this issue and #619 this sounds indeed like a good feature and the justification behind them is valid. This would definitively be useful as part of Laravel Mix.

Has this been implemented? I could also use it in my projects.

@molerat619 I don't think it was. But as an example (for laravel setup where package.json entry prod exists under scripts, can be adjusted for any project), an easy way to integrate it (command works on Ubuntu, you may have to tweak it for your case) as that you can add an entry to package.json:

"postprod": "find public/ -name '*.js' -or -name '*.css' -or -name '*.json' -or -name '*.svg' -or -name '*.html' | xargs gzip -6 -k"

So every time your assets are build for production, the corresponding .gz files will be generated automatically.

@sausin Thank you. I was actually able to create the gzip-files. Do I have to change anything in my html or .htaccess so it knows to serve those now instead of the actual .js or .css files?

@molerat619 I can't say about apache, but in nginx (I think version >=1.10; compiled with the support for serving static files), the following directive does the job when included in the server or http section

gzip_static on;

@sausin I've verified and my Apache actually returns gzip-compressed versions of the files. I'm not sure if this question belongs here anymore, but what I actually wanted to achieve was the following:

I have gzip-compressed the files during my build process, so that Apache does not have to do it anymore on every single request. The question now is how those gzip-compressed files can be served, instead of running the compression on every request.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

terion-name picture terion-name  路  3Comments

kpilard picture kpilard  路  3Comments

hasnatbabur picture hasnatbabur  路  3Comments

nezaboravi picture nezaboravi  路  3Comments

pixieaka picture pixieaka  路  3Comments