As already mentioned, mod_deflate does this for you, but I think you need to do it manually, as it is an embedded environment.
First of all, you should leave the name of the file foo.js after its gzip.
You should not change anything in your html files. Since the file is still foo.js
In the response header (gzipped) of foo.js you send the header
Content-Encoding: gzip
. foo.js Content-Encoding: gzip, gzipped , .
, , , gzip, , gzipped-,
Accept-Encoding: gzip