CDN vs non-CDN endpoints

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.

Hi Pau
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 :slight_smile: 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?

  • What’s the exact query you're running when you see the old master ref?
  • Could you give explain to us a bit more about how you found this issue so we can reproduce it?
  • Are you using any caching system on your side?

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:

Api::get('https://client-name.cdn.prismic.io/api/v2', '...

$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.

Hi there,

hope you are well. We are experiencing the same problem to @MichaelVestergaard 's point 2.
We tried to use masterRef too, we got from webhook's payload to explicitly define the ref when we initialize client with createClient() method but this yielded no results.

Has the masterRef been changed since 2022 November? If so, what's the replacement code?

here's the code snippet:
const client = createClient({ ref: req.body.masterRef });

thanks so much and I look forward to hearing from someone!

Best,
Patty

Hey @patty. We'll proceed to post a summary of the resolution:

The issue was related to the timing of document updates and the propagation of the master ref in the application. When using On-demand revalidation, the app rebuilds the page upon document publication. However, due to the delay in the master ref propagation, the client would receive the previous master ref instead of the latest one, displaying outdated content.

To resolve this issue, switching to Incremental Static Regeneration (ISR) instead of On-demand revalidation is recommended. With ISR, the page is revalidated at specified time intervals, allowing for the rebuilding of the page every X seconds. This ensures that if the client initially receives outdated content, subsequent queries to Prismic will return the current content after the specified time interval.

Implementing ISR addresses the timing issue, and the user will consistently receive the up-to-date content in their application.

Hi Pau. This would not work for static generated sites, because the client needs to see their updates live after publishing, otherwise they cannot trust that it works (and fix typos or similar). At the moment my sites publish and build in 2-4 seconds, then the client can view/preview, which works fine. But they can't wait for an interval (maybe 30 minutes) to see their updates come to live, then they will prefer Wordpress or similar.
The problem is actually the same with the Shopify beta integration field - even if we can setup a webhook in Shopify to announce updates, I can't trigger Prismic to fetch the new product data from Shopify. This makes it a lot harder for clients to use and understand that feature, because they have to wait for the sync interval. If I could manually tell Prismic to fetch the updated data, then my clients could instantly use these in their Prismic documents, like they expect.

Again, I'm a little disappointed that you are not thinking more about the end-users. For me as a freelancer I cannot recommend a system that doesn't work for my clients - even if it's easy and comfortable for myself. I love Prismic, but these issues are really important to my clients and the end-users.