开发者

Any way to chunk gzip with Apache and PHP

开发者 https://www.devze.com 2022-12-31 13:42 出处:网络
I have a web application on a site that takes a while (~10 seconds) to complete a portion of the page near the bottom - it has been as optimized as it can be, and caching is not an option.

I have a web application on a site that takes a while (~10 seconds) to complete a portion of the page near the bottom - it has been as optimized as it can be, and caching is not an option.

We have compression enabled on the server via an .htaccess directive SetOutputFilter DEFLATE the problem is this causes the whole page to be held until completion before it starts outputting to the user, this is not optimal as the user sees nothing until the page completes.

I have also tried it via the php ob_start("ob_gzhandler"); method.

Currently I have a <FilesMatch > in my .htaccess restricting this specific script from being compressed.

Basically my question is this - Is the开发者_如何学编程re a way to say chunk gzip or deflate so that the user gets it in pieces, so they can see that the page has begun loading?


I would say: no. I think there is now way provided by HTTP.


If you are using the ob_start("ob_gzhandler") method, you can do this - you need to look at the flush and ob_flush functions.

Some sample code - try loading with curl, or use fiddler to inspect the actual http responses

<?php
ob_start('ob_gzhandler');
print "chunk 1";
ob_flush();
flush();
sleep(2);
print "chunk 2";
ob_end_flush();

Unfortunately, browsers don't seem to display this in chunks - I think this is because the data of each chunk is too small. You can verify this effect by calling wget -O - -q http://chunktest/chunktest.php on your test file.

There are some more useful resources here


If the page is that long of a load time, the creative way to handle it is to use a very quick loading page with an ajax call to that long-loading content on the page. We do this for the pages that pull detailed member usage statistics... Other sites, like Adsense for example, do this on their reports page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号