SilverStripe speed up Portfolio Sites with Static Publisher

A common site type that we build are portfolio pages
which mostly get filled once in the beginning and then only periodically, rather rarely.
They also have almost never User interaction from the Frontend.

So we consider them as almost “readonly” sites.

These sites you can easily speed up “dramatically” with the SilverStripe builtin StaticPublisher.
The tweaks you need for this are moderate to easy.

You can read the Docs to get a glimpse of what is possible.
I will just describe the basic setup to make a site static.

Add this to your Page.php, this will tell SilverStripe what do export to the static cache:

/**
* Return a list of all the pages to cache
*/
public function allPagesToCache() {
// Get each page type to define its sub-urls
$urls = array();
// memory intensive depending on number of pages
$pages = SiteTree::get();
foreach ($pages as $page) {
$urls = array_merge($urls, (array) $page->subPagesToCache());
}
return $urls;
}
/**
* Get a list of URLs to cache related to this page
*/
public function subPagesToCache() {
$urls = array();
// add current page
$urls[] = $this->Link();
return $urls;
}

Add this to your _config:

Object::add_extension("SiteTree", "FilesystemPublisher('silverstripe-cache/static/', 'html')");

I placed the cache folder in the cache dir since its already writeable and can be cleared via flush.
But this you can define yourself.
— But notice if you change this path, you also have to change $cacheBaseDir in framework/static-main.php!

Now we can fill up the cache:

http://www.example.com/dev/buildcache?flush=1

EDIT: The below part can be skipped, if you use the method pagesAffectedByChanges(), see comments below.
———————

A bit of a downside is that after you have altered the content in your backend you have to refresh your cache via the above build command.
This might be too much for a client to remember.
There for you could override the admin template:
cms/templates/Includes/CMSPagesController_ContentToolActions.ss
and add a button to call this command.

<div class="cms-actions-row">
<a href="/dev/buildcache" target="_blank" class="ss-ui-button ss-ui-action-constructive" style="float: right">StaticPublisher</a>
<a class="cms-page-add-button ss-ui-button ss-ui-action-constructive" data-icon="add" href="$LinkPageAdd" data-url-addpage="{$LinkPageAdd('?ParentID=%s')}"><% _t('CMSMain.AddNewButton', 'Add new') %></a>
</div>

See:
Silverstripe-StaticPublisher

3 Replies to “SilverStripe speed up Portfolio Sites with Static Publisher”

  1. You don’t need a button to manually rebuilt. Simply define the pagesAffectedByChanges() function on your object which returns the urls that you want to republish when you publish the page

    For instance, a simple case may be to republish just the current page and the parent page

    function pagesAffectedByChanges() {
    return array(
    $this->URLSegment, $this->Parent()->URLSegment
    );
    }
    

    https://github.com/silverstripe-labs/silverstripe-staticpublisher/blob/master/docs/en/StaticPublisher.md

  2. Thanks Will for pointing this out. I didnt know that.
    Great feature.

    I guess your snippet should be like this to get the urls right:

    function pagesAffectedByChanges() {
    return array($this->Link(),$this->Parent()->Link());
    }
    

    When using this method it might be neccessary to have a strategy resp. method for each PageType, depending on pages that might be affected. This can become quite complex compared to just rebuild all after finishing edit.

Leave a Reply

Your email address will not be published. Required fields are marked *