I would like to compress js and css files. I am using the [expressStaticGzip][1] module.
As you might have figured out, the response is not being compressed (at all). Neither Brotli nor Gzip is being used.
The (relevant) code in my app.js looks like this
const expressStaticGzip = require('express-static-gzip'); //compression
//-- compression --//
app.use('/public', expressStaticGzip(path.join(__dirname, 'static', 'public'), {
enableBrotli: true, // Enable Brotli compression
orderPreference: ['br', 'gz'], // Prefer Brotli over gzip
}));
app.use(express.static('static'));
My folder structure looks like this:
static
--public
----css
----js
app.js
As far as I understand, any request made to public should compress the files into br or gzip, whichever the client supports.
If we look at the devTools, network tab this is shown:
GET
https://localhost/public/css/main/main.min.css
Response Headers:
Accept-Ranges
bytes
Cache-Control
public, max-age=0
Connection
keep-alive
Content-Length
14333
Content-Type
text/css; charset=UTF-8
Date
Fri, 08 Sep 2023 13:48:30 GMT
ETag
W/"37fd-18a657d2170"
Keep-Alive
timeout=5
Last-Modified
Tue, 05 Sep 2023 13:16:54 GMT
X-Powered-By
Express
Request Headers:
Accept
text/css,*/*;q=0.1
Accept-Encoding
gzip, deflate, br
Accept-Language
en-GB,en;q=0.5
Cache-Control
no-cache
Connection
keep-alive
DNT
1
Host
localhost
Pragma
no-cache
Referer
https://localhost/refering/page
Sec-Fetch-Dest
style
Sec-Fetch-Mode
no-cors
Sec-Fetch-Site
same-origin
User-Agent
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0
GET https://localhost/public/css/main/main.min.css
the GET contains public, so it should be compressed.
Accept-Encoding gzip, deflate, br
gzip and br are accepted as encoding.
So I am wondering why doesn't this work. Any help, tips or questions for clarification are welcome.
Thank you.
Edit
Since I couldn't get it to work, I'm now using compression. Works out of the box.
const compression = require('compression') //compression
app.use(compression());
I would still like to serve Brotli if possible, so I will leave the question open for now, if someone can point me in the right direction. [1]: https://www.npmjs.com/package/express-static-gzip