I have a setup in my LemonSqueezy plugin that generates pages on disk and also alows mixing local content with the api data. At the moment the virtual pages are unlisted but all pages get updated when the API is called after the cache has been flushed. This could be slow with 100s of products.
What would be the cleanest way to check the page exists already and only update the page if the data from the api has changed since the page was created. This would dramatically speed things up in the panel.
My model looks like this currently
<?php
class ShopPage extends Page
{
public function subpages()
{
return Pages::factory($this->inventory()['children'], $this);
}
public function children()
{
site()->products(null);
$apiCache = kirby()->cache('hashandsalt.lemonsqueezy.products');
$apiData = $apiCache->get('hashandsalt.lemonsqueezy.products');
foreach ($apiData['data'] as $key => $productitem) {
if ($productitem['attributes']['status'] === 'published') {
$slug = $productitem['attributes']['slug'];
$page = $this->subpages()->find($slug);
$lemonSquezyProduct = [
'title' => $productitem['attributes']['name'],
'description' => $productitem['attributes']['description'],
'productstatus' => $productitem['attributes']['status'],
'thumburl' => $productitem['attributes']['thumb_url'],
'largethumburl' => $productitem['attributes']['large_thumb_url'],
'price' => $productitem['attributes']['price'],
'formattedprice' => $productitem['attributes']['price_formatted'],
'buynowurl' => $productitem['attributes']['buy_now_url'],
'seotitle' => $page ? $page->seotitle()->value() : null,
'seometa' => $page ? $page->seotitle()->value() : null,
'shareimage' => $page ? $page->shareimage()->value() : null,
'seotags' => $page ? $page->seotags()->value() : null,
'thumbnail' => $page ? $page->thumbnail()->value() : null,
'productdemo' => $page ? $page->productdemo()->value() : null,
'detail' => $page ? $page->detail()->value() : null,
'install' => $page ? $page->install()->value() : null,
];
$pages[] = [
'slug' => $productitem['attributes']['slug'],
'content' => $lemonSquezyProduct,
'template' => kirby()->option('hashandsalt.lemonsqueezy.template'),
'model' => kirby()->option('hashandsalt.model'),
'files' => $page ? $page->files()->toArray() : null,
];
}
}
return Pages::factory($pages, $this);
}
}