Pergunta

Similar to this post need to send a large 30MB JSON file to the browser, which compresses to 3MB. (yes, yes, it's big and intensive to decompress, but better to serve it once as a static file than as, say, 150 separate AJAX calls to a REST API, and a bit of upfront load time is okay here).

Also like the original post, it's a static file, so can compress it ahead of time (so the server doesn't need to compress it on each request). Further, can serve it from the public directory on the server, which in Node.js / Express.js I have marked as static:

app.use(express.static(__dirname + "/public"));

One commenter replied:

"It is also possible to precompress your JSON and then just tell Apache to serve the files with the appropriate headers to avoid having to recompress all the time."

Question 1: How might I do that in Node/Express, i.e. tell Node to tell the browser the file is in gzip format, so that the browser automatically uncompresses it?

Another answer remarks "...but do check that the browser supports compression ", which raises another question?

Question 2: Do ~most~ modern browsers have the ability to decompress content? Or am I better off just including a javascript library for decompression, get the data via Ajax as a binary text, and always decompress it in my own code rather than browser's native utility?

(For my purpose, it's okay to exclude some old browsers, e.g. dropping IE6 would be okay, dropping support for IE9 would not)

Foi útil?

Solução

The browser should automatically decompress the server response, if there is a Content-Encoding header.

With node.js, you would serve the already compressed file with the header:

Content-Encoding: gzip

I.E:

res.setHeader( "Content-Encoding", "gzip" );

Modern browsers support gzip, for older browsers, check http://www.schroepl.net/projekte/mod_gzip/browser.htm

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top