HtmlHead unstable
The HTML vocabulary's head: meta and link tags, JSON-LD blocks, and the
lang, dir, class and data-* attributes of <html> and
<body>, on top of the host-neutral Head.title and
Head.titleTemplate.
A component on Html returns these entries from its head option, next to
Head's. Importing this module registers merge as Html's fold and
installs the document head sync, so the runtime folds the placed tree into
one HtmlHead, which the browser host diffs against the document and the
server prints. An app that never imports HtmlHead folds and syncs only
the title and doesn't carry this module.
Merge rules
- A node's value is its own entries followed by its children's values, so
a descendant overrides its ancestors. Children are taken in the order of
their paths, never in document order. The node's
reduceentries then map the value, in written order. - The title,
lang,dirand<link rel="canonical">each have one entry. A meta tag is identified by itskey, else itsnameorproperty; a link by itskey, elserelplushref; a JSON-LD block by itskey, else its canonical form.data-*attributes have one entry per name. - Siblings that set one of these to the same value pass it up once. When
they set different values it's left unset, and reported as a
Head.Conflictunless areduceabove them sets it. classtokens are unioned in the order they were first contributed.- Titles follow
Head's template rule.
Example A layout template and a page
import { Head } from "effect/ui"import { HtmlHead } from "effect/ui/html"
const head = HtmlHead.merge({ entries: [Head.titleTemplate((title) => `${title} ยท App`), HtmlHead.lang("en")], children: [{ entries: [Head.title("Ada"), HtmlHead.meta({ name: "description", content: "Ada's page" })], children: [] }]})
head.title // => "Ada ยท App"head.html.lang // => "en"head.meta.map((meta) => meta.content) // => ["Ada's page"]Combinators
Folds a tree's entries into one HtmlHead, applying each node's reduce
entries bottom up. The Html vocabulary installs it as its merge.
Details
A node's value is its own entries, then its children's values ordered by
path, so a descendant's entry wins over its ancestor's per identity. The
title passes through the titleTemplate of each ancestor of the node that
set it, nearest first, as Head.resolveTitle says. A node's
HtmlHead.reduce and Head.reduce entries then map its value, in
written order. A template or a reduce that throws is reported to its
node's onError and skipped.
When siblings set the title, lang, dir, a data-* attribute or a
meta, link or JSON-LD entry of one identity to different values, it's left
unset. A reduce above them that sets it settles it, under its own node's
path. Otherwise onConflict gets a Head.Conflict with every path that
set it, sorted.
Signature
declare const merge: (tree: Head.Tree<Entry>, options?: Head.MergeOptions) => HtmlHeadConstants
Constructors
Adds class tokens to <body>. Strings are split on ASCII whitespace.
Signature
declare function bodyClass(tokens: string | readonly Array<string>): BodyClassSets a data-* attribute of <body>. "" means present and empty.
Signature
declare function bodyData<N extends string>(name: N & DataName<N>, value: string): BodyDataSets the dir attribute of <html>.
Signature
declare function dir(dir: "ltr" | "rtl"): DirAdds class tokens to <html>. Strings are split on ASCII whitespace.
Details
A token becomes the framework's once any entry contributes it, and the browser host removes it once no entry does, even if it was in the shell. Tokens no entry ever contributed are never touched.
Signature
declare function htmlClass(tokens: string | readonly Array<string>): HtmlClassSets a data-* attribute of <html>. "" means present and empty.
Details
When the last entry for a name goes away, the browser host removes the attribute, or restores the value the shell had.
Signature
declare function htmlData<N extends string>(name: N & DataName<N>, value: string): HtmlDataAdds a JSON-LD block.
Details
The value is an object, never a top-level array: use @graph for several
nodes. It's identified by key, else by its canonical form, and the host
rewrites a block only when its canonical form changes.
The type rejects functions, dates, bigints and symbols one level deep. When
the entry is built, deeper levels are checked too: an object whose prototype
isn't Object.prototype or null (a Date, Map, Option or class
instance), or an array holding NaN, Infinity or undefined, throws an
error naming its JSON path. In a head option that fails the instance.
The check runs in every build, not only in development: effect has no
development switch, and it shares the walk the canonical serialisation
makes anyway.
Signature
declare function jsonLd<A extends object>(value: JsonLdValue<A>, options?: { readonly key?: string;}): JsonLdExample Describing a person
import { HtmlHead } from "effect/ui/html"
const entry = HtmlHead.jsonLd({ "@context": "https://schema.org", "@type": "Person", name: "Ada <Lovelace>" })
entry.json // => '{"@context":"https://schema.org","@type":"Person","name":"Ada \\u003cLovelace>"}'Sets the lang attribute of <html>.
Signature
declare function lang(code: string): LangAdds a <link> tag.
Details
rel="canonical" has one entry. Other links are identified by key,
else by rel plus href.
Signature
declare function link(options: LinkAttributes & { readonly key?: string;}): LinkAdds a <meta> tag, named by name or by property.
Details
Its identity is key, else the name or property. http-equiv meta tags
and <meta charset> belong to the shell and aren't entries.
Signature
declare function meta(options: { readonly content: string; readonly key?: string; readonly name: string;} | { readonly content: string; readonly key?: string; readonly property: string;}): MetaMaps the folded head of the instance and everything its view placed, such
as deriving og:title from the title that won.
Details
It runs with the node's other reduce entries, Head.reduce's included,
in written order, after its own entries and its children's values are
merged. Head.merge ignores it.
Signature
declare function reduce(apply: (below: HtmlHead) => HtmlHead): ReduceExample Deriving og:title
import { Head } from "effect/ui"import { HtmlHead } from "effect/ui/html"
const ogTitle = HtmlHead.reduce((below) => ({ ...below, meta: [...below.meta, HtmlHead.meta({ property: "og:title", content: below.title ?? "App" })]}))
const head = HtmlHead.merge({ entries: [Head.titleTemplate((title) => `${title} ยท App`), ogTitle], children: [{ entries: [Head.title("Ada")], children: [] }]})
head.meta.map((meta) => meta.content) // => ["Ada ยท App"]Models
Class tokens for <body>.
Signature
interface BodyClass { readonly _tag: "BodyClass"; readonly tokens: readonly Array<string>;}A data-* attribute of <body>.
Signature
interface BodyData { readonly _tag: "BodyData"; readonly name: `data-${string}`; readonly value: string;}The dir attribute of <html>.
Signature
interface Dir { readonly _tag: "Dir"; readonly dir: "ltr" | "rtl";}One entry of the HTML vocabulary's head: Head's entries, or one of the
HTML ones.
Signature
type Entry = Head.Entry | Reduce | Meta | Link | Lang | Dir | JsonLd | HtmlClass | BodyClass | HtmlData | BodyDataClass tokens for <html>.
Signature
interface HtmlClass { readonly _tag: "HtmlClass"; readonly tokens: readonly Array<string>;}A data-* attribute of <html>.
Signature
interface HtmlData { readonly _tag: "HtmlData"; readonly name: `data-${string}`; readonly value: string;}The folded head of a tree, ready for a host.
Details
title has the templates applied. meta, links and jsonLd hold one
entry per identity, in the order each identity was first contributed: a
node's own entries as written, then its children's, ordered by path.
html and body hold the attributes set on those elements; class
lists tokens in the same order.
Signature
interface HtmlHead extends Head { readonly body: { readonly class: readonly Array<string>; readonly data: Readonly<Record<string, string>>; }; readonly html: { readonly class: readonly Array<string>; readonly data: Readonly<Record<string, string>>; readonly dir: "ltr" | "rtl" | undefined; readonly lang: string | undefined; }; readonly jsonLd: readonly Array<JsonLd>; readonly links: readonly Array<Link>; readonly meta: readonly Array<Meta>; readonly title: string | undefined;}A JSON-LD block, rendered as <script type="application/ld+json">.
Details
json is the canonical form: keys sorted recursively, undefined properties
dropped, and < escaped as \u003c, so it's safe inside a script tag and
two equal values always print the same.
Signature
interface JsonLd { readonly _tag: "JsonLd"; readonly json: string; readonly key: string | undefined; readonly value: object;}The lang attribute of <html>.
Signature
interface Lang { readonly _tag: "Lang"; readonly code: string;}A <link> tag.
Signature
interface Link { readonly _tag: "Link"; readonly attributes: LinkAttributes; readonly key: string | undefined;}The attributes a <link> tag may have.
Details
Links have no nonce and no event attributes.
Signature
interface LinkAttributes { readonly as?: string; readonly crossorigin?: string; readonly href: string; readonly hreflang?: string; readonly imagesizes?: string; readonly imagesrcset?: string; readonly media?: string; readonly rel: string; readonly sizes?: string; readonly type?: string;}A <meta> tag, named by name or by property.
Signature
interface Meta { readonly _tag: "Meta"; readonly content: string; readonly key: string | undefined; readonly name: string | undefined; readonly property: string | undefined;}Maps the folded head of its node, its own entries merged with its children's values, with every HTML field in view.
Signature
interface Reduce { readonly _tag: "HtmlHeadReduce"; readonly apply: (below: HtmlHead) => HtmlHead;}Utility Types
A data-* attribute name. data-eui is the framework's marker, so it's
excluded.
Signature
type DataName<N extends string> = N extends "data-eui" ? never : N extends `data-${string}` ? N : neverWhat a JSON-LD block accepts: an object whose properties aren't functions, dates, bigints or symbols. Deeper levels are checked when the entry is built.
Signature
type JsonLdValue<A extends object> = A & { [K in keyof A]: A[K] extends Function | Date | bigint | symbol ? never : A[K] }Utils
attributes
unstableThe attributes of a meta or link tag, in a stable order, without the marker.
Signature
declare const attributes: (entry: Meta | Link) => ReadonlyArray<readonly [string, string]>canonicalJson
unstableThe canonical JSON of a value: keys sorted recursively, undefined
properties dropped and < escaped as \u003c.
Signature
declare function canonicalJson(value: object): stringThe identity of a meta, link or JSON-LD entry: two entries with the same identity are the same entry. In one node's entries the later one wins.
Signature
declare function identity(entry: Meta | Link | JsonLd): string