Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to enhance assets:precompile to provide compressed assets #161

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ On the other hand, if you're already bundling JavaScript and CSS through a Node-
But for greenfield apps using the default import-map approach, Propshaft can also work well, if you're able to deal with vanilla CSS.


## Providing compressed versions of assets

If you're not using a CDN that will automatically serve compressed versions of the compiled assets, you may want your web server to do so. You could let your web server compress assets on the fly, or to minimize overhead you could configure it to serve already-compressed files from disk (e.g. through NGINX's `gzip_static` directive).
In order to provide those compressed asset files (if your existing build configuration doesn't already create them), you can enhance the `assets:precompile` rake task to create the compressed files. For example, to create Brotli-compressed versions of each file, you could add this to your application (e.g. inside an initializer):

```rb
Rake::Task["assets:precompile"].enhance do
assembly = Rails.application.assets
output_path = assembly.config.output_path

assembly.load_path.assets.each do |asset|
asset_path = output_path.join(asset.digested_path)
compressed_path = output_path.join(asset.digested_path.to_s + ".br")

unless compressed_path.exist?
Propshaft.logger.info "Compressing #{asset.digested_path}"
`brotli #{asset_path} -o #{compressed_path}`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using https://ruby-doc.org/3.2.0/exts/zlib/Zlib/GzipWriter.html instead of Brotli since more web servers support GZip out of the box vs Brotli? Then the docs could act as a copy / paste'able production ready example that would work for most folks.

Perhaps the brotli ... line could even remain commented out to show how you could do it for both?

end
end
end if Rake::Task.task_defined?("assets:precompile")
```


## Will Propshaft replace Sprockets as the Rails default?

Most likely, but Sprockets need to be supported as well for a long time to come. Plenty of apps and gems were built on Sprocket features, and they won't be migrating soon. Still working out the compatibility story. This is very much beta software at the moment.
Expand Down