21285 Posts in 5732 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 903 Views |
-
serve assets from a different hostname

2 August 2009 at 11:41pm Last edited: 2 August 2009 11:42pm
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.jpgdo you have any hint?
thanks,
kochab -
Re: serve assets from a different hostname

6 July 2010 at 8:43am
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 -
Re: serve assets from a different hostname

6 July 2010 at 9:12am Last edited: 6 July 2010 9:13am
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
-
Re: serve assets from a different hostname

6 July 2010 at 9:51am Last edited: 6 July 2010 10:42am
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() methodsapphire/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
| 903 Views | ||
|
Page:
1
|
Go to Top |




