NextJS preview works locally but not on Netlify

Hi,

I’m using NextJS with Sanity.io on https://mambu-rebuild.netlify.app/ (Live site: https://www.mambu.com/). We have preview set up in accordance with the NextJS/Sanity demo (next.js/examples/cms-sanity at canary · vercel/next.js · GitHub). It was working up until last Friday but stopped at the beginning of this week. We did a bunch of work towards the end of the week/early this week and so I initially assumed we had changed something to cause it to break and rolled back to a prior commit before all the potentially breaking changes. However, I expected that the preview would start working again once rolled back and it hasn’t. This is a quick run down of my experience in case anyone has any ideas that might help identify the problem:

  • Hitting the preview api route sets __prerender_bypass & __next_preview_data cookies as expected and redirects correctly;
  • However, despite the cookies being set preview mode does not kick in and therefore live updates do not show;
  • The preview variable logs false in getStaticProps;
  • We are using the next essentials plugin and have set the next.config.js target to ‘serverless’;
  • Everything works perfectly on my local server i.e. the cookies are set, the app enters preview mode and all live updates are visible.

The cookies are being set as HttpOnly, Secure, SameSite=None. I initially suspected that one of these settings might be the cause but manually manipulating those cookies in the api preview function did not help;

res.setPreviewData({})

const previous = res.getHeader('Set-Cookie')
previous.forEach((cookie, index) => {
  previous[index] = cookie
    .replace('SameSite=None', 'SameSite=Lax')
    .replace('HttpOnly', '')
    .replace('Secure', '')
})

res.setHeader(`Set-Cookie`, previous)

I’m at a loss for what else to try now. I’d be grateful for any ideas.

Thanks

2 Likes

UPDATE: I just tested our site and it seems to be working once again as of 04/30 6.20am PST. Yesterday before it stopped working for good, I noticed a similar spotty behavior throughout the day. I will report again throughout the day today.


We are experiencing the same issue. For us, it seems to have started yesterday despite no code changes or package updates either on nextjs or next-on-netlify.

We are seeing both nextjs cookies being set and the redirect work, but preview is coming as undefined on getStaticProps. This problem is only happening in production. Preview mode works fine in both development mode and in local production when running and serving a local build via netlify build.

We have since tried, upgrading next-on-netlify to 3.2.0 which did not help. We also tried going back a few days to deploys we know were 100% working and these are now not working either.

I have a feeling that this issue could be related to this other issue function cookies, but thats just a wild guess.

This issue is concerning as we were about to go live net week with a new project.

Thanks for any help.

JP

1 Like

howdy @jpprietobaez ! sorry you had some instability. Yesterday we were alerted to a bug that seems connected to what you were seeing:

the good news is we were able to roll out a fix for this issue, and we understand the cause of the problem, so this should not happen again. This explains why you saw some issues that were then mysteriously fixed (its cause we fixed 'em~)

if you do see problems like this re-occur, please do let us know right away and we will investigate, but we feel confident this was a one-time thing.

3 Likes

thanks for the explanation @perry! much appreciated. We were going crazy thinking it was something we had done at our end.

So, today we certainly saw some improvement compared to yesterday, but we did have some odd behaviors this afternoon (PST). Things seems to be working at the moment, but it would be great to get some reassurance that you guys are still monitoring this issue.

I will keep you posted during the weekend.

Once again thanks for the response.
JP

1 Like

Thanks, we ended up deploying the live preview to Vercel but I’ve just checked staging and everything seems to be up and running again :partying_face:.

Thanks for letting us know - do reach out again as soon as possible if you see something like this re-occur.