klnap

payload slug

by klnap
Redirects & URLs
v3
>=3.94.1 <4
MIT

Overview

Summary generated from the README

Provides a slugField helper that adds a required, unique, indexed slug text field generated from a top-level or nested source field path. It supports optional persisted Auto/Manual mode per locale, custom sync or async slugifiers, and a live preview in the admin. An optional slug() plugin registers English labels and enables custom preview. Requires Payload 3.94.1 or newer below 4, and is ESM only.

Features

  • Generates slugs from top-level or dot-path source fields in named groups and objects
  • Persisted Auto/Manual mode toggle stored as slugMode, per locale when localized
  • Custom slugify callback, synchronous or asynchronous, applied to manual values too
  • Live editable preview in the admin, with a custom preview endpoint for collections
  • Standalone generateSlug utility with locale-aware transliteration
  • Works on collections and globals, with uniqueness enforced by the database adapter

Use cases

  • Create URL-friendly slugs from a title that stay stable after publishing
  • Manage per-locale slugs in a multi-language site with Auto or Manual control
  • Derive slugs from nested fields such as a hero title using custom rules

From the author

A Payload CMS field that generates URL-friendly slugs from top-level or nested source paths and enforces uniqueness, with optional persisted Auto/Manual control.

Installation

npm i @klnap/payload-slug

0

Stars

0

Forks

0

Open Issues

Yesterday

Last Updated

2.5k

Weekly Downloads

v0.3.2

npm Version

193.1 KB

Unpacked Size

1

Dependencies

GitHub stars

00 since Nov 22

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 20260
Dec 4, 20260
Dec 3, 20260
Dec 2, 20260
Dec 1, 20260
Nov 30, 20260
Nov 29, 20260
Nov 28, 20260
Nov 27, 20260
Nov 26, 20260
Nov 25, 20260
Nov 24, 20260
Nov 23, 20260
Nov 22, 20260
npm weekly downloads

2.4K+2.4K since Nov 27

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 20262,399
Dec 4, 20262,496
Dec 3, 20262,376
Dec 2, 20262,323
Dec 1, 20262,199
Nov 30, 2026861
Nov 29, 2026583
Nov 28, 2026412
Nov 27, 202617

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

Health Score

80/100Excellent

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/1469459976-root.svg)](https://payloaddirectory.dev/plugins/1469459976-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1469459976-root"><img src="https://payloaddirectory.dev/badge/1469459976-root.svg" alt="Payload Directory"></a>
Updated Dec 4, 2026Created Nov 24, 2026Published Dec 4, 2026

README

@klnap/payload-slug

Slug fields for Payload CMS with live preview, automatic and manual modes, localization, and nested source paths.

Features

Per-locale persisted modes, live preview, custom slugification, and stable Auto slugs.

Installation

bun add @klnap/payload-slug
# or
npm install @klnap/payload-slug

Usage

import type { CollectionConfig } from "payload";
import { slugField } from "@klnap/payload-slug";

export const Posts: CollectionConfig = {
  slug: "posts",
  fields: [
    { name: "title", type: "text", required: true },
    slugField({ source: "title", modeToggle: true }),
  ],
};

Use slugField() for the field, Admin component, built-in preview, and save hook. Register slug() for English labels or custom preview; server callbacks work without it.

import type { Plugin } from "payload";
import { slug } from "@klnap/payload-slug";

const plugins: Plugin[] = [slug()];

Payload's native slug field also supports localization and custom slugification. This package adds persisted Auto/Manual control and nested paths.

Slug behavior

ConfigurationBehavior
Default (modeToggle: false)Editable preview follows the source until manually changed. Direct updates omitting slug preserve it; Admin may submit its preview.
Auto (modeToggle: true)Draft source changes regenerate the slug. Source-only updates to published documents preserve the established slug.
Manual (modeToggle: true)The editor controls the slug; source changes do not regenerate it. Supplied values are normalized and an empty manual value fails validation.

Auto is read-only in the Admin. Manual-to-Auto or an explicit API slug change can change a published URL; applications own redirects. Resolve each locale's draft placeholder before publishing. Preview does not reserve uniqueness; duplicate saves fail under adapter rules.

A new article with an automatically generated slug

An article in Auto mode with its title and slug visible

An article in Manual mode with a custom slug

Configuration

slugField() creates a required, unique, indexed slug text field.

OptionDefaultDescription
sourceRequiredDocument-root field path. Dot paths support named groups and objects, not arrays or blocks.
slugifyBuilt-inOptional synchronous or asynchronous slugifier.
localizedfalseLocalizes the slug and, when enabled, its mode.
modeTogglefalseAdds persisted slugMode, defaulting to Auto.

Localized slugs require Payload localization and an effectively localized source; slug() checks at initialization, while standalone slugField() checks before preview and save.

slugField({
  source: "hero.title",
  localized: true,
  modeToggle: true,
});

Values and modes are per locale; fallback display does not persist another translation. Uniqueness follows adapter semantics (PostgreSQL scopes localized values by locale). Override slug()'s English labels through i18n.translations.

Custom slugification

Callbacks receive valueToSlugify, submitted data, and req. Return a non-empty URL-safe string or undefined; keep it deterministic and idempotent. Manual values also use the callback on save.

import { generateSlug, slugField } from "@klnap/payload-slug";

slugField({
  source: "title",
  slugify: ({ valueToSlugify }) => {
    if (typeof valueToSlugify !== "string") return undefined;
    const words = generateSlug(valueToSlugify).split("-");
    return words.filter((word) => !["a", "an", "the"].includes(word)).join("-") || undefined;
  },
});

Custom preview requires slug() and supports collections only. Its authenticated endpoint enforces collection and field read access; client data is untrusted and new-document conditional access fails closed. Global callbacks run on save, not preview.

generateSlug() can also be used independently:

import { generateSlug } from "@klnap/payload-slug";

generateSlug("Comment écrire une page", { locale: "fr" }); // "comment-ecrire-une-page"

Public API

  • slugField(options)
  • slug()
  • generateSlug(source, options?)

Types: SlugFieldArgs, SlugifyFunction, SlugMode, and GenerateSlugOptions; published declarations are authoritative.

Compatibility

Declared support
Payload and @payloadcms/ui>=3.94.1 <4
React and React DOM~19.0.9 || ~19.1.10 || ~19.2.9 || ^19.3.1
Node.js^22.23.5 || ^24.22.3 || >=26.13.1
Package formatESM only; Admin CSS must be supported by the application bundler.
DatabaseSQLite and PostgreSQL verified; localized uniqueness follows adapter semantics.

Collections and Globals support field saves; preview is collection-only. Next.js is not a peer dependency.

Documentation

Matched PostgreSQL runs measured equal SQL counts and paired medians within 0.78 ms; this is not a zero-overhead guarantee. See performance and the playground guide.

Payload docs: localization, Admin i18n, native slug. Package: security, changelog, MIT license.

Similar plugins