@payloadpack/redirectsGuides
Let redirects win over pages
Configure winOverDocuments and a Next.js proxy so selected or all redirects answer even where a page holds the path.
When to use this
Section titled “When to use this”You want a redirect to answer at a path where a page is still published, without unpublishing the page.
With this setup, every page view pays one database query; see Speed. Skip this guide if your redirects only cover paths no page holds.
Prerequisites: Complete Installation on a Next.js 16 site using proxy.ts.
By default, a page wins over every redirect at its path except one that matched the visitor’s query string.
Set it up
Section titled “Set it up”-
Set
winOverDocumentsinpayload.config.ts.payload.config.ts redirectsPlugin({collections: ['pages'],buildDocumentPath: ({ document }) => `/${document.slug}`,documentSelect: { slug: true },winOverDocuments: 'perRedirect'})'perRedirect'adds a Wins over a page (winsOverDocument) box to each redirect, unchecked by default and on every suggestion an editor says yes to. Editors check the box on redirects that should win over a page.'always'lets every redirect that is On win over a page and adds no box.
A redirect that matched the visitor’s query string wins over a page under both settings. Setting
'perRedirect'adds awinsOverDocumentcolumn, so runbunx payload migrate:createon Postgres and SQLite. -
Answer every redirect in
proxy.ts.Because a redirect can now cover a path where a page exists, the proxy checks every request, with or without a query string. The same proxy works for both
'perRedirect'and'always'.Put
proxy.tsnext to thesrc/find-page.tsyou wrote in step 5 of Installation, whosefindPageit calls.src/proxy.ts import payloadConfig from '@payload-config'import { findRedirect } from '@payloadpack/redirects'import { NextResponse, type NextRequest } from 'next/server'import { getPayload } from 'payload'import { findPage } from './find-page'// Answers every redirect, since one can win over the page at its path. findRedirect decides by// winOverDocuments whether a page wins, and asks hasDocument only when it needs to.export async function proxy(request: NextRequest) {let path: stringtry {path = decodeURIComponent(request.nextUrl.pathname)} catch {return NextResponse.next()}const payload = await getPayload({ config: payloadConfig })const hasDocument = async (pathAsked: string) => (await findPage({ path: pathAsked, payload })) !== nullconst { foundRedirect } = await findRedirect({ hasDocument, path, payload, searchParams: request.nextUrl.searchParams })if (!foundRedirect) return NextResponse.next()// A path on this site goes below its basePath, which Next adds to a page route's redirect() itself.const { destination, permanent } = foundRedirectconst url = destination.startsWith('/') ? `${request.nextUrl.basePath}${destination}` : destinationreturn NextResponse.redirect(new URL(url, request.url), permanent ? 308 : 307)}// Skips the admin, the API and Next's own files, whole segments only, so a path such as /apiary passes.export const config = { matcher: ['/((?!(?:admin|api|_next)(?:/|$)).*)'] } -
Remove the redirect check from your page route.
The proxy now answers every redirect before the page route runs, so your page route only renders the page or calls
notFound().
Performance, hasDocument, and CDN caching
Section titled “Performance, hasDocument, and CDN caching”-
Every page view pays a lookup: Each request runs one database query. A redirect to a document costs two more queries: one to read the document’s path and one lookup at that path, because another redirect can win over that page too. Each step of a chain costs one lookup. See Speed.
-
Files in
public/: Requests for static files such as/favicon.icopass the default matcher and run a lookup. Exclude static files your site serves inconfig.matcher. -
hasDocumentwith'perRedirect':findRedirectaskshasDocumentonly when the best candidate cannot win over a page by itself. With'always',findRedirectnever callshasDocumentand follows chains without it. CallingfindRedirectwithouthasDocumentwhenwinOverDocumentsis'perRedirect'throws:@payloadpack/redirects: give findRedirect hasDocument, which says whether a document holds a path, because with winOverDocuments ‘perRedirect’ a document at the path wins over every redirect but the ones an editor picked and one that matched the query string
-
CDN caching: A CDN in front of the proxy serves pages it cached before the proxy runs, so a redirect saved later reaches visitors only after that path is purged at the CDN. Next’s CDN caching guide recommends running the proxy before the CDN cache or skipping CDN caching for routes that depend on it.