How to set the appropriate http headers for gzip

Viewed 3648

I'm using Unity's WebGL and I'm getting this message on the console "You can reduce your startup time if you configure your web server to host .unityweb files using gzip compression." So according to Unity's documentation, I need to add the correct response Headers https://docs.unity3d.com/Manual/webgl-deploying.html.

I found the "express-static-gzip" module, and I tried to do just that, but the warning is still there. Below is the server.

enter image description here

const express = require('express');
const ip = require("ip");
const expressStaticGzip = require('express-static-gzip');
const http = require('http');


const app = express();
const server = http.Server(app);

app.use('/public/Builds/Build/', expressStaticGzip('public/Builds/Build/', {
    customCompressions: [{
        encodingName: "gzip",
        fileExtension: "unityweb"
    }]
}));


// app.use(compression());
app.use(express.static('public'));

server.listen(3000, function(){
    console.log( ":: http://" + ip.address() + "/ ::" );
});

Any ideas? Nick

2 Answers

Many thanks to @d_shiv for his help. I changed the code to the following, and the warning went away.

(you can change gzip with br if you're using brotli)

const express = require('express');
const ip = require("ip");
const http = require('http');


const app = express();
const server = http.Server(app);

app.use(express.static('public', {
    setHeaders: function(res, path) {
        if(path.endsWith(".unityweb")){
            res.set("Content-Encoding", "gzip");
        }
    }
}));

server.listen(3000, function(){
    console.log( ":: http://" + ip.address() + ":3000/ ::" );
});

express-static-gzip does not gzip the files on the fly before serving it. It assumes that you have the normal as well as gzipped versions of the file available on the specified directory. Check the Examples section of documentation here.

In this scenario, if the public/Builds/Build/Builds.wasm.framework.unityweb had to be transferred with gzip compression, you'd need to create a gzipped version by name of public/Builds/Build/Builds.wasm.framework.unityweb.gz. The middleware will automatically scan the folder for all such file pairs where original as well as gzipped versions are available. It will serve the gzipped version when request comes for original file, if the browser supports it.

The customCompressions array should also be skipped since that's enabled by default. The middleware would be registered, something like this:

app.use('/Builds/Build/', expressStaticGzip('public/Builds/Build/'));

Also note that public/ is removed from the middleware path (should be present in the expressStaticGzip path though). This is because your assets are being loaded from path https://{hostname}/Builds/Build/....

If you intend to compress the files on the fly and server it, take a look at compression module. The can be very costly operation for your server though, if possible do the gzipping during build time to create the equivalent .gz files, and continue to use express-static-gzip.

Related