user3476614
user3476614

Reputation: 607

SyntaxError: illegal character is thrown only in firefox when serving the files with gzip

I use webpack to bundle my application to bundle.gzip using the compress plugin.

new CompressionPlugin({
  asset: '[path].gz[query]',
  algorithm: 'gzip',
  test: /\.js$|\.css$|\.html$/,
  threshold: 10240,
  minRatio: 0.8,
}),

And then I have an express server that serves the everything web pack has bundled and I add content-encoding to the response.

const path = require('path')
const express = require('express')

const app = express()
const server = require('http').createServer(app)

app.get('*.js', (req, res, next) => {
    req.url = `${req.url}.gz`
    res.set('Content-Encoding', 'gzip')
    next()
})

app.use(express.static(path.resolve(__dirname, 'dist')))
app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'dist/index.html'))
})
// eslint-disable-next-line
console.log("Server listening on port 8500")
server.listen(8500)

This works well in every browser except for firefox which when I open the console see this.

enter image description here

What could the problem be I think the problem has something to do with the content-encoding

Upvotes: 4

Views: 2666

Answers (1)

Kushagra Vaish
Kushagra Vaish

Reputation: 76

You need to set Content-Type for the responses.

// For JS
app.get('*.js', function(req, res, next) {
  req.url = req.url + '.gz';
  res.set('Content-Encoding', 'gzip');
  res.set('Content-Type', 'text/javascript');
  next();
});

// For CSS
app.get('*.css', function(req, res, next) {
  req.url = req.url + '.gz';
  res.set('Content-Encoding', 'gzip');
  res.set('Content-Type', 'text/css');
  next();
});

Upvotes: 6

Related Questions