Webpacker: How does webpacker / rails serve the gzipped production files?

Created on 21 Jul 2017  路  2Comments  路  Source: rails/webpacker

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.

help wanted style loaders

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 .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.

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pioz picture pioz  路  3Comments

naps62 picture naps62  路  3Comments

iChip picture iChip  路  3Comments

ilrock picture ilrock  路  3Comments

FrankFang picture FrankFang  路  3Comments