2

I have a fully static website. All images and videos are currently delivered via aws cloudfront.

Now I am thinking of delivering the HTML via cloudfront as well. My concern is that craftCMS routing will always point to the CDN domain.

How do you preview your content without invalidating the cache? I want my clients to immediately see the changes they made without waiting for 30 minutes (cdn cache expiration).

Any ideas?

edit #1:

I am wondering if relative urls for linking between pages has any pit falls.

1 Answer 1

3

That's the dream, isn't it? CDN everywhere, no database hits, etc. How "static" is your site? Unlike other CMS, the great thing about Craft is that you can store information everywhere and pull it out anywhere. That gets to be dynamic content problem nightmare unless you're very diligent about knowing what will happen, for example: "updating this page will also update content on the home page, the blog index, related white papers section, author archives, etc".

Let' say you update the title of one of your main areas that's also part of your navigation: Services becomes Products for example. Now you have to invalidate every single page.

So it can get complex quickly but if you have a very simple site, obviously, the above doesn't apply.

One way to pull this off would be to create a subdomain, like admin.whatever.com. Your admins would login to this URL. As far as Craft is concerned, it'll serve up on whatever domain you need, you just need multiple environment config.php and template trickery.

In your template files, check for the server name.

{% if craft.request.serverName is not "domain.com" %} {% reqireLogin %} {% endif %}

This will prevent non-logged in users from trying to accesses the admin pages, otherwise Craft will serve up your non-CDN on that domain to anyone who hits it (which is not something you want to have happen generally).

Unlike something like WordPress where it actually stores URLs in the database, Craft uses reference tags and builds up the URL on the fly so you generally don't have to worry about relative vs absolute URLs.

The hardest part of this is going to be the cache invalidation. Unlike static files, where you can just upload a new file with a different filename on the CDN to invalidate them, pages are much different. To make matters worse, it can take up to 10-15 minutes for that invalidation to happen on Cloudfront.

I use nginx here for static/microcaching and it works pretty well but the idea is the same. I can share some code I use to invalidate its cache but basically you want to create a plugin that listens on entries.SaveEntry and then tell CloudFront to invalidate the cache after it's saved. Something like:

public function init()
{

    parent::init();
    craft()->on('entries.saveEntry', function (Event $event) {

        $entry = $event->params['entry'];

        if ($entry->url && $entry->enabled) {            

        $url = $entry->url;

            try {

                 // do CloudFront stuff here
                if (!$response->isSuccessful()) {
                    return;
                }               




            } catch (Exception $e) {
                // something happened.                  
            }

        }
    });
}
1
  • Thanks for the comprehensive answer. I am wondering how the 'multiple environment config.php and template trickery' might look like
    – Sven
    Feb 8, 2017 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.