This post originated from an RSS feed registered with Ruby Buzz
by Guy Naor.
Original Post: Assets Servers, JavaScript and CSS - A Dangerous Mix
Feed Title: Famundo - The Dev Blog
Feed URL: http://devblog.famundo.com/xml/rss/feed.xml
Feed Description: A blog describing the development and related technologies involved in creating famundo.com - a family management sytem written using Ruby On Rails and postgres
To make Famundo load faster, we implemented asset servers - 4 of those - to let the client open more connections simulatenously and so load everything much faster. If you want to see the reasoning, check out this explanation. Rails edge currently implement that as well. We even went a step further, and we can serve the static content either from our data center, or from Amazon's S3. This, in conjunction with packing and compressing on the JS/CSS files gave us a huge speed boost.
But while testing it, we discovered a very interesting problem, that is a result of the JavaScript security model in the browsers. All the regular stuff works with no incident, as we use the same main domain for both the main servers and assets servers, the browsers are happy. But one thing won't work - accessing the actual stylesheets from withting JavaScript. We have code that does something along this lines:
var ss=document.styleSheets[i];
var cssRules=ss.cssRules?ss.cssRules:ss.rules;
We use that to get at some colors stored in the CSS, as we support multiple themes. And yes, we could have used different methods, but this is just a sample of actually accessing the CSS files. This results in the JavaScript error "Access to restricted URI denied". The fix is simple - load the CSS from the same place you load the main content. Or don't access the CSS files directly from JavaScript.
So if you have similar constructs, be warned it might cause errors.