---
title: "Dedupe Middleware"
description: "Learn how oRPC skips repeated middleware and metadata, and how to use context to dedupe the rest."
---

## Problem

The same middleware can run more than once during a single call. This often happens when:

- you use `.use(authProvider).router(router)`, and some procedures in `router` already use `authProvider`
- a procedure [calls](/docs/client/server-side#one-off-calls) another procedure that both use the same middleware

:::warning
Repeated middleware work can hurt performance, especially for expensive operations such as opening a database connection.
:::

## Automatic Dedupe

oRPC skips router-level middleware that a procedure already starts with:

```ts
const authed = os.use(auth)

const router = authed.router({
  foo: authed.handler(() => 'foo'), // auth runs once
})
```

[Metadata](/docs/metadata) works the same way:

```ts
const base = os.meta(openapi({ tags: ['planets'] }))

const router = base.router({
  list: base.handler(() => []), // tags: ['planets'], not ['planets', 'planets']
})
```

:::info
Only the same middleware in the same leading position, before any `.input`/`.output`, is skipped.
:::

To disable it:

```ts
os.$config({ dedupeLeadingMiddlewares: false, dedupeLeadingMetaPlugins: false })
oc.$config({ dedupeLeadingMetaPlugins: false })
implement(contract).$config({ dedupeLeadingMiddlewares: false })
```

## Manual Dedupe

For other cases, such as one procedure calling another, store the computed value in [`context`](/docs/context) and reuse it when the [middleware](/docs/middleware) runs again.

For example, this middleware loads auth at most once per call:

```ts twoslash
import { os } from '@orpc/server'

declare function loadAuth(headers: Headers): Promise<{ id: string } | undefined>
// ---cut---
const authProvider = os
  .$context<{ headers: Headers, auth?: { id: string } | undefined, authLoaded?: boolean | undefined }>()
  .middleware(async ({ context, next }) => {
    // reuse the loaded auth value if it was already loaded
    const auth = context.authLoaded
      ? context.auth
      : await loadAuth(context.headers)

    return next({ context: { auth, authLoaded: true } })
  })
```

You can now apply `authProvider` multiple times without loading auth again:

```ts twoslash
import { call, os } from '@orpc/server'

declare function loadAuth(headers: Headers): Promise<{ id: string } | undefined>
const authProvider = os
  .$context<{ headers: Headers, auth?: { id: string } | undefined, authLoaded?: boolean | undefined }>()
  .middleware(async ({ context, next }) => {
    // reuse the loaded auth value if it was already loaded
    const auth = context.authLoaded
      ? context.auth
      : await loadAuth(context.headers)

    return next({ context: { auth, authLoaded: true } })
  })
// ---cut---
const base = os.$context<{ headers: Headers }>()

const foo = base.use(authProvider).handler(({ context }) => 'Hello World')

const bar = base.use(authProvider).handler(({ context }) => {
  // Reuse the auth value that is already stored in context.
  return call(foo, undefined, { context }) // [!code highlight]
})

// authProvider runs again here, but loads auth only once.
const router = base
  .use(({ next }) => {
    // Additional middleware logic
    return next()
  })
  .use(authProvider) // [!code highlight]
  .router({
    foo,
    bar,
  })
```
