1. Compressing the file beforehand (then renaming it back to original file extension like file.js, not file.js.gz)
2. Configuring particular files to be treated as compressed ones (so that they get decompressed when downloaded in web-browser)
For that, here are configuration examples for compressing particular files to gzip in particular folders on Apache and nginx web servers.
Header set Content-Encoding gzip
add_header Content-Encoding ‘gzip’;
You can compress particular file types by using wildcards before extension like /var/www/static/*.js or compress all the files in folder by specifying just folder, not particular file.