0.01
Low commit activity in last 3 years
A long-lived project that still receives updates
Rails/Rack middleware to enable delivery of non-digest assets with appropriate cache headers.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

Runtime

>= 6.1
 Project Readme

SmartAssets

SmartAssets is a Rack Middleware for Rails that enables delivery of non-digest assets when using Sprockets for the asset pipeline.

It solves the problem of production environment requests for assets (eg: application.css) returning a 404 because they do not contain a digest (eg: application-1c8db23293725a8857e5132d59211909.css).

In general, Rails' default behavior of requiring a digest is a good idea. Serving asset files without a digest can easily result in assets being cached for long periods of time, making them impossible to update. Consider yourself warned.

At the same time, there are legitimate reasons to serve assets without a digest in the name, among them using assets with 404 and other error pages and with 3rd-party applications and sites.

Usage

This middleware works by reading the sprockets manifest file and internally changing the HTTP request to reflect the digested name. If your manifest is out of date, you will still get 404s.

In order to minimize the risks of long caching while using this gem, requests where the requested filename is mutated will set Cache-Control: public,max-age=60.

This can be configured in your environment files (or an initializer). max-age is in seconds. If you've configured Apache, Nginx, Traefik, or any other reverse proxy or load balancer to set Cache-Control, you may need to ensure that configuration doesn't override this.

config.smart_assets.cache_control = 'public,max-age=60'

Caching by browsers and proxies can be disabled entirely with:

config.smart_assets.cache_control = 'max-age=0,no-cache,no-store'

This middleware may also be diabled on a per-environment basis with:

config.smart_assets.serve_non_digest_assets = false

The default is disabled for development and enabled for all other environments.

When this middleware is enabled for a given environment, it automatically enables serving of static files.

Installation

Add this line to your application's Gemfile:

gem 'smart_assets'

And then execute:

$ bundle

Compatibility

Works with sprockets-rails 3.x. propshaft not presently supported.

Tested against the latest Rails, but should work from Rails 6.1 onward.

Contributing

Contributions are welcomed. If unsure about the scope or reach of a potential PR, feel free to open a discussion Issue first (not required though).

Please submit human-written contributions only. No contributions containing output generated by LLMs or other non-deterministic systems are allowed.

  • Code, tests, docs, PRs: must be 100% human written.
  • Issues, security reports: must originally be 100% human written. If not originally in English, an LLM may be used to translate to English provided that this translation is disclosed.

We recognize that opinions about the use of LLMs and non-deterministic systems vary widely. Still, we respectfully ask that you honor this policy when interacting with this project.

Q: May I use LLMs apart from contributions?

All usage, training or otherwise, must respect copyrights. Beyond that, we've found that LLMs are prone to errors and misunderstandings--sometimes obvious, sometimes subtle. Please verify everything before trusting an LLM's output.

Running tests

bundle install
rake [test]

License

MIT