Hello,
I’m improving a nuxt.js website I naively deployed to Netlify without leveraging the full potential of a cache-layer…
I want to setup my headless Kirby so it purges the cache whenever I update or create a page. In my config I’ve put this together to make a call to the Netlify api for a purge:
return: [
'hooks' => [
'page.update:after' => function ($page) {
if (env('KIRBY_ENV') !== 'production') {
throw new Exception('Page updated in ' . env('KIRBY_ENV'), 0);
return;
}
$this->purgeNetlifyCache([env('NETLIFY_API_KEY')]);
},
'page.create:after' => function ($page) {
if (env('KIRBY_ENV') !== 'production') {
throw new Exception('Page created in ' . env('KIRBY_ENV'), 0);
return;
}
$this->purgeNetlifyCache([env('NETLIFY_API_KEY')]);
},
]
]
function purgeNetlifyCache($tags)
{
$SITE_ID = env('NETLIFY_SITE_ID');
$endpoint = `https://api.netlify.com/api/v1/sites/$SITE_ID/purge`;
$apiKey = 'YOUR_NETLIFY_API_KEY';
$data = json_encode(['tags' => $tags]);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
}
]
Since I’ve never worked with hooks I’m not quite sure what will happen… Especially since I don’t want to mess things up… I’m still a bit scared about caching