A Sanity client (CDN recommended for build-time use).
Optionaloptions: GetRedirectsOptionsOptional filtering and caching options.
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)
}
Fetch all redirects from Sanity and return them in a format compatible with Next.js
redirects()innext.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
cacheTtlto avoid repeated API calls: