documentcloud / jammit

Industrial Strength Asset Packaging for Rails
http://documentcloud.github.com/jammit/
MIT License
1.16k stars 197 forks source link

MHTML/base_url and asset_host/asset_path + CDN #219

Closed dpehrson closed 12 years ago

dpehrson commented 12 years ago

Howdy,

I'm not really sure how to classify this as I'm not yet sure whether it's a bug, a feature request, or simply me attempting to use jammit beyond the way it was designed, but here goes...

I run a website that

  1. Uses Data-URI/MHTML asset embedding.
  2. Serves it's assets out of a CloudFront distribution with multiple hostnames by using the Rails asset_host configuration.
  3. Busts caches by including part of the deployed commit hash in the URL by using the Rails asset_path configuration.
  4. Calls Jammit.package! via a Rake task on each deploy.

This results in Jammit asset includes as such:

<!--[if (!IE)|(gte IE 8)]><!-->
<link href="http://cdn1/release-01c901/assets/stylesheet-datauri.css" media="all" rel="stylesheet" type="text/css" />
<!--<![endif]-->
<!--[if lte IE 7]>
<link href="http://cdn2/release-01c901/assets/stylesheet-mhtml.css" media="all" rel="stylesheet" type="text/css" />
<![endif]-->

The problem I have is that MHTML documents don't work because:

  1. At the time the Rake task calls Jammit.package! I don't know what the base_url will be because it is determined by the combination of asset_host and asset_path configuration of the MHTML stylesheet itself.
  2. Even if I set a base_url directly to where the stylesheet will be (without the right hostname), the MHTML stylesheet will load, but the embedded assets won't because it appears that Jammit doesn't seem to utilize my asset_host and asset_path configuration options when generating paths, so it has no hope of generating a URL that matches where the MHTML stylesheet is loaded from.

The first problem is something that I can likely find a way to work-around, even if it's hackish, but I'm not sure I can do anything about the second one without changing Jammit itself.

Is there any way I can make this work? If not is this something Jammit sees as "fixable? Or should I start looking at an alternate solution? (I hope not, I love Jammit!)

Thanks in advance, and I hope this isn't an inappropriate place for asking this.

jashkenas commented 12 years ago

Howdy.

You should be able to make this work ... but you'll need to know the complete URL for every deployed asset in advance if you'd like to use MHTML. Simply pass the absolute URL prefix as the --base-url when calling jammit to generate your assets.

That said, the wise thing to do is probably not to bother with MHTML, and just use datauri embedding for modern browsers.

dpehrson commented 12 years ago

Hey there,

Thanks for the quick reply!

The problem is that even if I know what the base-url is (say host0.cdn.com) I'll get a link tag with an href like:

http://host0.cdn.com/release-a1b2c3/stylesheet-mhtml.css

The hostname will come from my asset_host configuration (It'd be up to me to find a way to pass the appropriate hostname to Jammit.package!) The release-XXXXXX comes from my asset_path configuration.

The problem is, any assets embedded in that stylesheet would have the URL:

mhtml:http://host0.cdn.com/assets/stylesheet-mhtml.css?1329086338!1-information-24.png

Which won't work because jammit ignores my asset_path configuration so the URL is incorrect. This is what I think may be the bug/feature request.

Unfortunately while I'm fine abandoning IE6 at this point, I need to continue to support IE7 as it constitutes about 10% of our traffic (we're consumer focused with an older demographic on many of our sites.)

jashkenas commented 12 years ago

The "base url" needs to include /release-a1b2c3 at the time you build your assets. Once the files are written out, how could they possibly know the value of asset_host inside of your booted Rails app?

If you can't know the complete path to your assets ahead of deploy time, then using MHTML is impossible -- no tool can help you.

dpehrson commented 12 years ago

I'm having a head slap moment I think. Thank you.