简体   繁体   English

为什么gulp-gzip我可以在不配置服务器的情况下提供gzip压缩内容?

[英]Why gulp-gzip and can I serve gzipped content without configuring the server?

I ran into a dilemma lately as I was exploring the various plugins for gulp. 我最近在探索各种插件时陷入了困境。 One of them was gulp-gzip and till then, I have never thought about compressing my files. 其中一个是gulp-gzip ,直到那时,我从未想过压缩我的文件。 I got gulp-gzip to work correctly and spit out gzipped versions of my HTML, CSS and JS files but then, what next? 我得到gulp-gzip正常工作并吐出我的HTML,CSS和JS文件的gzip压缩版本然后,下一步是什么?

I googled around and found that most articles talk about configuring the server to send gzipped versions of the content automatically to the client upon request. 我搜索了一下,发现大多数文章都谈到配置服务器以根据请求自动向客户端发送gzip压缩版本的内容。 But then, I kind of don't seem to understand the purpose of gzipping locally. 但是,我似乎并不理解本地gzipping的目的。

So, my questions are: 所以,我的问题是:

  1. Can I serve gzipped content I get from gulp-gzip without configuring my server? 我可以在不配置服务器的情况下提供gulp-gzip提供的gzip压缩内容吗?
  2. If yes, how should I proceed -- what should I name my gzipped files as? 如果是的话,我应该怎么做 - 我应该把我的gzip文件命名为什么? Should I keep the .gz extension and link to my CSS and JS files using the same? 我应该保留.gz扩展名并使用相同的链接到我的CSS和JS文件吗?
  3. If yes, can I test it locally by linking to the same .gz files? 如果是,我可以通过链接到相同的.gz文件在本地测试吗?
  4. If no, what is the purpose of gulp-gzip in a development environment if the server can be configured to do it automatically? 如果不是,如果服务器可以配置为自动执行,那么gulp-gzip在开发环境中的用途是什么?

Most servers have an option to serve statically pre-compressed files if a *.gz version exists, ie when user requests foo.css , the server will check if foo.css.gz exists and use it. 如果存在*.gz版本,大多数服务器都可以选择提供静态预压缩文件,即当用户请求foo.css ,服务器将检查foo.css.gz存在并使用它。

It requires server support (the server has to set appropriate HTTP headers), so it won't work with the file:// protocol and may not work on every server. 它需要服务器支持(服务器必须设置适当的HTTP头),因此它不能与file://协议一起使用,并且可能无法在每台服务器上运行。

In URLs you have to refer to the base filename (do not link to .gz directly). 在URL中,您必须引用基本文件名(不要直接链接到.gz )。

Compressing files ahead of time may be better: 提前压缩文件可能会更好:

  • You can use higher compression level (eg maximum gzip level or the Zopfli compressor), which would be too slow to do real-time on the server. 您可以使用更高的压缩级别(例如,最大gzip级别或Zopfli压缩器),这对于在服务器上实时执行而言太慢。
  • Compressing ahead of time saves CPU time of the server, because it doesn't have to dynamically compress files when they're requested. 提前压缩可以节省服务器的CPU时间,因为它不需要在请求时动态压缩文件。

Just be careful when you deploy files to the server to update both *.css and *.css.gz at the same time, otherwise you may be surprised that you sometimes see old version of the file. 将文件部署到服务器以同时更新*.css*.css.gz时要小心,否则您可能会惊讶于您有时会看到旧版本的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Gulp 对 uglify js 进行 gzip 压缩而不在 dist 文件夹中生成两个文件 - How can I gzip a uglify js with Gulp without generating two file in dist folder 如果文件被服务器gzip压缩,则ajax内容是否被gzip压缩 - Is ajax content gzipped if the file is gzipped by server 如何使用带有 XHR onProgress 函数的压缩/gzipped 内容? - How can I use deflated/gzipped content with an XHR onProgress function? 为什么 Gulp 服务在“服务”任务后退出而没有任何错误消息? - Why Gulp Serve exiting without any error message after 'serve' task? 如何使用Google App Engine为AJAX请求提供gzip压缩内容? - How to serve gzipped content for AJAX requests with Google App Engine? 无法使用Express正确提供压缩文件 - Can't serve gzipped files properly using Express 如何在不使用ng serve的情况下提供Angular 5应用程序? - How can I serve an Angular 5 app without using ng serve? 如何在 Deno Oak 服务器中与动态路由一起提供 Static 内容 - How Can I Serve Static Content Alongside Dynamic Routes in A Deno Oak Server 如何在浏览器中解压缩压缩文件? - How can I unzip a gzipped file in the browser? 在gulp构建期间如何将内容插入文件? - How can I insert content into a file during a gulp build?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM