Links

Add a field that links to one of your pages or to a URL with @cms/links, and read it on your website.

@cms/links gives you link(), a field for the most common edit on a site: a button or a menu item that points at one of your pages, or at a URL. Editors fill it from one search box that finds your pages and takes a pasted URL.

          pnpm add @cms/links
        
          // cms/navbar.ts
import { array, entity } from "@cms/config";
import { link } from "@cms/links";

export const navbar = entity("navbar", {
  global: true,
  fields: [
    link("cta", { to: ["page", "product_page"], label: "required", newTab: true }),
    array("items", { of: link("item", { to: ["page", "product_page"] }) }),
  ],
});
        

link() is a plain field, made from union, object, relation, text and boolean: there is nothing to register in plugins.

Options

Option Type Description
to entity or name, or a list of them The entities an internal link may point at. A name that isn’t an entity makes config() throw.
label "required" | "optional" | false Whether the link has a label, and whether editors have to fill it. Default: "optional".
newTab boolean Adds an “Open in new tab” toggle. Default: false.
allow RegExp[] Values an external link accepts beyond the built-in URL forms, like [/^#chat$/] for a chat launcher. A match is accepted even when it’s a path like /api/feed.xml.
origins string[] The site’s own origins, like ["https://site.com"]. An external link to one of them is refused: editors pick the page as an internal link.
required boolean or a function As on every field.
title, description string As on every field.

Keep to in a constant the project imports where it needs the same list, such as a sitemap that lists every page a link can reach.

What it stores

_type Fields Checked on save
internal label?, target (a relation to one of to), newTab? target is filled and is one of to
external label?, url, newTab? url starts with https://, http://, mailto: or tel: (a web address must parse, with a host), or matches allow. A path like /about, or a URL on one of origins, is refused with “Use an internal link”

An internal link follows its page when the page’s path changes and shows up in the page’s Used in list, so a link into the site has to be internal. The checks run on the server and live in the admin. target and url are required through a function, so they stay optional in the client type: a target can also be deleted after the link was saved.

The input

The field shows an Add link button. It opens a search box:

Editors type They see Picking it stores
about matching documents of the to entities, grouped by entity, with their path an internal link to the document
https://…, mailto:…, tel:…, or an allow match Use this URL an external link
/about, or a URL on one of origins a hint to search for the page instead nothing
anything else the matches, and a hint about the URL forms nothing

The search reads each entity’s searchable fields, as the admin’s global search does: mark the title searchable: true on every entity in to. A translatable entity is searched in the locale of the document being edited.

Once picked, the link shows as a card with the document’s title and path, or the URL, with Edit and Clear. The label and the new-tab toggle sit under it. In array(link()), Add item adds an empty link and opens it.

@cms/links/client has the resolve selection that reaches each target’s path, and the type of what comes back. The engine has no path field of its own, so name yours:

          import { linkListResolve, linkResolve, type ResolvedLink } from "@cms/links/client";

const { data: navbar } = await cms.get("navbar", {
  resolve: {
    cta: linkResolve("path"), // one link, or one inside an object
    items: linkListResolve("path"), // array(link())
  },
});
        

The engine addresses an array of unions by variant, which is why array(link()) takes linkListResolve. The typed client infers the result: an internal link’s target comes back as { id, _entity, path }.

ResolvedLink<"path"> is that shape for your own components:

          type ResolvedLink<TPathField extends string = "path"> =
  | { _type: "internal"; label?: string | null; newTab?: boolean | null;
      target?: { id: string; _entity: string; path?: string | null } | null }
  | { _type: "external"; label?: string | null; newTab?: boolean | null; url?: string | null };
        

path is missing when the target couldn’t be resolved: it was deleted, or the API key can’t read it.

Building the href

Turning a link into an href stays in your project, since locale and region prefixes are your site’s rules:

          import type { ResolvedLink } from "@cms/links/client";

export function CMSLink({ link, children }: { link: ResolvedLink; children?: React.ReactNode }) {
  const href = link._type === "internal" ? link.target?.path : link.url;
  if (!href) return null;
  const newTab = link.newTab ? { target: "_blank", rel: "noopener noreferrer" } : {};
  return (
    <a href={href} {...newTab}>
      {children ?? link.label}
    </a>
  );
}
        

Translations

An internal link’s target is a relation, so translating a document points it at the target’s counterpart in the new locale, like any other relation. A target with no counterpart is kept.

A project that wrote its own link field stores the same shape:

          // before
union("cta", {
  of: [
    object("internal", { fields: [text("label"), relation("target", { to: INTERNAL_TARGETS })] }),
    object("external", { fields: [text("label"), text("url", { validate: isUrl })] }),
  ],
});

// after
link("cta", { to: INTERNAL_TARGETS });
        

With the same field name, the same targets and the default options, the swap needs no migration: cms migration generate finds no change. label: "required" or newTab: true change the stored shape, so generate a migration after adding them.

A hand-written field may hold external URLs that point into the site, like /about. They still read fine, but saving the document refuses them until an editor picks the page as an internal link. To find them before the swap, query the field for url values that start with / or one of your origins.

On the website, replace the hand-written resolve tree with linkResolve(...) or linkListResolve(...) and the hand-typed link type with ResolvedLink.

Previous

Cloudflare

Next

Paths