@sanity/routes
    Preparing search index...

    Function getRedirects

    • Fetch all redirects from Sanity and return them in a format compatible with Next.js redirects() in next.config.js.

      For sites with fewer than 2,000 redirects, this enables zero-middleware, edge-served redirects compiled into the routing table at build time.

      When called in server hooks or middleware (every request), use cacheTtl to avoid repeated API calls:

      // Cached — one API call per 60 seconds
      const redirects = await getRedirects(client, { cacheTtl: 60_000 })

      Parameters

      • client: SanityClient

        A Sanity client (CDN recommended for build-time use).

      • Optionaloptions: GetRedirectsOptions

        Optional filtering and caching options.

      Returns Promise<FrameworkRedirect[]>

      Array of redirect objects ready for next.config.js redirects().

      // next.config.js — build-time, no cache needed
      import { getRedirects } from '@sanity/routes'
      import { client } from './src/sanity/lib/client'

      export default {
      async redirects() {
      return getRedirects(client)
      }
      }
      // SvelteKit hooks.server.ts — runtime, cached
      import { getRedirects } from '@sanity/routes'
      import { client } from '$lib/sanity'

      export const handle = async ({ event, resolve }) => {
      const redirects = await getRedirects(client, { cacheTtl: 60_000 })
      const match = redirects.find(r => r.source === event.url.pathname)
      if (match) throw redirect(match.statusCode, match.destination)
      return resolve(event)
      }
      // Filter by source type
      const autoOnly = await getRedirects(client, { source: 'auto' })
      const manualOnly = await getRedirects(client, { source: 'manual' })