expressjs / serve-static

Serve static files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add an option to try a file based on Accept-Encoding

gajus opened this issue · comments

My webpack configuration compiles a .gz version of the scripts, though only if the compression reduces the file size, meaning that some .js scripts will have .js.gz counterpart, some will not.

I am able to tell nginx to try .gz in case of .js extension. Though, how'd I tell nginx to fallback to .js if .js.gz does not exist?

app.get('*.js', (req, res, next) => {
  // eslint-disable-next-line operator-assignment
  req.url = req.url + '.gz';

  res.set('Content-Encoding', 'gzip');

  next();
});

To complicate things further, I am using serve-static.

In nginx I'd be able to achieve this using:

try_files $uri.gz $uri @404

Repost: http://stackoverflow.com/questions/42602053/how-to-try-a-file-and-fallback-to-another-file

Turns out I did not read the documentation properly:

Set file extension fallbacks. When set, if a file is not found, the given extensions will be added to the file name and search for. The first that exists will be served. Example: ['html', 'htm'].

i.e. serve-static does not attempt to see if gz file exists first; it fallbacks to .gz if the original file does not exist.

What I'd like is:

If client-sends a request with header Accept-Encoding:gzip, deflate, sdch, br, I'd like serve-static to try load files specific to those encodings, e.g.

serveStatic(..., {
  tryEncoding: {
    br: '.br'
  }
});

This configuration would make serve-static first try serve requested-file.js.br and then fallback to requested-file.js.

Looks like there is an extension of serve-static that enables this functionality.

https://github.com/tkoenig89/express-static-gzip

Appears that this is WIP pillarjs/send#108

(The PR is held because of jshttp/negotiator#49.)