views:

187

answers:

1

I recently started serving static assets (mainly images) from an asset host for my Rails project.

A small portion of my images are not being served from the asset host because they are displayed using the CSS background-image property rather than image_tag

Is there are clean workaround for this? I'd rather not create a "stylesheets" controller because I'm using the asset-packager plugin and would like to preserve this functionality.

Thanks! Moe

A: 

You should be able to put the full path in the url inside your CSS:

background:url({asset_domain}/images/image.png) repeat-x left top;
Toby Hede
I've thought about doing that. But, one of the reasons I setup the CDN was so I could use multiple hostnames in order to parallelize requests. Also, hardcoding the asset domain might cause some issues in my dev environment.
Moe
Well, there is a sweet spot between multiple domains and the cost for domain lookups - so perhaps having the css images on a single domain is OK? As for dev, could you use your hosts file to map the request to the local domain?
Toby Hede
Actually - you could add some smarts to asset packager to set this up. Inject the domain name into the CSS as part of the rake task?
Toby Hede
Super helpful! Of the four subdomains I have, I'll dedicate one for the CSS background images and then use the remaining three for non `background:url` images. Thanks Toby.
Moe
Oh, and I'll use a rake task for a creating assigning the asset subdomain within the css.
Moe