Jump to:

23476 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » serve assets from a different hostname

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

Page: 1
Go to End
Author Topic: 1140 Views
  • kochab
    Avatar
    Community Member
    3 Posts

    serve assets from a different hostname Link to this post

    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

  • Tonyair
    Avatar
    Community Member
    81 Posts
  • banal
    Avatar
    Community Member
    901 Posts

    Re: serve assets from a different hostname Link to this post

    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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: serve assets from a different hostname Link to this post

    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

  • Tonyair
    Avatar
    Community Member
    81 Posts

    Re: serve assets from a different hostname Link to this post

    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

    1140 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.