Paths

Give each document its URL on your site with @cms/paths, and keep locale prefixes in step between the CMS and your website.

@cms/paths gives you path(), a slug that holds a document’s URL on your site (/about, /projects/acme). Editors type it in an input that shows the part of the URL they don’t own, and the stored value is always a path: lowercase segments with inner hyphens, starting with /, never ending with one.

          pnpm add @cms/paths
        
          // cms/page.ts
import { entity, text } from "@cms/config";
import { path } from "@cms/paths";

export const page = entity("page", {
  fields: [text("title", { required: true }), path("path", { searchable: true })],
});
        

path() is a plain field: there is nothing to register in plugins.

Options

Option Type Description
prefix ({ data }) => string The prefix the input shows before what editors type, from the document being edited. Only decorates the input: the server doesn’t check it.
localePrefix a locale-prefix scheme The site’s locale-prefix scheme. The input shows the document locale’s prefix and base, the server checks the stored path carries them, and a derived path gets them in front. Can’t be passed with prefix.
base string With localePrefix: where this entity’s paths start under the locale prefix (/projects). Default: /. A value that isn’t a path makes path() throw.
required boolean or a function Default: true, unlike other fields.
source, slugify, validate, searchable, … As on slug.

Locale prefixes

A site in several locales puts each document’s locale prefix in its path: /pt/about is the Portuguese about page. Describe the convention once with localePaths(), and both the path field and your website use that one value, so they can’t disagree:

          // cms/locales.ts
import { localePaths } from "@cms/paths/locales";

export const LOCALES = ["en", "pt"] as const;
export type Locale = (typeof LOCALES)[number];
export const DEFAULT_LOCALE: Locale = "en";

export const localeScheme = localePaths({
  locales: LOCALES,
  defaultLocale: DEFAULT_LOCALE,
  prefix: "except-default",
});
        
          // cms/project-page.ts
import { entity, text } from "@cms/config";
import { path } from "@cms/paths";
import { localeScheme } from "./locales";

export const project_page = entity("project_page", {
  translatable: true,
  fields: [
    text("title", { required: true }),
    path("path", { localePrefix: localeScheme, base: "/projects" }),
  ],
});
        

A Portuguese project now lives at /pt/projects/<slug> and an English one at /projects/<slug>. Editors type only <slug>: the input shows /pt/projects/ or /projects/ for the document’s locale. A locale’s index is just its prefix and base (/pt/projects), and a page with base: "/" left empty is the locale’s home (/pt, or / for English here).

The field reads its locales from the scheme, not from cms.config’s i18n, so build both from one constant, as in i18n and the scheme from one constant.

Modes

prefix is required: there is no default convention.

prefix English (default) Portuguese
"except-default" /about /pt/about
"always" /en/about /pt/about

Mapped prefixes

A locale’s prefix is its code (/pt). Map a locale to another prefix with segments:

          localePaths({
  locales: ["en", "pt-BR"],
  defaultLocale: "en",
  prefix: "except-default",
  segments: { "pt-BR": "br" }, // /br/about
});
        

localePaths() throws, so a mistake shows up when the config loads rather than as paths nobody can store, when:

  • a locale prefix, mapped or the bare code, isn’t a lowercase path segment (an unmapped pt-BR);
  • two locales share a locale prefix;
  • defaultLocale isn’t one of locales, or a key in segments isn’t;
  • the default locale has a mapping in "except-default", where it has no locale prefix;
  • a locale maps onto the default locale’s code in "except-default" (segments: { pt: "en" } with en the default), which would make /en/about a Portuguese path.

What the server rejects

The scheme turns into the field’s validate, which runs on every write (API, import, bulk edit) and live in the admin. For a document in locale L, a path is rejected when:

The path Message
doesn’t start with L’s prefix and base Start the path with /pt/projects/
has another locale prefix right after L’s prefix: a doubled prefix (/fr/fr/about in "always"), or a default-locale path under another locale (/pt/about on an English document in "except-default") Start the path with /fr/, not /fr/fr
belongs to a document whose locale isn’t in the scheme Locale "de" has no path prefix
belongs to a document with no locale, on an entity that isn’t translatable This path needs the document's locale

The admin’s translate action suggests the new translation’s path from locale codes, not from the scheme. With a mapped prefix (/br/about becomes /en/br/about), or when translating into the default locale in "except-default" (/pt/about becomes /en/about), edit the suggestion before saving: the check above rejects a wrong one.

A failing check is a 400 on the path field. A validate you pass yourself still runs, after the locale check.

Adding a locale to i18n but not to the scheme fails on that locale’s first write, with the third message, instead of storing an unprefixed path.

Derived paths

With localePrefix, a source returns only the part editors own. The field slugifies it (with your slugify, if you pass one) and puts the document locale’s prefix and base in front:

          path("path", {
  localePrefix: localeScheme,
  base: "/products",
  source: (doc) => doc.product?.handle ?? "", // → /pt/products/<handle>
});
        

Each translation’s derived path lands under its own prefix. A document written without a locale takes the scheme’s default locale.

On your website

@cms/paths/locales imports no React and no admin code, so your website’s server bundle can read paths back with the same scheme:

Method Returns "except-default" "always"
localeOf(path) The locale whose locale prefix starts path /pt/about → "pt", /about → "en" /pt/about → "pt", /about → null
strip(path) path without its locale prefix; the base stays, and a locale’s home becomes / /pt/projects/acme → /projects/acme, /pt → / /en/about → /about
prefix(locale, base?) Where locale’s paths start, base included, as the field builds it ("pt", "/projects") → /pt/projects, ("en") → / ("en") → /en
          // src/routes/$.tsx
import { localeScheme } from "~/cms/locales";

const locale = localeScheme.localeOf(pathname); // "en" | "pt": never null in "except-default"
const home = localeScheme.prefix(locale);
        

localeOf’s type returns null only for a scheme built with "always", where a null is a URL with no locale: redirect it to a prefixed one.

i18n and the scheme from one constant

          // cms.config.tsx
import { config } from "@cms/config";
import { DEFAULT_LOCALE, LOCALES } from "./cms/locales";

const LABELS = { en: "English", pt: "Português" } satisfies Record<(typeof LOCALES)[number], string>;

export default config({
  i18n: {
    locales: LOCALES.map((value) => ({ value, label: LABELS[value] })),
    defaultLocale: DEFAULT_LOCALE,
  },
  // ...
});
        

Changing a scheme doesn’t rewrite stored paths: existing documents keep theirs until edited.

Previous

Links

Next

Redirects