Last active
April 26, 2019 06:06
-
-
Save svewag/be6a12699970b9ceca106f2f91a0f677 to your computer and use it in GitHub Desktop.
after changing the req.url we have to set the Content-Type Header manually, otherwise it is set to application/octet-stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const mime = require('mime') | |
const fs = require('fs') | |
const server = express() | |
server.get(/\.(js|css)$/, (req, res, next) => { | |
if (req.header('Accept-Encoding').includes('br')) { | |
if (fs.existsSync(`${config.path}${req.url}.br`)) { | |
const type = mime.lookup(req.path) | |
const charset = mime.charsets.lookup(type) | |
req.url += '.br' | |
res.set('Content-Encoding', 'br') | |
res.set("Content-Type", type + (charset ? "; charset=" + charset : "")) | |
} | |
} | |
next() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment