I'm currently trying to make the preview work on a Next.JS website I'm building. It works perfectly when running in dev mode locally but when deployed on Netlify, I get a 404 page (the Next JS default one since I didn't do a custom one yet). When I look up the cookies they are present and seems valid.
I'm running NextJS 9, @prismicio/client 4 and deploying using the @netlify/plugin-nextjs plugin
Welcome to the Prismic Community Forum, and thanks for posting this question
For starters, do you have a custom 404 page in your project? For previews to work, you must include the Preview script on every page of your website, including your 404 page. Netlify includes a default 404 page, which wouldn't have the preview script. If you don't have a custom 404 page, try adding one to your project with the script on it:
Let me know if this helps! If not, we'll try some other troubleshooting.
Saddly it doesn't work. I put the script in _document.tsx and I saw it was there in the default in the default 404 page while inspecting it. It still built a custom page just to test and it didn't worked either. In both cases the preview plugin/tool is there but I still get a 404 page instead of the content I want to see (a new document)
I've spent some time looking into this, and I understand the problem a little more, now. Vercel supports the ~/pages/api/ directory natively, and runs turns your API routes into serverless functions. Netlify doesn't have this support built in. So, rather than putting your preview.js file in the API directory, you need to put it in a Netlify function. I see you have installed Netlify's Next plugin. Have you also created a Netlify function to set your preview cookie?
I found the solution last night and forgot to update the thread sorry.
The preview was working fine for existing pages but I was testing with one new. The solution I found to support the new pages was to set the fallback property for the getStaticPaths to true and manually redirecting to the 404 page in a useEffect if the page doesn't receive data
I'm trying to implement Prismic preview in a Next.js application. I'm using Server-side Rendering with getServerSideProps.
When I run the code locally, the preview works perfectly. However, when deployed into production (running over https) I just get 404 page not found.
I was following the troubleshooting steps here and
the preview script is loaded into the application.
It seems that the issue for @fprovost was setting the fallback property for getStaticPaths. However, I don't use getStaticProps. I use getServerSideProps instead. Is there an equivalent I should check here?
Also, now I'm a bit confused about the cookies. I was checking out this Prismic Next.js example, where the preview works fine but it doesn't use io.prismic.preview cookie at all. It only has __next_preview_data, __prerender_bypass cookies. As far as I understand this two are generic next.js preview cookies while io.prismic.preview is specifically for prismic. Could you please clarify which one is needed for Next.js Prismic preview to work?
Unfortunately I can't use next export, since it's for exporting the app to static HTML, while I have dynamic runtime pages using getServerSideProps.
I tried deploying a test app to Vercel, which works fine. Trying to deploy the same app to Netlify currently fails for me. But I wouldn't focus much on Netlify, since my real project uses Firebase hosting.
So I'll try to understand if it's a Firebase hosting issue or something else.
As I was mentioning, io.prismic.preview cookie is present in the page, which makes me think the /api/preview endpoint is deployed correctly.
Sorry if this is too many details, I'm just out of ideas what could cause the issue. Thanks.
Satenik
I figured out that the issue I'm facing has something to do with the Firebase deployment using Google Cloud Build. Once I deployed the same app on Vercel, it worked fine.
My Firebase hosting configuration somehow blocks the preview API's but using the Cloud Run URL instead works fine (as a temporary solution). My colleague is researching this further and I will post an update once we find the exact issue.
Hey, Phil. Thanks for the updated documentation - I got preview to work with the updated steps for Next. However, I noticed a slight typo on this page:
The dynamic page shows to import and call useUpdatePreviewRef, however the actual export in the file is called useUpdatePreview. Screenshots of the differences below.