Selectively disable asset.digest in Rails 3, so external site can include stylesheet
Asked Answered
F

2

7

The aim:

I would like a Tumblr blog to pull CSS from a Rails app's asset directory.

This means I can use SASS, Compass and other sitewide CSS to generate the styling.

This also means if anything is updated in low-level CSS, tumblr.css will be regenerated along with the regular cap deploy, and Tumblr's appearance will change automatically.

The problem:

The Rails 3 asset pipeline adds a hash to the filename, e.g.:

tumblr-c6ec969ce054623163b9404f6c8330e9.css

Therefore the Tumblr template can't include it unless I update the URL manually every time it changes.

Is there a way to either selectively disable asset.digest for one file, or to explicitly generate a single CSS file from SASS, without going through the whole asset pipeline? Or maybe to generate an automatic alias or something?

Finish answered 28/2, 2012 at 21:13 Comment(0)
I
6

You will not have to disable the digests at all.

When Rails precompiles the assets, it adds digests to all the files. However it also creates identical files without digests. So both the following files will load the same css:

  • tumblr-c6ec969ce054623163b9404f6c8330e9.css
  • tumblr.css

If you check the public/assets directory after precompilation you should see both files.

Hope this makes helps.

Ingrid answered 1/4, 2012 at 20:1 Comment(9)
Hmm, this definitely isn't the case with my deployment. There are no files in public/assets without digests. Perhaps I have broken something in pre-compilation then?Finish
Are you running it with the correct environment? Like this: rake assets:precompile RAILS_ENV=productionIngrid
In Capistrano you may have to add set :rails_env, :production to your recipe.Ingrid
Hiya, yes I was running it on the server with RAILS_ENV=production. Capistrano seems to deploy to production by default, but I tried adding the line you suggest. Didn't seem to help. I should clarify: if I delete the shared public/assets directory, Capistrano correctly rebuilds everything in it on the next deploy, but it all has digests appended! Deeply confused as to how I've broken the default behaviour you describe!Finish
What happens if you run rake assets:precompile:nodigest manually?Ingrid
Hi Phil, I have now fixed this by the rather blunt expedient of upgrading to Rails 3.2 and Ruby 1.9.3. So rake assets:precompile:nodigest does work now, and the regular Capistrano deploy generates the right files. Unfortunately I have not been very scientific, so I cannot pinpoint what was broken before. Thanks for all your help though - your answer made it clear that this was an error somewhere in my environment rather than due to how Rails works.Finish
N.B. I have accepted, as under normal circumstances this is the right answer!Finish
@PhilBottomley it seems like rake assets:precompile in Rails 4 doesn't generate files without digests could you help me? #19703968Inocenciainoculable
You seem to be correct: there are some recommendations here: github.com/rails/sprockets-rails/issues/49Ingrid
A
4

In Rails 4 it seems that the asset precompile no longer does both the digest and non-digest filenames, only the filenames with the digest get compiled. The best option now to precompile assets without a digest hash is to use the Non Stupid Digest Assets gem:

https://github.com/alexspeller/non-stupid-digest-assets

Hopefully they add this to the Rails project as a standard feature in Rails 5.

Attending answered 20/5, 2015 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.