Preview data not available in middleware

Hi there,

I use the preview functionality on a few of our Next.js sites. It works great when the routes are directly accessible. But for some pages, we use middleware to redirect them internally to the right page component. For these pages, the preview doesn't seem to work, especially for pages that haven't been published yet. I'm assuming this is because the previewData that gets passed to the getStaticProps isn't available to the middleware to be passed to the createClient function used in our middleware file. Is there a way to get this previewData in the middleware so the internal routing works?

Quick summary of what we do: based on the slug, we fetch a document that has the slug in a field. This returns null because the page hasn't been published yet, resulting us to return a 404 page.

Hi Gijs,

My guess is that it's more to do with your routing than data not being passed through the middleware. The preview system relies on the route resolver or link resolver to find the correct file on which to load the preview data. So, either the routing does not match your page directory, or maybe the preview route is obscured.

It would really help if you could share all your relevant code so we can have a look together.

  • The prismicio.js file
  • The middleware
  • The preview page
  • Your page directory structure
  • An example file where the preview isn't loading so we can see the query code

Thanks.