Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gajus opened this issue Mar 4, 2017 · 4 comments
Closed

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

gajus opened this issue Mar 4, 2017 · 4 comments

Comments

@gajus
Copy link

gajus commented Mar 4, 2017

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

@gajus
Copy link
Author

gajus commented Mar 5, 2017

@gajus gajus closed this as completed Mar 5, 2017
@gajus
Copy link
Author

gajus commented May 8, 2017

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.

@gajus gajus reopened this May 8, 2017
@gajus gajus changed the title Add an option to try .gz file Add an option to try a file based on Accept-Encoding May 8, 2017
@gajus
Copy link
Author

gajus commented May 8, 2017

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

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

@gajus
Copy link
Author

gajus commented May 8, 2017

Appears that this is WIP pillarjs/send#108

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

@gajus gajus closed this as completed May 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant