Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview does not work for existing, static paths #11298

Closed
pex opened this issue Mar 23, 2020 · 9 comments
Closed

Preview does not work for existing, static paths #11298

pex opened this issue Mar 23, 2020 · 9 comments
Labels
please add a complete reproduction Please add a complete reproduction.

Comments

@pex
Copy link
Contributor

pex commented Mar 23, 2020

Bug report

Describe the bug

When requesting a static path generated by getStaticPaths with preview mode on (by using setPreviewData),

  • in dev getStaticProps is called but context.preview and context.previewData are undefined.
  • in production getStaticProps is never called.

A common scenario for this bug to occur is when:

  • Having an existing, published, and statically built article in a headless CMS
  • Then editing the contents without changing the slug
  • And previewing the changes

To Reproduce

  1. Setup https://github.com/zeit/next.js/tree/canary/examples/cms-datocms
  2. Publish and build a blog post
  3. Change the article
  4. Preview changes
  5. No changes will be visible

Expected behavior

Refering to the documention, I'd expect that (...)

If you request a page which has getStaticProps with the preview mode cookies set (via res.setPreviewData), then getStaticProps will be called at request time (instead of at build time).

(...) also for pages that are known to getStaticPaths and were already built.

  • Version of Next.js: 9.3.1

Workaround

Right now I am attaching a custom preview indicator when starting a preview session in pages/api/preview.js and redirecting to the corresponding post:

res.writeHead(307, { Location: `${url}__preview__` })

In pages/posts/[slug].js due to the __preview__ indicator this URL was not pre-built by getStaticPaths and triggers a fallback call to getStaticProps. Like this I use this indicator to identify preview mode

const [slug, previewIndicator] = params.slug.split('__preview__')
const preview = previewIndicator === ''
@Timer Timer added the please add a complete reproduction Please add a complete reproduction. label Mar 23, 2020
@Timer
Copy link
Member

Timer commented Mar 23, 2020

We have a comprehensive test suite ensuring this feature works. Can you please provide more concrete reproduction steps and a full demo that doesn't work?

Example of Preview Mode for the DatoCMS example.

@pex
Copy link
Contributor Author

pex commented Mar 23, 2020

Thanks for the quick reply @Timer.

After trying to reproduce the bug in a clean setup I can confirm that it works as expected. 👍

Obviously this bug only occurs when beeing redirected by a third party (in my case prismic.io preview session). For some reason (and I am really struggling to figure out why), __prerender_bypass and __next_preview_data cookies are not set in request headers in this scenario, though they were set on the redirect response.

Accessing /api/preview route directly as well as having another internal page performing the redirect works as expected.

Redirect Response

HTTP/1.1 307 Temporary Redirect
Set-Cookie: __prerender_bypass=92659d731044c1fbc89afc10ebe9b753; Path=/; HttpOnly; SameSite=Strict
Set-Cookie: __next_preview_data=eyJhbGciOiJIUzI1NiJ9.NmQzYjMxNDliODY2MzQ4MmNjYjM0MWI1MzYyNTY2NzYwOWE4YTRkOTAwZjhmNjg1YmIzYTVhMDYyMWRhNDljMjFkNzk0ZTI0MDQzNWM3YjcyMzUxYjgwNzNlZDcwZmE5ZWE1Yzk3OTQwNjljMGJjM2I0MzU3ZDYxNWM0YmNiNWJlMTc5Njc3NTFjMDEwMDQ1MThjYzBiM2IyNzkzMTA3MmMyNjUwOGUyM2RkM2NjZjQ3YWEyY2M0NDQ3MjM5MzQxZDg0YmMzM2IyMDhkNjE4NzRkYWMyYjJmMGE0YjBlZGE0MzU1MDdmYg.iHkpnaKlB_sdDaojUo0k5C7YJBpCfAJP4nkYbLCWa74; Path=/; HttpOnly; SameSite=Strict
Location: /de/blog/next.js-ssr-prismic-headless-cms
Date: Mon, 23 Mar 2020 19:43:49 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Page Request (redirect target)

Host: localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:74.0) Gecko/20100101 Firefox/74.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Pragma: no-cache
Cache-Control: no-cache

No Cookie header is attached. When not beeing redirected from Prismic before, this field is present as expected.

@pex
Copy link
Contributor Author

pex commented Mar 23, 2020

Obviously this is not about Prismic. As soon as you have two sequenced redirects, the cookie won't be passed to the actual page:

Redirect within one app ✅

  1. Visit https://next-preview-mode.now.sh/api/preview
  2. You get redirected to https://next-preview-mode.now.sh/test with correct cookie headers

Redirect from another app ❗️

  1. (Clear cookies)
  2. Visit https://next-preview-mode-redirect.now.sh/api/redirect
  3. You get redirected to https://next-preview-mode.now.sh/api/preview (previous entrypoint)
  4. You get redirected to https://next-preview-mode.now.sh/test with no cookies header set!

Source code for the two apps:

  1. Preview Mode App: https://zeit.co/pex/next-preview-mode/7dtqcyof8/source
  2. Simple Redirect App: https://zeit.co/pex/next-preview-mode-redirect/o34d2dx4h/source

@pex
Copy link
Contributor Author

pex commented Mar 23, 2020

It gets even stranger:

  1. Chrome, Edge and Internet Explorer work as expected
  2. Firefox and Safari won't work

@pex
Copy link
Contributor Author

pex commented Mar 23, 2020

Cleared and condensed version can be found in #11304.

@gitabhisingh
Copy link

I see the issue happening again, I am able to see the preview mode enabled on Dev mode (cookies are being set), but in Prod build, it doesn't work at all (No cookies set)
#18155

@gitabhisingh
Copy link

After curl I found out that cookies are being set but in SameSite=None mode. Is that the issue?
I am using Next JS v9.5.2

 HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Set-Cookie: __prerender_bypass=e00e4ba3b660386df18eea2687f9b75; Path=/; HttpOnly; Secure; SameSite=None
Set-Cookie: __prerender_bypass=e00e4ba3b660386ef18eea2687f9b75; Path=/; HttpOnly; Secure; SameSite=None
< Set-Cookie: __next_preview_data=eyJhbGciOiJIUzI1NiIsInR5cI6IkpXVCJ9.eyJkYXRhIjoiYTU0NWQyN2RkZTM1ZTBiNjhiNzE3MTBkOWMxZjBmMGVlZDU3ZjEwMGQ5ZmExZTIxYmRjMDliMDY4Yjc5ODA1MWNlYzdhYWU1OWY5MzY1MTI5Y2RiMTNlZTA2OTM3M2ZhYjVkYjQ3NjYxYjdlMTkzMThhNmM4N2MxY2MzZTI3NDVhND4YjI0ZjllODgxN2I2OWYxODdhMTFhYjE0YzUyNzE0ZDQ0NDI4OTI1MWVhOTg1MDdkNWZlMDZkNThiNmUzMzI2MSIsImlhdCI6MTYwMzQ2NzA2Nn0.V4IpUW7V_goPgiJ5mV6-axAKQcMFxqNXwUQqVp6nfA; Path=/; HttpOnly; Secure; SameSite=None
Set-Cookie: __next_preview_data=eyJhbGciOiJIUzI1NisInR5cCI6IkpXVCJ9.eyJkYXRhIjoiYTU0NWQyN2RkZTM1ZTBiNjhiNzE3MTBkOWMxZjBmMGVlZDU3ZjEwMGQ5ZmExZTIxYmRjMDliMDY4Yjc5ODA1MWNlYzdhYWU1OWY5MzY1MTI5Y2RiMTNlZTA2OTM3M2ZhYjVkYjQ3NjYxYjdlMTkzMThhNmM4N2MxY2MzZTI3NDVhNDk4YjI0ZjllODgxN2I2OWYxODdhMTFhYjE0YzUyNzE0ZDQ0NDI4OTI1MWVhOTg1MDdkNWZlMDZkNThiNmUzMzI2MSIsmlhdCI6MTYwMzQ2NzA2Nn0.V4IpUW7V_goPgiJ5mV6-axAKQcMFxqNXwWQqVp6nfA; Path=/; HttpOnly; Secure; SameSite=None
< Date: Fri, 23 Oct 2020 15:31:06 GMT
Date: Fri, 23 Oct 2020 15:31:06 GMT
< Connection: keep-alive
Connection: keep-alive
< Transfer-Encoding: chunked
Transfer-Encoding: chunked

< 
<!DOCTYPE html><html><head><meta http-equiv="Refresh" content="0; url=/posts/everything-you-want-to-know" />
    <script>window.location.href = '/posts/everything-you-want-to-know'</script>
* Connection #0 to host localhost left intact
    </head>* Closing connection 0

@yoppoy
Copy link

yoppoy commented Dec 15, 2020

@shinesingh1994 When testing this feature locally, you might wanna replace localhost by 127.0.0.1 👍(when cookies are secure)

@balazsorban44
Copy link
Member

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.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
please add a complete reproduction Please add a complete reproduction.
Projects
None yet
Development

No branches or pull requests

5 participants