I have a very large json (~30mb) that is gzipped with IIS (size is about ~6mb thanks to that). For an unknown reason, gzip works most of the time, but sometimes it doesn't, I have to refresh several time the page before getting the file in gzip.
An user in China seems to never have the file in gzip format and behind proxies and other stuff, load time is just way too long. (He sent me a print screen showing 12mb downloaded in 4 minutes).
The file is generated with php and displayed on the client side with javascript.
My first (and unique) solution at that time is to gzip by myself the data returned by my query before put in on a file.
I searched and found there are gzcompress
, gzencode
and gzdeflate
on PHP, and most of all gzcompress is supposed to return a zlib
format so I could use the zlib
library on javascript.
PHP Side :
file_put_contents('data/data2.json', gzcompress($res, 9));
file_put_contents is needed as I refresh the same file every 20 minutes.
Javascript side :
$.ajax({
type: 'GET',
url: "data/data2.json",
headers: { "Content-Encoding" : "gzip", "Content-type": "text/plain" },
dataType: "text",
async: false,
success: function(data) {
let inflate = new zlib.inflate(data);
});
Unfortunately that doesn't work.
If I write let inflate = new zlib.inflate(data).toString('utf8');
like the example it returns "[Object object]"
and if I removed the toString part it returns e…s.inflate {}
.
My guess is the encoding between gzcompress
and zlib
is not the same (thanks captain) so zlib can't convert but I can't manage to fix it
TL;DR Why gzip doesn't work every time? How to compress a json on PHP and decompress it on Javascript?