rstacruz / sinatra-assetpack

Package your assets transparently in Sinatra.
http://ricostacruz.com/sinatra-assetpack/
MIT License
542 stars 97 forks source link

Cache busting does not work when using absolute path on the serve from parameter #172

Open sashee opened 10 years ago

sashee commented 10 years ago

I have an app that uses absolute path (it loads some static files (css/js/fonts) from a gem) and noticed that cache busting is not working this way. The serving and the concatenation is working fine, but I'm getting just app.js and not app.hash.js (and also for the css).

I've traced back to: https://github.com/rstacruz/sinatra-assetpack/blob/master/lib/sinatra/assetpack/options.rb#L264 . Here the absolute path (local_path) is joined to the app root, effectively making /home/user/... to /home/user/.../home/user/... .

I ended up using Pathname.new(path).relative_path_from(Pathname.new(app.root)).to_s to relativize the absolute path from the root, but I think it should work this way.

jsignanini commented 9 years ago

Could you please further explain how you managed to get around this issue?

sashee commented 9 years ago

I have a gem that has some assets which I want to include in the app.js and app.css. I find the assets using:

Gem::Specification.find_by_name(gemname).gem_dir+'/assets/javascripts'

It gives me an absolute url, like:

 /home/sashee/.rvm/gems/.../assets/javascripts

Using this absolute url like this:

serve '/js',       from: abs_url

does not work.

The workaround is to relativize it to app.root: This:

Pathname.new(abs_url).relative_path_from(Pathname.new(app.root)).to_s

makes the abs_url to like this:

../../../.rvm/gems/.../assets/javascripts

And using this url on the serve works fine.

cernyjakub commented 9 years ago

I have come into the same problem, your proposed solution works for me. Thanks!

Will try to make a patch later on...