craigspaeth/nap

How to use gzip

nonameplum opened this issue · 2 comments

Hi,

I think that I missed something becouse when I set gzip: true then my gzipped javascript is not readable for browser

SyntaxError: illegal character

I can see that resulted *.cgz files are without gzip content type header. I tried to add by myself but without success):

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

app.use(function (req, res, next) {
    if(endsWith(req.url, '.cgz')) {
        res.setHeader("Content-Encoding", "gzip");
    }
    return next();
});

I repair a little my code:

app.use(function (req, res, next) {
    var acceptEncoding = req.get('Accept-Encoding');
    var isGzip = false;
    if (acceptEncoding && (acceptEncoding.indexOf("gzip") > -1)) {
        isGzip = true;
    }

    if (isGzip &&
        (endsWith(req.url, '.cgz') || endsWith(req.url, '.jgz'))) {
        console.log(req.url);
        res.set("Content-Encoding", "gzip");
    }
    next();
});

For javascript its is working but for css is not. Css is gzipped but not recognized by browser.

Hmm, at Artsy we upload the generated gzip files to S3 and set Content-Encoding=gzip & Content-Type=text/css in our deploy script. Maybe it's just a matter of setting Content-Type. For serving gzip files directly from the Node server you might want to look into another module such as https://github.com/nateps/connect-gzip. Hopefully that helps. Let me know if you come across any good solutions :)