Skip to content

Instantly share code, notes, and snippets.

@svewag
Last active April 26, 2019 06:06
Show Gist options
  • Save svewag/be6a12699970b9ceca106f2f91a0f677 to your computer and use it in GitHub Desktop.
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
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