I have discovered what I assume is a bug - or maybe just a slow CDN
I have a webhook set up to build my static website. I have a system for handling previews too.
If I "save and preview" (latest content is loaded), but then make some changes and "save and publish": then this happens:
Using https://client-name.cdn.prismic.io/api/v2. the latest content is not ready yet! I can see the master ref hasn't been updated.
Using https://client-name.prismic.io/api/v2 everything works.
I thought that the "cdn" version could be used for this. But it seems to save the content too slowly, so I'm getting the wrong master ref.
Is this intended behaviour, maybe because I'm using the CDN for something it wasn't intended for?
Hello @MichaelVestergaard, I'll reach out to the relevant team and come back when I have more information.
So, it isn't clear why you experienced those events, but for the CDN, it is always recommended to use it because it is cached, so it's always faster.
The way it works is if the Prismic master
ref is old, then there's a good chance that any request you make is cached. When you create a new
ref, when you publish a change, and your API updates, it clears that cache.
So the first time you make any given request, it's gonna make a call to the server, and it'll run the request and come back. From that point on, if you're using the same
ref and making the same request, it's gonna give you the cached version, and it'll do that for all requests.
So, using the CDN after you've done the first request of any kind, it'll be much faster to use, and it won't consume your bandwidth on the Prismic side.
I understand the benefit of the cache. But a cache is only faster after the first time, right? For a static generated website, I never have more than one API request to get the master ref (so every call is a "first" call that refreshes the cache). So the next request is when something new is published and the webhook is executed.
I am still often getting the wrong/old master ref returned from the API when I use the cdn url. I was hoping you could either fix it or tell me that it's intended behaviour Maybe it has something to do with the preview ref that is being removed at the same time as a new master ref gets generated?
Sure. It's not consistent. Sometimes I get the latest ref, sometimes not. Last few days I haven't had any errors!
1: These are the only queries I run in the webhook:
$ref = $api->master()->getRef(); //get master ref
$modulesTypes = ['frontpage','about','services']; //all document types
$api->query(Predicates::any('document.type', $modulesTypes),['pageSize' => 100, 'orderings' => '[document.last_publication_date desc]', 'ref' => $ref]);
2: I found out because my client didn't always see the latest published content on their website. The steps to (sometimes) reproduce:
a) Edit, save and preview a document.
b) Edit, save, publish and view.
It's like the cdn is slower at saving new content, so it sometimes isn't ready when the webhook fires.
3: I'm not caching, because I only have one api->query. and the content loaded should always be the newest.
I have made a fallback where I'm using the master ref from the webhook data, so I'm not using the master ref returned from the API::get... This has worked so far.
$d = json_decode(trim(file_get_contents("php://input")), true);
if(isset($d["masterRef"])) $ref = $d["masterRef"];
Thanks, I'll share this with the team.