Closed justintemps closed 3 years ago
I did some more investigating and I believe this is caused by aggressive caching for apps that are being served behind Cloudflare. It would be fine if you could set caching headers as explained here, however those headers in particular are overridden by Next.js so that configuring Cache-Control has no effect. I'm opening a separate issue about that as I think it would solve this issue.
Cloudflare is not the culprit, or at least not the only one. On my local machine, where there is certainly no Cloudflare involved:
_ | next dev |
next start |
---|---|---|
Firefox | preview works | preview works |
Chrome | preview works | preview does not work |
...unless you're saying Prismic itself is using Cloudflare?
@tremby it may not be the only culprit, I can get preview working in Chrome on the server but only for my /
route where Next.js sets
Cache-Control: s-maxage=1, stale-while-revalidate
On dynamic routes like /posts/[:uid]
it uses
Cache-Control: s-maxage=31536000, stale-while-revalidate
Is your server running HTTPS?
I just did my own digging and the culprit in my case is that in next start
mode Next's setPreviewData
function results in some Set-Cookie
headers with the Secure
flag (and SameSite=None
which I'm not sure of the purpose of but mentioning it anyway), while in next dev
mode those cookies are set without the Secure
flag (and with SameSite=Lax
).
Firefox doesn't seem to care (at least for localhost? not sure if an exception or it just doesn't honour Secure
at all), but Chrome gives a warning on both Set-Cookie
attempts:
This Set-Cookie was blocked because it had the "Secure" attribute but was not received over a secure connection.
So in next start
mode without HTTPS the cookies aren't set in Chrome, and so preview mode doesn't work.
Oh, and that's super weird that it'd set different max-age hints for different routes, assuming you didn't ask it to.
It looks like I just needed to set NODE_ENV to development, and now everything is working on my end in both browsers and modes.
Sorry for the noise -- my issue was not the same as yours! My comments here were irrelevant to your bug report; say the word and I'll delete them.
My project isn't in production yet. My client and I were finding this issue on local machines when testing. In production we'll be using HTTPS of course, and so I don't anticipate seeing the same issue. Maybe at that point I'll see the bug you're seeing though. :)
Just ran NODE_ENV=development npm start
.
Ok, so running NODE_ENV=development npm start
also resolves this issue on my local machine, but not in production where requests with the cookie are not reaching the server because they're getting cached in Cloudflare.
So, I've got this find on my end and it turned out there were two issues:
On my local machine
With next start
, Preview Mode worked in Chrome but not FireFox. This is probably because of the Next.js preview cookies were set with a secure flag. In any event, running NODE_ENV=development npm start
fixed it.
On the server
With next start
, Preview Mode only worked on the first request, because afterwards the normal page were getting cached in Cloudflare and successive requests weren't reaching the server. I was able to fix this by setting revalidate
in getStaticProps to 1.
I hope this helps anyone who runs into the same problem.
Setting revalidate
to 1 effectively disables the cache in production which seems like a very bad fix. We are currently also having a very hard time to find a way how to do this right.
Seems like previews are only really doable on a development instance but not on production.
A hacky workaround might be to adjust your preview route's link resolver so that it adds a random query string to the end of the URL.
@webholics I came to the same conclusion, I'm only enabling preview mode on my staging instance. However, you don't have to run next in development mode to do this, you can simply disable caching on your staging instance with an env var or something. I think that's preferable as Next.js behaviour changes a lot between prod and dev modes.
...or to adjust your CDN settings so that it will vary the response based on the preview data cookie. This would be much less hacky. I believe this is possible with Cloudfront. Not sure about Cloudflare.
Actually we use next export
and there the support for doing previews is even worse. So it seems we now need a separately running node server just to be able to support previews.
@webholics I don't think getStaticProps is called on the client, so you would probably have to have that anyway (or a serverless setup that took care of that for you)
true
Actually we use
next export
and there the support for doing previews is even worse
Oh, that's a shame. I thought one of Next's whole things was that the /pages/api/*
things could be turned into lambdas. Can you not do that with next export
...?
@tremby I think Vercel will do this for you without any customization. Otherwise you need a serverless wrapper like next-on-netlify which converts all of your api routes to lambdas.
Damn I guess not, since all the preview route does is set cookies and then redirect you. The static site I'm building may be a whole lot less static than we intended.
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Bug report
The docs state that when preview mode cookies are set via
res.setPreviewData
thengetStaticProps
will be called at request time instead of at build time. But I can't get this to work reliably in production in dynamic routes with getStaticPaths. The cookies are set correctly, but getStaticProps does not get called.Describe the bug
I have a headless CMS (Prismic) that generates a preview url that looks like this:
https://my.website.com/api/preview?token={token}&documentId={documentID}
There I have the /api/preview handler that redirects to the appropriate content and sets the preview cookies
__next_preview_data
and__prerender_bypass
LocalHost/Development All pages are correctly loaded in Preview Mode:
LocalHost/Production All pages are correctly loaded in Preview Mode In FireFox but not in Chrome. In Chrome, static routes appear in Preview mode whereas dynamic routes do not. Preview cookies are set correctly.
Server/Production Only static routes appear in Preview Mode for both FireFox and Chrome. Dynamic routes do not appear in Preview Mode even though preview cookies are set.
To Reproduce
Expected behavior
Preview cookies should result in
getStaticProps
being called at request time.System information