Not sure if I know understand how this works
developer239 opened this issue · comments
Michal Jarnot commented
I have simple server file:
import path from 'path'
import express from 'express'
import compression from 'compression'
const PORT = process.env.PORT || 8080
const DIST_DIR = path.resolve(__dirname, '..', 'public')
const app = express()
app.use(express.static(DIST_DIR))
app.use(compression())
// Create route for static vendors.js file
app.get('/vendor/vendors.js', (req, res) => {
res.sendFile(`${DIST_DIR}/vendor/vendors.js`)
})
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'public', 'index.html'))
})
app.listen(PORT, (error) => {
if (error) {
console.log(error)
}
console.info('Express is listening on PORT %s.', PORT)
})
But lighthouse keeps saying that compression is not working: http://workbox-webpack-react-pwa-app.herokuapp.com/
What am I doing wrong? Index.html servers app.js
and other stuff - mainly react router. Does that mean that my files are not compressed because they are not served by express server?
Douglas Wilson commented
In Express, the middleware are run from top to bottom. Since you have express.static
before compression
, it means anything served but that will never enter into this module to do any work. Try switching the position of those two and seeing if that does the trick.
Douglas Wilson commented
Closing since I never heard back.
Michal Jarnot commented
Yea thanks it worked. :)