Skip to content

@payloadpack/redirects

Introduction

Redirects editors manage in Payload 4, found through database indexes at the same cost for 100,000 as for 1,000, with optional move suggestions.

@payloadpack/redirects adds a Redirects collection to Payload 4. Editors send visitors from an exact path or a regex to a document on your site or to a URL, and your site asks for a redirect only when no page holds a path. When your site reports a page move, the plugin suggests a redirect from the old path that an editor turns on with a yes.

payload.config.ts
import { redirectsPlugin } from '@payloadpack/redirects'
import { buildConfig } from 'payload'
export default buildConfig({
plugins: [
redirectsPlugin({
collections: ['pages'],
buildDocumentPath: ({ document }) => `/${document.slug}`,
documentSelect: { slug: true }
})
]
})
import { findRedirect } from '@payloadpack/redirects'
const { foundRedirect } = await findRedirect({ path: '/about-us/team', payload })
// foundRedirect: { destination: '/company/team', matchedQueryString: false, permanent: true, to: { type: 'document' } }, or null

Compared with Payload’s redirects plugin

Section titled “Compared with Payload’s redirects plugin”

Payload’s own @payloadcms/plugin-redirects only stores the list of redirects. On its own it sends no visitor anywhere: your site needs code that finds the redirect and sends the visitor on. Payload’s example website ships that code, so it’s Payload’s side below.

@payloadpack/redirectsPayload's plugin with Payload's example website
Finds one redirect among 100,000Median on MongoDB, Postgres and SQLite, each on the same machine as the app.How it's measured0.19–0.36 ms3,400× fasterAbout the same as among 1,000.1.3–1.5 sThe example website's lookup. The list is too big for Next.js's data cache, so every lookup reads all 100,000 from the database. Next.js caches each rendered page, so it runs when a page or post renders, not on every view, pages that exist included.
Sends visitors to their redirectThe lookup is built in. Your page route calls findRedirect and sends the visitor on, about ten lines.Not on its own: the plugin only stores the list. Your site needs code that finds the redirect and sends the visitor. Payload's example website ships about 90 lines of it, in four files.
Ignores capital letters/About-Us finds the redirect from /about-usAny spelling finds it.Only the exact spelling.
Redirects from URLs at any depth/blog/2019/05/opening-hoursAny URL your site gets, up to 32 levels deep.Only /slug and /posts/slug. No route looks up a redirect for a deeper URL.
Redirects a whole section and keeps the rest of the URL/blog/(.*) → /news/$1One regex redirect. Its groups fill into the new URL as $1 to $9.No regexes, and /blog/… is deeper than /slug, so editors can't redirect it.
Tells old URLs apart by what follows the ?/news.aspx?id=123 and ?id=124 to different pagesYes, with the query-string proxy, and with the parameters in any order and case.No. Both go wherever /news.aspx goes.
Keeps campaign tags on the way?utm_source=newsletterKept on the new URL, with the query-string proxy.Dropped.
Suggests a redirect when a page movesYour site reports the move, and an editor turns the suggestion on with one click.Nothing. Someone has to notice the old URL and add a redirect by hand.
Redirects to pages at any depth/about/team/peopleAny page's path, nested or not, in every collection you list, and it follows the page when it moves.A picked page goes to /slug and a post to /posts/slug. Any other path must be typed as a URL, which doesn't follow the page when it moves.
Permanent or temporary, as editors pickVisitors get what the editor picked: permanent (308) or temporary (307).The plugin can store one of five status codes editors pick. Payload's example website doesn't turn it on and always sends temporary.
Turns a redirect off without deleting itSet it to Off.Delete it.
Catches mistakes when you savea mailto: link to redirect fromCleans each one and refuses what can't work.Only checks that the fields are filled in and no old URL is used twice.
Neither side waits on a network here, since each database runs on the same machine as the app. With the database elsewhere, ours and the example website's uncached read each wait at least one round trip more, and the uncached read pulls the whole list across it: see Speed for ours with the database 5 ms away. A list Next.js holds needs no query. With a custom cache handler, which Next.js doesn't hold to 2 MB, the example website's lookup among 100,000 parses the cached list instead and costs at least 32.4–33.1 ms. At 1,000 redirects, where Next.js's data cache holds the list, it costs at least 0.3–0.31 ms with no database query: close to ours on the same machine, and faster than ours once the database is a round trip away. The plugin indexes from, so a lookup you write yourself can find a redirect in one query where from matches exactly as stored, without regexes, and with a query string only in the order and case it was stored.
  • Exact paths and regexes: Exact paths match in any case, and regex groups fill into a URL as $1 to $9. See How redirects match.
  • Old URLs with a query string: A redirect from /news.aspx?id=123 matches parameters in any order and case. Tracking parameters such as utm_source are not compared and stay on the destination. See Match query strings in a proxy.
  • Document targets follow moves: A redirect to a document sends visitors to wherever the document is now, even after later moves.
  • Move suggestions: When your site reports a moved page from its own hook or through @payloadpack/path, one suggestion covers the whole moved branch. An editor answers yes, edit, or no right on the moved page, and no redirect answers until someone says yes. See How move suggestions work.
  • Constant lookup cost: One indexed database query reads only the redirects and regexes that can cover a path, so 100,000 redirects cost the same as 1,000. Reading a winning document target adds one query. See Speed.
  • Safe regexes on RE2: Regexes run on RE2, which never backtracks. A regex too large to try on unknown paths is refused on save.
  • Host-locked destinations: A target starting with / never leaves your site, and a URL target never leaves its host, whatever a regex group fills in.
  • Optional redirects that win over pages: By default, a page wins over every redirect at its path except one that matched the visitor’s query string. Set winOverDocuments to let selected redirects or every redirect that is On win over pages. See Let redirects win over pages.
Package or database Version or setup
payload 4.0.0-beta.11 or later
MongoDB @payloadcms/db-mongodb
Postgres @payloadcms/db-postgres
SQLite @payloadcms/db-sqlite
@payloadpack/path 0.3.1 or later (optional)

Works with and without database transactions on MongoDB, Postgres, and SQLite. The package does not depend on @payloadpack/path.