VPS server configuration

How to use gzip bundle.js in reactJS server side rendering

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #120
    Murali
    Keymaster

    Below is the code to run reactjs server side rendering with gzipped bundle.js file,

    Below code gives response headers with gzip which will load website/webapp faster using gzipped javascript and css files. Below code needs to be in expressjs server side starting script

    app.get('*.js', function(req, res, next) {
      req.url = req.url + '.gz';
      res.set('Content-Encoding', 'gzip');
      res.set('Content-Type', 'text/javascript');
      res.set('Access-Control-Allow-Origin', '*');
      res.set('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type,Accept, Authortization');  
      res.set('Acces-Control-Allow-Methods','GET, POST, PATCH, DELETE');  
      next();
     });
    
     app.get('*.css', function(req, res, next) {
      req.url = req.url + '.gz';
      res.set('Content-Encoding', 'gzip');
      res.set('Content-Type', 'text/css');
      res.set('Access-Control-Allow-Origin', '*');
      res.set('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type,Accept, Authortization');  
      res.set('Acces-Control-Allow-Methods','GET, POST, PATCH, DELETE');  
      next();
     });
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.