1

I am sending a whole lot of JSON information to jQuery (around 40KB), which could be a lot smaller if the descriptors weren't repeated for every single object. (e.g. 'name': , 'title': 'image': ...)

Usually I send JSON data like this:

reply = json.dumps({'items':itemsArray})         
return HttpResponse(reply, 'mimetype/javascript')

Is there a way in which I can either gzip this for the client (if available), or use a third party utility to translate between shortened versions of object attribute names between server and client side, using the jQuery map() function?

Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139

4 Answers4

3

This answer is outdated and should not be followed. Using a GZip middleware has been shown to compromise encryption. I'll leave the old answer but DO NOT follow this suggestion.


You want the GZip middleware: django.middleware.gzip.GZipMiddleware. Of course it also entirely possible to let the compression be handle by you webserver. An example of this is: mod_deflate

Exelian
  • 5,749
  • 1
  • 30
  • 49
1

GZipping is handled by the webserver and browser. So you should check your webserver and webservers configuration, that it gzips the response.

For the minification: JSON is already a pretty minimal protocol. For still shortening it and its indices you probably won’t find something, as you’ll also have to map it on both sides, server in PHP and client in JavaScript. Adapting existing minifiers also requires attention, as it has to be consistent and map, not only minify, as you’ll have to decode it on the other side again.

Kissaki
  • 8,810
  • 5
  • 40
  • 42
0

I got the following to work for me:

Simply letting nginx do the compression worked much smoother.

I made the following changes to the /etc/nginx/nginx.conf file to "turn on" gzip compression and that was it. Most modern browsers automatically extract (uncompress) gzip'ed data so there isn't anything to do on the client-side.

nginx.conf file:

    http {

        #... other settings ...#

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    }
aero
  • 1,654
  • 1
  • 21
  • 31
0

I had a similar scenario where i was returning 70k points from django to leaflet. The geojson serializer was taking too much time. For me it helped when i returned lat,lng and id as a JsonResponse and then created geometry on client side in leaflet.

user4906240
  • 605
  • 1
  • 6
  • 20