enhancement for write_asset_file_contents of AssetTagHelper module [patch]

Hi all,

when using stylesheets caching in rails, one might get problems if the stylesheets contain relative urls and the cache file is generated in a different folder than the original one.

Example original: stylesheets/jquery/redmond/index.css cached: stylesheets/cached.all.css

If the stylesheet contains relative links like "url(images/test.png)" it get's resolved like this by the browser:

original: stylesheets/jquery/redmond/images/test.png cached: stylesheets/images/test.png

The attached patch contains a join_asset_file_contents_for_css methode, which gets called by write_asset_file_contents in case the asset is a stylesheet.

Instead of a simple join, it parses the css and updates all urls as needed.

Corin

join_asset_file_contents_for_css.diff (2.83 KB)

It would be great to take advante of: config.action_controller.asset_host = "http://static%d.domain.com"

We are serving static content from different server/location with different URL (just as YSlow describes it) and we have to add absolute URLs in CSS files to serve those files from those domains.

This feature should work same as all other helpers/methods for generating URLs for static content, e.g. image_tag

Martin

The thought of parsing the stylesheets to replace url references seems pretty clunky. Could something like Sass address this problem more cleanly?

Hi Marcin,

may be I didn't understand you correctly, but I think config.action_controller.asset_host is not related with the problems I address with the patch.

If you set asset_host to "/public" for development, urls look like this (resolved by browser):

original: public/stylesheets/jquery/redmond/images/test.png cached: public/stylesheets/images/test.png

After my patch:

original: public/stylesheets/jquery/redmond/images/test.png cached: public/stylesheets/jquery/redmond/images/test.png

If you set asset_host to a different host, like "http://mydata.com:123/data/":

original: http://mydata.com:123/data/stylesheets/jquery/redmond/images/test.png cached: http://mydata.com:123/data/stylesheets/images/test.png

After my patch:

original: http://mydata.com:123/data/stylesheets/jquery/redmond/images/test.png cached: http://mydata.com:123/data/stylesheets/jquery/redmond/images/test.png

So there should be no need at all to set absolute urls in the stylesheets, as the browser does the resolving of the external url's relative to the location of the css.

Corin

Hi Chris,

The main reason for my patch was that external libraries like jquery ui-plugin etc. come with their own finished .css files. Without my patch, caching doesn't work fully transparent as it should. One would also have to adjust these css files manually everytime a new version of the plugin is installed.

Speed shouldn't be affected too, as the parsing is done only once when the cached file is created.

Corin