Staticache not working on posts

Hey folks,

I have an issue where my cache isn’t working for post because of a model I use. I remove the /blog/ part of my posts with a model (see this forum thread for details). Since then I’ve changed url() for cleanurl() so wherever I call cleanurl it gives me the url without /blog/.

That works great, but because of this model, caching isn’t working. If I visit my homepage, I see the comment at the bottom of the html that confirms it’s cached. If I go to a “cleanurl” there’s no cache. However, if I add /blog/ to the url, I see the cache.

Here’s an example:
No cache: Mastodon Is DDoSing Me | Kev Quirk
Cache: Mastodon Is DDoSing Me | Kev Quirk

So I now either need to fix the caching issue so that cleanurls are cached (my preferred method) or somehow revert the entire cleanurl change and go back to using /blog/ in my URLs. If go down this route though, I’ll need a way to automatically redirect children of the blog page from the cleanurl to the /blog/ url.

Any advice is very welcome at this point.

Thanks,

Kev

Instead of creating a new method for the url, try overwriting the url method in a page model

I got some time away from the keyboard, walked the dogs and cleared my head. Came back, read this, and knew exactly the change I needed to make. Thanks Sonja.

I’ve just gone to write a new post, but I can no longer preview drafts, I get a 404. I assume it’s something to do with the edited route. Here’s what it looks like:

// Make the non /blog/ URLs work
        [
            'pattern' => '(:any)',
            'action'  => function($uid) {
                $page = page($uid);
                if(!$page) $page = page('blog/' . $uid);
                if(!$page) $page = site()->errorPage();
                return site()->visit($page);
            }
        ],
        [
            'pattern' => 'blog/(:any)',
            'action'  => function($uid) {
                go($uid);
            }
        ],

Is there something I can do to exclude drafts from this route, or (better yet) include them in it?

Tried this solution but it didn’t work for me.

Thanks.

Could you please post exactly what you tried?

Sorry, yeah. I changed

 $page = page($uid);

to

$page = kirby()->page($uid);

Did you also change that for this second line

1 Like

Doh! That worked, thanks Sonja.