Preview token expired errors

I'm getting the following result from the API when an expired token is used. Here is the result.

{
  "type":"api_notfound_error",
  "message":"https://[REDACTED].prismic.io/previews/[REDACTED]?websitePreviewId=[REDACTED] not found"
}

I've had an error handler that looks for "This preview token has expired" that had been working, has the API changed?

Hey!

The API hasn't changed and yes, this happens when the token expires. This can happen if the Prismic toolbar is not setup properly because its role is to update the preview token after a change has been made.

  • Have you tried refreshing the page?
  • Which technology are you using in your project and how did you configured the previews in it?

I'm using .NET.

I maintain the kit for this so I am keen to know if the error type api_notfound_error is only returned for expired preview tokens, or if it would be returned for any badly configured API.

I've seen this occur when a user closes the preview tab without exiting preview mode and the preview is subsequently published. Because the exception is thrown server-side there is no way for the toolbar to tidy up the cookie.

I would like to update the kit to catch the exception and use a piece of middleware to remove the cookie and reload the page.

I see, thanks for the information, I've asked the team. I'll get back to you as soon as I have an answer

Thanks

Hello, I've discussed this with the team today and we've agreed to add it to the issue tracker. Also, if you would like to do a collaboration to make it quicker you can submit a PR to the kit and we'll review it as soon as we can.

Thanks

This issue has been closed due to inactivity. Flag to reopen.

@benembery I've reopened this for you now.

This issue has been closed due to inactivity. Flag to reopen.

@benembery I'm just quoting you here to see if I can get more info for you.

Which api response are you describing?

The CSharp Kit sets the ref set based on the preview cookie, if the cookie is expired they the request throws an exception.

This exception has changed.

{
  "type":"api_security_error",
  "message":"This preview token has expired",
  "oauth_initiate":"https://[REDACTED].prismic.io/auth",
  "oauth_token":"https://[REDACTED].prismic.io/auth/token"
}

I should be able handle it correctly if this is the correct response.

HI Ben,

I'm really sorry about how long it took to get this information for you. I suck.

So I confirmed with Seb and Renaud. If any ref starts with https:// and is either expired or invalid you get this error. The API hasn't changed and this has been how expired ref have been returned as long as I've been at the company anyway.

@Phil you definitely do not suck. I appreciate your support, thanks for getting back to me, it wasn't super critical. I now have another little improvement I can make for people using prismic with .NET. :smiley:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Hi, I have suddenly a similar issue. One of my pages on the live server receives the error "This preview token has expired" when trying to fetch data. The same page on the dev server works fine though.

In general, my website exists of pages that have no data from Prismic, pages that source some data from Prismic, but don't work with Preview and thus Preview is not implemented on those pages and lastly some pages that source data from Prismic and have Preview implemented.

The page on which this issue happens does not have Preview implemented. It's a search page - aka to search for blogs. Why would a standard query even check if a preview token that was set on a different page has expired?

Tech Stack is React on Vercel.

Apart from deleting cookies, I'm concerned that customers might have the same issue. Any advice, please?

Thanks,
Goppi

Hi Goppi,

I just want to start off by saying that there are no fears of this being an issue for your customers as the preview cookie only exists in the local browser and is only generated for logged-in Prismic users. So there's no way a customer could encounter this error.

The Prismic toolbar should be enabled globally on your project to work, and it is this toolbar which runs the check. So if you haven't properly exited your preview at some point using the Exit 'X' Preview button, then the cookie remains in the browser then when the content in Prismic changes the check will return this error anywhere in the project in YOUR browser.

So the only solution to make sure you don't see this issue is to make sure you hit the Exit 'X' Preview button every time you finish a preview session.

Thanks.

Hi @Phil ,

I don't quite understand what you are saying in your post - especially as you seem to emphasize on that cookies are stored locally - of course that's the case, but I'm not sure what led you to believe that I think otherwise. In any case, I still have a few question in regards to this issue:

  • I can forward a preview link to someone without them having to log into Prismic and when they open the Preview page, cookies are stored locally. What did you mean by that cookies are only generated for logged-in Prismic users?
  • Based on your post, I gather that it's the Toolbar that creates/manages all Prismic cookies.
    The prismic.js (Toolbar) is loaded on a app level and therefore available on all pages. Does the Toolbar create cookies in any case or only ever if someone opens a Preview link?

If this is only giong to be a potential problem on devices where a user clicked on a Preview link (and didn't close it properly), I can live with it.

Thanks,
Goppi

Hi Goppi,

I was only re-enforcing the understanding that the cookies are only generated by users logged in to Prismic, so a normal website user will not receive a shared preview link. Whereas you or anyone else using Prismic can generate their own preview links.

Does the Toolbar create cookies in any case or only ever if someone opens a Preview link?

Only if someone opens a preview link.

If this is ever only ging to be a potential problemon devides where a user clicked on a Preview link (and didn't close it properly), I can live with it.

This is exactly the case.

Thanks.