Preview cookies expiring

Hi @Phil !

Sorry for the necromancy, but i’ve been seeing this happening lately with one of the prismic pages we manage. I was wondering, is there a way to reproduce this issue by adjusting cookies etc? We’ve introduced a more user-friendly way of detecting and informing about this on the frontend but replicating the issues reliably is very tricky :slight_smile: any help on this would be appreciated.

Hi korneliusz,

What you are seeing is expected behavior and there has not been a recent API change around preview expiration.

Here is what is happening under the hood.

When a user opens a Prismic preview, a preview cookie is set in the browser. That cookie points to a preview token. Once that preview session expires, the token behind that cookie is no longer valid. At that point, the API no longer returns the older "This preview token has expired" message and instead responds with the api_notfound_error you are seeing.

The important part is how the preview session is exited.

If the user clicks the X button on the preview popup, the preview cookie is properly cleared and everything works as expected.

If they do not click the X and just keep navigating, closing the tab, or returning later, the stale preview cookie stays in the browser. From that point on, every API call will keep trying to resolve an expired preview token, which leads to this error.

That explains why it feels intermittent and hard to reproduce.

To reproduce this reliably:
• Open a preview
• Let the preview token expire
• Do not click the X on the preview popup
• Reload or revisit the page later

At that stage, the only way to recover is:
• Either clicking the X if the preview bar is still visible
• Or manually clearing cookies for the repository domain in the browser settings

Your frontend approach of detecting this and showing a friendly message is a good solution. The key takeaway is that this is cookie related, not an API change, and clearing the preview cookie resolves it.

Is there a way to resolve this problem live instead of having the user see an error screen?

Hi Ben,

As Phil mentioned above, this happens when the preview cookie remains after the preview token has expired.

There isn’t a way to refresh that token, but you can handle the expired-preview state on your frontend. For example, you could detect it and give the user a friendlier way to exit preview and clear the stale cookie instead of leaving them on the error screen.

Thanks Pau.

I'm finding it hard to replicate the error so I can work out how to detect it. Would you be able to provide some advice on how I can identify this error?

Thanks.

Hi Ben,

Yes, this can be resolved by clearing the stale preview cookie from the browser. As mentioned in the thread, this can happen when a preview expires without being closed using the X in the preview toolbar, leaving the expired preview cookie behind.

In Chrome, you can check and clear the cookies for the affected site:

  1. Open the website where you're seeing the preview error.

  2. Click the icon to the left of the URL in the address bar.

  3. Select Cookies and site dataManage on-device site data.

  4. Remove the stored data/cookies for the site.

  5. Reload the page.

Alternatively, you can go to Chrome Settings → Privacy and security → Third-party cookies → See all site data and permissions, search for the affected domain, and delete its stored data.

After clearing the cookie and refreshing, the expired preview token should no longer be used.

Thanks.