digitalbunch

payload slug redirects

by digitalbunch
Redirects & URLs
v3
^3.0.0
MIT

Overview

Summary generated from the README

Adds an afterChange hook to watched collections and a slug-redirects collection that stores the old slug along with the document ID. Next.js helpers for the App Router, Pages Router and an API route look up an old slug and redirect to the document's current slug. Requires Payload 3, Node.js 20 or later, and Next.js 14 or later for the helpers.

Features

  • Records the old slug when a published document's slug changes
  • Stores the document ID so chained renames all resolve to the latest slug
  • SlugRedirect server component issuing 308 or 307 redirects in the App Router
  • resolveSlugRedirect helper for the Pages Router and an optional API route handler
  • Multi-locale tracking through a localizedSlugs JSON field
  • Optional revalidation URL and onChange callbacks per collection

Use cases

  • Preserve inbound links and search rankings after renaming blog post slugs
  • Redirect old localized URLs to current pages on a multi-language Next.js site
  • Trigger frontend cache revalidation or custom logic whenever a slug changes

From the author

PayloadCMS plugin that automatically creates slug redirects when document slugs change

Installation

npm i payload-slug-redirects

3

Stars

0

Forks

3

Open Issues

3 days ago

Last Updated

71

Weekly Downloads

v2.0.3

npm Version

51.4 KB

Unpacked Size

0

Dependencies

GitHub stars

3+1 since Sep 7

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 20263
Dec 5, 20263
Dec 4, 20263
Dec 3, 20263
Dec 2, 20263
Dec 1, 20263
Nov 30, 20263
Nov 29, 20263
Nov 28, 20263
Nov 27, 20263
Nov 26, 20263
Nov 25, 20263
Nov 24, 20263
Nov 23, 20263
Nov 22, 20263
Nov 21, 20263
Nov 20, 20263
Nov 19, 20263
Nov 18, 20263
Nov 17, 20263
Nov 16, 20263
Nov 15, 20263
Nov 14, 20263
Nov 13, 20263
Nov 12, 20263
Nov 11, 20263
Nov 10, 20263
Nov 9, 20263
Nov 8, 20263
Nov 7, 20263
Nov 6, 20263
Nov 5, 20263
Nov 4, 20263
Nov 3, 20263
Nov 2, 20263
Nov 1, 20263
Oct 31, 20263
Oct 30, 20263
Oct 29, 20263
Oct 28, 20263
Oct 27, 20263
Oct 26, 20263
Oct 25, 20263
Oct 24, 20263
Oct 23, 20263
Oct 22, 20263
Oct 21, 20263
Oct 20, 20263
Oct 19, 20263
Oct 18, 20263
Oct 17, 20263
Oct 16, 20263
Oct 15, 20263
Oct 14, 20263
Oct 13, 20263
Oct 12, 20263
Oct 11, 20263
Oct 10, 20263
Oct 9, 20263
Oct 8, 20263
Oct 7, 20263
Oct 6, 20263
Oct 5, 20263
Oct 4, 20263
Oct 3, 20263
Oct 2, 20263
Oct 1, 20263
Sep 30, 20263
Sep 29, 20263
Sep 28, 20263
Sep 27, 20263
Sep 26, 20263
Sep 25, 20262
Sep 24, 20262
Sep 23, 20262
Sep 22, 20262
Sep 21, 20262
Sep 20, 20262
Sep 19, 20262
Sep 18, 20262
Sep 17, 20262
Sep 16, 20262
Sep 15, 20262
Sep 14, 20262
Sep 13, 20262
Sep 12, 20262
Sep 11, 20262
Sep 10, 20262
Sep 9, 20262
Sep 8, 20262
Sep 7, 20262
npm weekly downloads

88+55 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 202688
Dec 5, 202687
Dec 4, 202689
Dec 3, 2026104
Dec 2, 2026127
Dec 1, 2026132
Nov 30, 2026132
Nov 29, 2026110
Nov 28, 2026189
Nov 27, 2026303
Nov 26, 2026423
Nov 25, 2026426
Nov 24, 2026422
Nov 23, 2026420
Nov 22, 2026447
Nov 21, 2026364
Nov 20, 2026250
Nov 19, 2026162
Nov 18, 2026197
Nov 17, 2026199
Nov 16, 2026218
Nov 15, 2026366
Nov 14, 2026374
Nov 13, 2026385
Nov 12, 2026343
Nov 11, 2026281
Nov 10, 2026284
Nov 9, 2026270
Nov 8, 202677
Nov 7, 202677
Nov 6, 202657
Nov 5, 202667
Nov 4, 202677
Nov 3, 202683
Nov 2, 202680
Nov 1, 2026100
Oct 31, 2026105
Oct 30, 2026144
Oct 29, 2026122
Oct 28, 2026105
Oct 27, 202694
Oct 26, 202693
Oct 25, 202672
Oct 24, 202669
Oct 23, 202627
Oct 22, 202647
Oct 21, 202666
Oct 20, 202668
Oct 19, 202667
Oct 18, 202672
Oct 17, 202654
Oct 16, 202662
Oct 15, 202651
Oct 14, 202639
Oct 13, 202635
Oct 12, 202635
Oct 11, 202628
Oct 10, 202633
Oct 9, 202634
Oct 8, 202651
Oct 7, 202661
Oct 6, 202661
Oct 5, 202664
Oct 4, 202666
Oct 3, 202665
Oct 2, 202660
Oct 1, 202639
Sep 30, 202631
Sep 29, 202632
Sep 28, 202629
Sep 27, 202628
Sep 26, 202627
Sep 25, 202623
Sep 24, 202631
Sep 23, 202639
Sep 22, 202641
Sep 21, 202641
Sep 20, 202657
Sep 19, 202665
Sep 18, 202680
Sep 17, 202673
Sep 16, 202665
Sep 15, 202662
Sep 14, 202664
Sep 13, 202646
Sep 12, 202638
Sep 11, 202623
Sep 10, 202622
Sep 9, 202632
Sep 8, 202633
Sep 7, 202633

