@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.
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 nullCompared 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/redirects | Payload'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 measured | 0.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 redirect | The 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-us | Any spelling finds it. | Only the exact spelling. |
| Redirects from URLs at any depth/blog/2019/05/opening-hours | Any 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/$1 | One 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 pages | Yes, 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=newsletter | Kept on the new URL, with the query-string proxy. | Dropped. |
| Suggests a redirect when a page moves | Your 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/people | Any 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 pick | Visitors 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 it | Set it to Off. | Delete it. |
| Catches mistakes when you savea mailto: link to redirect from | Cleans each one and refuses what can't work. | Only checks that the fields are filled in and no old URL is used twice. |
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.What you get
Section titled “What you get”- Exact paths and regexes: Exact paths match in any case, and regex groups fill into a URL as
$1to$9. See How redirects match. - Old URLs with a query string: A redirect from
/news.aspx?id=123matches parameters in any order and case. Tracking parameters such asutm_sourceare 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
winOverDocumentsto let selected redirects or every redirect that is On win over pages. See Let redirects win over pages.
Requirements
Section titled “Requirements”| 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.
Next steps
Section titled “Next steps”- Installation — install the package, add the plugin, and answer redirects in your page route.
- Configuration — full reference for
redirectsPlugin(). - Suggest redirects on a move — call
suggestRedirectsfrom your own hook or@payloadpack/path. - Match query strings in a proxy — match old URLs with query strings before cached pages.
- Let redirects win over pages — let selected or all redirects answer even where a page exists.
findRedirectandsuggestRedirects— API reference.