Skip to Content
Delmare Digital
Guides

Custom slugs

Replace the folderPath/pageSegment rule with your own buildSlug function — flat slugs, random IDs or a shallow hierarchy.

By default a slug is folderPath/pageSegment, for example appeals/2024/spring/my-page. The buildSlug option replaces that rule with any function you write.

Use it for:

  • Flat slugs — use the tree to organise content without affecting URLs.
  • Random IDs — generated slugs, with the update limitations below.
  • Custom patterns — slug schemes that depend on the collection, locale or other document data.

Signature

type BuildSlugFn = (args: {
  folderPath: string           // e.g. "appeals/2024"; '' when the page has no folder
  pageSegment: string          // The page's own URL segment
  doc: Record<string, unknown> // Incoming data, which may be a partial update
  operation: 'create' | 'update'
}) => string | Promise<string>

BuildSlugFn and BuildSlugArgs are exported from @delmaredigital/payload-page-tree. The function runs whenever the plugin would otherwise build a slug, and the tree's segment availability checks use it too.

Examples

Errors fall back to the default

If buildSlug throws or returns an empty value, the plugin uses the default folderPath/pageSegment slug instead. A throw is logged as [payload-page-tree] buildSlug callback error, falling back to default.

Optional and additive

Configurations without buildSlug behave exactly as before. URL history, cascading updates and redirects all keep working with custom slugs.

Edit this page on GitHub

On this page