Each point is npm's rolling 7-day download count.

Health Score

64/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1185354194-root.svg)](https://payloaddirectory.dev/plugins/1185354194-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1185354194-root"><img src="https://payloaddirectory.dev/badge/1185354194-root.svg" alt="Payload Directory"></a>
Updated Dec 2, 2026Created Mar 18, 2026Published Nov 6, 2026

README

payload-slug-redirects

Records slug changes in PayloadCMS v3 and gives your Next.js frontend what it needs to send visitors from an old URL to the current one.

Redirect records store the document ID instead of the destination slug, so after renames from A to B to C, both old URLs resolve to C.

npm version MIT license

Install

pnpm add payload-slug-redirects

Requires PayloadCMS ^3.0.0 and Node.js 20 or later. The Next.js helpers require Next.js >=14.0.0.

Quick start

// payload.config.ts
import { slugRedirectsPlugin } from 'payload-slug-redirects'

export default buildConfig({
  plugins: [
    slugRedirectsPlugin({
      collections: ['posts', 'case-studies'],
    }),
  ],
})

The plugin adds an afterChange hook to each watched collection and creates a slug-redirects collection. When a published document's slug changes, the hook records the old slug. When a visitor hits a URL with no matching document, the frontend helpers look up that slug and redirect to the document's current slug.

Slug field

If a watched collection has no slug field, the plugin adds one. An existing field, including one from Payload's slugField(), is left alone.

SiteDefault fieldType
Single localeslugtext
Multiple localeslocalizedSlugsjson

On multi-locale sites the hook needs every locale's slug on each save, but Payload only passes the current locale to afterChange for localized: true fields. Redirects are therefore tracked from a JSON field shaped like { "en": "my-post", "ar": "..." }.

The plugin creates localizedSlugs but does not fill it. Your project has to write that value on save, for example in a beforeChange hook. If it stays empty, no redirects are recorded.

Options

OptionTypeDefaultDescription
collectionsArray<string | CollectionEntry>requiredCollections to watch
enabledbooleantrueDisable the plugin without removing it
localesstring[]['en']Locale codes to track
slugFieldstringsee aboveField holding the slug
revalidateUrlstring--Called with the old slug after a change, in production only
revalidateHeadersRecord<string, string>--Headers for revalidation requests
collection.namestring'slug-redirects'Collection slug
collection.visibleInTheUIbooleantrueShow in the admin sidebar
collection.onChange(args) => void | Promise<void>--Fires on any slug change

Entries in collections can override slugField and add their own onChange:

slugRedirectsPlugin({
  collections: [
    'posts',
    {
      name: 'case-studies',
      slugField: 'customSlugField',
      onChange: ({ fromSlug, toSlug, locale, collectionType }) => {},
    },
  ],
})

Next.js

The helpers below also accept redirectsCollection for a custom collection.name, slugField when the frontend slug field is not slug, and fallbackLocale.

App Router

// app/[locale]/posts/[slug]/page.tsx
import { SlugRedirect } from 'payload-slug-redirects/next/app'

export default async function PostPage({ params }) {
  const post = await getPost(params.slug, params.locale)

  if (!post) {
    return (
      <SlugRedirect
        slug={params.slug}
        collectionType="posts"
        locale={params.locale}
        cmsUrl={process.env.CMS_URL!}
        buildUrl={(slug, locale) => `/${locale}/posts/${slug}`}
      />
    )
  }

  return <PostView post={post} />
}

SlugRedirect is a server component that calls permanentRedirect() (308) or notFound(). Pass permanent={false} for a 307.

Pages Router

// pages/posts/[slug].tsx, inside getStaticProps
import { resolveSlugRedirect } from 'payload-slug-redirects/next/pages'

if (!post) {
  const redirect = await resolveSlugRedirect({
    fromSlug: slug,
    locale: locale ?? 'en',
    collectionType: 'posts',
    cmsUrl: process.env.NEXT_PUBLIC_CMS_API_URL!,
    buildUrl: (s, l) => (l === 'ar' ? `/ar/posts/${s}` : `/posts/${s}`),
  })
  if (redirect) return redirect
  return { notFound: true }
}

Pass permanent: false for a temporary redirect.

API route

An optional endpoint for client-side lookups:

// pages/api/slug-redirects.ts
import { createSlugRedirectHandler } from 'payload-slug-redirects/next'

export default createSlugRedirectHandler({
  cmsUrl: process.env.NEXT_PUBLIC_CMS_API_URL!,
})

GET /api/slug-redirects?fromSlug=old-slug&locale=en&collectionType=posts returns { slug: 'new-slug' }, or 404 if there is no redirect and 400 for invalid parameters.

Access control

Records hold fromSlug, locale, collectionType and documentId.

OperationAllowed for
ReadEveryone, because the Next.js helpers fetch without credentials
Create, updateNobody except the plugin's hook
DeleteUsers from the collection set in admin.user

Contributing

See CONTRIBUTING.md. Report vulnerabilities through SECURITY.md.

License

MIT

Similar plugins