Error when fetching documents using cache request id and ref

Hi :wave:
I'm reaching out regarding a problem with fetching documents from Prismic. The error happens during our cache revalidation process, where use the doc id from the request that Prismic sends us to get some extra data for that document.

This setup was working until a couple of weeks ago but has since stopped and we're not sure what changed.

Our code looked like this (simplified for the sake of brevity):

export async function POST(request: NextRequest) {
    const payload = await request.json() 
    // payload is the body of the request Prismic sends to this endpoint, relevant data below
    // {
    //   documents: ['updatedDocumentId'],
    //   domain: 'findmypast-titan',
    //   apiUrl: 'https://findmypast-titan.prismic.io/api'
    // }
    const client = createClient()
    const doc = await client.getByID(payload.documents[0], {
      fetchOptions: { cache: 'no-store' },
    })

    switch (doc.type) {
      case 'authors':
      case 'blog_article':
      ...

      default:
        revalidateTag(doc.id)
        break
    }

    return NextResponse.json({ revalidated: true, now: Date.now() })
}

And we started getting "no documents were returned" on the getByID function recently.

We've tried adding the ref that comes in that payload to the getByID options but it had no effect.

const doc = await client.getByID(payload.documents[0], {
      ref: payload.masterRef,
      fetchOptions: { cache: 'no-store' },
    })

We've also noticed that the same document id works if we paste it into a page builder URL, so it we know it's correct.

Example:
id: Zo6OmBIAACkAGPLA
:white_check_mark: Page editor URL for this document: https://findmypast-titan.prismic.io/builder/pages/Zo6OmBIAACkAGPLA?fallback={fallbackValue}&s=published
:x: Query URL for this document (generated by JS client): https://findmypast-titan.cdn.prismic.io/api/v2/documents/search?q=%5B%5Bat%28document.id%2C+%22Zo6OmBIAACkAGPLA%22%29%5D%5D&pageSize=1&ref={refValue}&access_token={accessToken}

Hi Eric,

The most likely sounds like a stale master ref caused by Next.js caching. This thread goes into that issue a bit and suggests some solutions.

You find the latest master ref here and compare it with what you're seeing in your project to confirm this is the problem.
https://findmypast-titan.cdn.prismic.io/api/v2

Thanks.