# Detect the document type dynamically

**URL:** <https://community.prismic.io/t/detect-the-document-type-dynamically/19643>\
**Category:** Developing with Prismic\
**Tags:** nextjs\
**Created:** [April 28, 2025, 6:13am UTC](https://community.prismic.io/t/detect-the-document-type-dynamically/19643 "2025-04-28T06:13:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![drl.oucommunity](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/drl.oucommunity/32/7969_2.png) [@drl.oucommunity](https://community.prismic.io/u/drl.oucommunity)\
**Post date:** [April 28, 2025, 6:13am UTC](https://community.prismic.io/t/detect-the-document-type-dynamically/19643/1 "2025-04-28T06:13:41Z")

</div>

Hi everyone,

I'm using Prismic with a Next.js project, and I have a situation where:

- I have documents of different types (`page`, `article_post`, and `documentation_page`).
- All of them should resolve to the same route structure (i put it in `app/[uid]/page.tsx`).
- Each type has its own `graphQuery` to fetch different fields.

❓ The current problem:

- To detect the document by UID, I have to `try/catch` and call `getByUID` multiple times (one per type).
- In the worst case, this causes **3 API requests** before even loading the real content.

```auto
  try {
    page = await client.getByUID('page', uid)
  } catch {
    try {
      const graphQuery = `{
          ......
      }`
      page = await client.getByUID('article_post', uid, { graphQuery })
    } catch {
      try {
        const graphQuery = `{
          documentation_page {
           .........
          }
        }`
        page = await client.getByUID('documentation_page', uid, { graphQuery })
      } catch {
        notFound()
      }
    }
  }

  if (!page) {
    notFound()
  }

  switch (page.type) {
    case 'article_post':
      return <ArticlePage page={page} />
    case 'documentation_page':
      return <DocumentationPage page={page} />
    default:
      return <DefaultPage page={page} />
  }

```

💬 What I want to achieve:

- Find the correct document by UID **across multiple types** using **only 1 API call**.
- Then, based on the type, make 1 additional call with the correct `graphQuery`.

📚 I checked the Prismic Client v7 docs and know about `filter.any("document.type", [...])`, but I’m not 100% sure how to combine it perfectly for UID lookup.

I want put it in `app/[uid]/page.tsx` instead of put it in

- `app/[uid]/page.tsx`
- `app/article/[uid]/page.tsx`
- `app/docs/[uid]/page.tsx`

**Question** :

- What’s the best practice to efficiently detect a document by UID across several types?
- Should I use `getFirst`, `getAll`, or maybe a custom filters?
- Is there an even better strategy (e.g., GraphQL one-shot query)?

Any advice, best practices, or sample code would be super appreciated! 🚀

Thanks so much 🙏

---

<div class="post-metadata">

**Author:** ![Pau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.prismic.io/pau/32/5422_2.png) [@Pau](https://community.prismic.io/u/Pau)\
**Post date:** [April 28, 2025, 6:53pm UTC](https://community.prismic.io/t/detect-the-document-type-dynamically/19643/2 "2025-04-28T18:53:08Z")

</div>

To avoid multiple `getByUID` calls, you could use `client.get()` with `filters` to search by UID across types in one request:

```auto
const page = await client.get({
  filters: [
    `my.document.uid = "${uid}"`,
    `document.type in ["page", "article_post", "documentation_page"]`,
  ],
  pageSize: 1,
});

```

Although please take into account that the UID is unique **per type and per language**.  
If different types share the same UID, and you’re trying to resolve everything under `/[uid]`, it’s hard to know which document should load. That’s why, in cases like this, it’s actually **better** to split the routes:  
`/article/[uid]`, `/docs/[uid]`, and `/[uid]`.

If you really need to mix content from different types into one page, another option is using **Content Relationship fields** to pull related documents rather than trying to create a link between them all directly by UID.
