-2

My web application sends JSON files to client. These files can be relatively big.

Can I compress them on server-side and receive uncompressed on client side transparently?

For example, if I write

$.getJSON( "ajax/test.json", function( data ) {
...
});

can I just have ajax/test.json gzipped and set content-encoding to gzip?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • 1
    Yes. Typically, this is a configuration item for your webserver, you don't have to do anything programmatically or change your files. – pvg Mar 29 '17 at 13:58
  • So, if I just have stati JSON laying on server (say, AWS) it is compressed most probably? – Suzan Cioc Mar 29 '17 at 14:26
  • 1
    Maybe, depends how you set up your server. It's trivial to check by looking at the network tab in the browser's dev tools. – pvg Mar 29 '17 at 14:27

1 Answers1

2

You have not specified any information about your server-side setup.

If you are generating the json on the fly, then you should check if the request send by the browser contains Accept-Encoding: gzip header. If yes, then add header Content-Encoding: gzip to the response and just sent gzip compressed data.

If the json files are static you should configure application/json mime type as compressible in your webserver configuration, or better yet, precompress it, store both file.json and file.json.gz on your webserver and then configure conditional rewrite and headers, so the compressed file is served only to clients that supports compression. Search/Ask ServerFault how to do this on your webserver.

SWilk
  • 3,261
  • 8
  • 30
  • 51