Currently, I'm testing the preview function of Prismic in my dev environment on my Next.js site. I included all scripts as described in the docs and basically, it works fine, and I can preview the page. But when I'm seeing the document that is in draft on the page I'm getting after 1 or 2 seconds the following erros:
Uncaught (in promise) Error: Failed to load script: /_next/static/chunks/pages/api/exit-preview.js
at HTMLScriptElement.script.onerror (route-loader.js:104:36)
"localhost:3000" is my dev environment, so that's fine. I also check that the route "/api/exit-preview" exists, and it does.
After the errors in the console and from Next.js appears, I'm getting redirected to the 404 page of my site.
Meanwhile I did some testing and found out that the problem is caused by the useUpdatePreviewRef function that runs every second. If I comment that out, it works all fine.
I copied that file from your docs, so it's the same version. The reason why I'm getting redirected to the 404 page is because previewCookieRef in this file is null.
Do you have an idea what could cause this?
UPDATE: previewCookieRef is null because previewCookieObject is undefined and used to declare previewCookieRef
In my package.json it's declared as latest. I checked the version with npm outdated and it was v5.1.0. After updating, it's now at v6.1.0. But now I'm getting an error in my Prismic config file TypeError: Cannot read property 'client' of undefined which means that Prismic is not defined in that file (I didn't change anything).
The latest version of @prismicio/client is V6, and it has a different method to initiate client and queries the API than @prismicio/client V5. We highly recommend updating your project with V6, Although our Next.js documentation is using V5 so far (Our documentation team will update it soon).
It is your choice to use any version until our documentation is updated, but we provide support in both versions. You can again install v5.1.0 and share a project file with me. I'd be happy to look into it.
@feicht.marco I am glad that you have migrated your project to client v6. But our docs are still using the old client version.
You can check my response in this thread, where I have shared my project's zip file. It might be helpful for you.
Also, you can check the V6 migration guide on how to enable auto previews.
The problem was also that the cookie could not be read because my repo name was not declared as a public env variable. That's why the useUpdatePreviewRef always failed.