How does webpacker / rails serve the gzipped production files?
It is unclear to me how the .gz files are served.
The default configuration creates .js.gz files but the script tag references .js files.
Is this expected to be handled by the server configuration? Or is there some magical step that the browser performs?
I understand that this isn't an issue but I can't find any documentation on this topic.
Webpacker doesn鈥檛 serve anything in production. You鈥檙e expected to configure your web server to serve files in public/ directly.
Some servers support sending precompressed versions of files with the .gz extension when they鈥檙e available. For example, nginx offers a gzip_static directive. Here鈥檚 a sample nginx site config for a Rails app using Webpacker:
upstream app {
# ...
}
server {
server_name www.example.com;
root /path/to/app/public;
location @app {
proxy_pass http://app;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
try_files $uri @app;
}
location ^~ /packs/ {
gzip_static on;
expires max;
}
}
Given this config, nginx will serve a request to www.example.com by first checking for the existence of a corresponding file in /path/to/app/public/. If no such file exists, the request is proxied to the Rails app. The gzip_static directive tells nginx to prefer to serve precompressed files in public/packs/ when they exist and when the client will accept them.
Great thanks @georgeclaghorn 鉂わ笍
Will document this in README
Most helpful comment
Webpacker doesn鈥檛 serve anything in production. You鈥檙e expected to configure your web server to serve files in
public/directly.Some servers support sending precompressed versions of files with the
.gzextension when they鈥檙e available. For example, nginx offers agzip_staticdirective. Here鈥檚 a sample nginx site config for a Rails app using Webpacker:Given this config, nginx will serve a request to www.example.com by first checking for the existence of a corresponding file in
/path/to/app/public/. If no such file exists, the request is proxied to the Rails app. Thegzip_staticdirective tells nginx to prefer to serve precompressed files inpublic/packs/when they exist and when the client will accept them.