为 Laravel Ajax 调用打开 GZip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17863438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:10:57  来源:igfitidea点击:

Turn on GZip for Laravel Ajax calls

ajaxapache.htaccesslaravelgzip

提问by Traveling Tech Guy

I have a 2 views that make several ajax calls to some controllers. They return JSON results.

我有 2 个视图,可以对某些控制器进行多次 ajax 调用。它们返回 JSON 结果。

Google PageSpeed claimed I should enable it to achieve 89% compression (my page contacts the server every second, to update status).

Google PageSpeed 声称我应该启用它以实现 89% 的压缩(我的页面每秒联系服务器以更新状态)。

I've enabled gzip through the .htaccess at the root the /public subdirectory - meaning all my static resources get compressed. But all my Ajax results do not. Where do I enable it? Do I add another .htaccess somewhere, or enable it in the PHP code itself?

我已经通过 /public 子目录根目录下的 .htaccess 启用了 gzip - 这意味着我所有的静态资源都被压缩了。但是我所有的 Ajax 结果都没有。我在哪里启用它?我是在某处添加另一个 .htaccess,还是在 PHP 代码中启用它?

采纳答案by Traveling Tech Guy

Solved: the .htaccess in the public directory is enough, since everything is served off the index.php page. All I was missing was application/jsonat the DEFLATE line.

已解决:public 目录中的 .htaccess 就足够了,因为所有内容都在 index.php 页面上提供。我所缺少的只是application/json在 DEFLATE 线。

回答by West55

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE application/json
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
</IfModule>

This is what I use

这是我使用的