# Preview data not available in middleware

**URL:** <https://community.prismic.io/t/preview-data-not-available-in-middleware/16592>\
**Category:** Developing with Prismic\
**Tags:** nextjs\
**Created:** [September 2, 2024, 7:06am UTC](https://community.prismic.io/t/preview-data-not-available-in-middleware/16592 "2024-09-02T07:06:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gijs](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/gijs/32/4212_2.png) [@gijs](https://community.prismic.io/u/gijs)\
**Post date:** [September 2, 2024, 7:06am UTC](https://community.prismic.io/t/preview-data-not-available-in-middleware/16592/1 "2024-09-02T07:06:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Phil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/phil/32/14_2.png) [@Phil](https://community.prismic.io/u/Phil)\
**Post date:** [September 2, 2024, 9:52am UTC](https://community.prismic.io/t/preview-data-not-available-in-middleware/16592/2 "2024-09-02T09:52:28Z")

</div>

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.
