Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Bugfix for serving .svgz (gzipped Scalable Vector Graphics) via connect.... #477

Closed
wants to merge 1 commit into from

4 participants

@bf
bf commented

...static. Browsers with Webkit and Gecko rendering engines rely on the "Content-Encoding: gzip" HTTP header in order to properly recognize .svgz files.

This fixes a very annoying "feature" of at least Firefox and Chromium on Linux and Mac, as .SVGZ files are not recognized by the browser as gzipped .SVG files when served without the "Content-Encoding: gzip" HTTP header. As I'd love to serve scalable graphics to my users in a minified version - hence the svgz - this bugged us while serving content from static directories with node.js.

@bf bf Bugfix for serving .svgz (gzipped Scalable Vector Graphics) via conne…
…ct.static. Browsers with Webkit and Gecko rendering engines rely on the "Content-Encoding: Gzip" HTTP header in order to properly recognize .svgz files.
c7dc32c
@tj
Owner
tj commented

this doesn't really seem to be the right way to go about it. images are already compressed, not like you add a content-encoding for them, likewise when serving a tarball etc

@bf
bf commented

I see your point, but it's not as easy as it seems. SVGZ images in particular are just not displayed in both latest Firefox and Chrom(e/ium) when the "Content-Type: gzip" header is missing.

Due to the nature of this problem, there is also a Firefox Bug which has been filed over a year ago regarding this very same issue. The opinion on this topic seems to be that .SVGZ (and .SVG.GZ) need to be served with the gzip header.

I think, that connect.static should prevent their users' from having to research the common case in which connect.static("/dir/") serves all files properly, but suddenly all svgz images are not working. This issue and its facets have been keeping me occupied for months now and I suppose a patch to static is the most user-friendly solution.

@tj
Owner
tj commented

I don't disagree I'm just arguing for the sake of better understanding the issue better from other perspectives. You could also likely add middleware before static(), check the pathname and add it there, so we're not cluttering up static() with a bunch of odd edge-cases

@Voronchuk Voronchuk referenced this pull request in balderdashy/sails
Closed

Serving SVGZ static files #1350

@ivancamilov

just in case someone comes wandering off Google looking for how to do the middleware:

middleware: function (connect) {
            return [
            lrSnippet, // This is from connect-livereload
            pushState(), // This is from connect-pushstate
            function(req, res, next){ // This is our interesting function :)
              if(req.url.indexOf('svgz') != -1) 
                res.setHeader('Content-Encoding', 'gzip');
              next();
            },
            mountFolder(connect, '.tmp'), // This uses static inside.
            mountFolder(connect, 'app')
            ];
          }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 21, 2012
  1. @bf

    Bugfix for serving .svgz (gzipped Scalable Vector Graphics) via conne…

    bf authored
    …ct.static. Browsers with Webkit and Gecko rendering engines rely on the "Content-Encoding: Gzip" HTTP header in order to properly recognize .svgz files.
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 0 deletions.
  1. +9 −0 lib/middleware/static.js
View
9 lib/middleware/static.js
@@ -14,6 +14,7 @@ var fs = require('fs')
, join = path.join
, basename = path.basename
, normalize = path.normalize
+ , extname = path.extname
, utils = require('../utils')
, Buffer = require('buffer').Buffer
, parse = require('url').parse
@@ -152,6 +153,14 @@ var send = exports.send = function(req, res, next, options){
res.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''));
}
res.setHeader('Accept-Ranges', 'bytes');
+
+ // Add mandatory gzip header for svgz files
+ if (type == 'image/svg+xml') {
+ if (extname(path) == '.svgz') {
+ res.setHeader('Content-Encoding', 'gzip');
+ }
+ }
+
// conditional GET support
if (utils.conditionalGET(req)) {
Something went wrong with that request. Please try again.