Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

serve assets from a different hostname


Reply

5 Posts   1149 Views

Avatar
kochab

2 August 2009 at 11:41pm (Last edited: 2 August 2009 11:42pm), Community Member, 3 Posts

Hi,
in order to maximize parallel download:
http://www.websiteoptimization.com/speed/tweak/parallel/
I would like to serve all the files stored in the /assets/ folder from a different hostname, like static.mywebsite.com (that points to the same host)
In few words I would like that all the articles containing links to the /assets/ folder, be rewritten from:
mywebsite.com/assets/image.jpg
to:
static.mywebsite.com/image.jpg

do you have any hint?

thanks,
kochab

Avatar
Tonyair

6 July 2010 at 8:09am Community Member, 81 Posts

UP!

Avatar
banal

6 July 2010 at 8:43am Community Member, 901 Posts

AFAIK links to assets aren't created using shortcodes, so no luck with that..

But you could still write a custom TextParser that replaces all links in your content.
The page about TextParser apparently no longer on doc.silverstripe.org, but just look at the implementations in sapphire/parsers

Avatar
Martijn

6 July 2010 at 9:12am (Last edited: 6 July 2010 9:13am), Community Member, 271 Posts

I had to hack Image::getTag as well to replace urls in ImageObjects to be able to use $Object.Image etc:

function getTag() {
      if(file_exists("../" . $this->Filename)) {
         $url = $this->URL();
         $title = ($this->Title) ? $this->Title : $this->Filename;
         $url = str_replace('/assets/','http://assets.mydomain.com/assets/',$url);
         return "<img src=\"$url\" alt=\"$title\" />";
      }
   }


I also use a isDev switch in Page_Controller init() to use the static domain only in live mode for css and js :

public function init() {
      parent::init();
      if(Director::isDev()){
         Requirements::css("themes/".SSViewer::current_theme()."/css/blah.css");
         Requirements::Javascript("mysite/javascript/blah.js");
      } else {
         Requirements::css("http://assets.mydomain.com/themes/blah.css");
         Requirements::Javascript("http://assets.mydomain.com/mysite/javascript/blah.js");
      }
   }

Never tried the TextParser. If you try it, pleas post results :)

Avatar
Tonyair

6 July 2010 at 9:51am (Last edited: 6 July 2010 10:42am), Community Member, 81 Posts

Second solution is the best i'll just add some getter and setter functions and it'll be easy to configure assets image domain.

Thx! =)

Image is just extending File
So we can modify getURL function and add Director::assetsURL() method

sapphire/filesystem/File.php:
..
function getURL() {
if(Director::assetsURL()){
return Director::assetsURL() . $this->getFilename();
}else{
return Director::baseURL() . $this->getFilename();
}
}
..

So all static files will be on the other domain, not only images