head
Converts a HeadDescriptor (title, description, Open Graph, canonical, JSON-LD) into the meta, links, styles and scripts arrays a TanStack route returns.
import { head } from '@salla.sa/twilight-theme-engine/tanstack';In plain words
The engine describes page tags as one plain object, a HeadDescriptor: { title, description, canonical, openGraph, twitter, jsonLd, … }. TanStack Router wants lists instead: a meta list, a links list, a scripts list.
head(descriptor) does that conversion. withHead() calls it for you, so use head directly when you build a descriptor yourself, in a route that has no route module.
Signature
function head(descriptor: HeadDescriptor): {
meta: Record<string, unknown>[];
links: Record<string, unknown>[];
styles: Record<string, unknown>[];
scripts: Record<string, unknown>[];
}Try it live
HeadDescriptor converted to the meta, links, styles and scripts arrays a TanStack route returns from head.Try this: turn on the Open Graph image: one image becomes two meta entries, og:image and og:image:width. The title is a meta entry too.head(descriptor): {…} 4 keys
meta: Array(2)
0: {…} 1 keys
1: {…} 2 keys
links: Array(1)
0: {…} 2 keys
import { createFileRoute } from '@tanstack/react-router';
import { head } from '@salla.sa/twilight-theme-engine/tanstack';
export const Route = createFileRoute('/{-$locale}/offers-landing')({
head: () =>
head({
title: 'Summer offers',
keywords: ['offers', 'sale'],
canonical: 'https://example.com/ar/offers',
}),
component: OffersLanding,
});
Example
// Declared in app/routes.ts: route('/gift-cards', 'gift-cards.tsx')
import { createFileRoute } from '@tanstack/react-router';
import { head } from '@salla.sa/twilight-theme-engine/tanstack';
export const Route = createFileRoute('/{-$locale}/gift-cards')({
head: () =>
head({
title: 'Gift cards',
description: 'Send a gift card by email in a minute.',
openGraph: { type: 'website', title: 'Gift cards' },
robots: 'index, follow',
}),
component: GiftCards,
});
function GiftCards() {
return <h1>Gift cards</h1>;
}
How it behaves
titlebecomes a{ title }entry inmeta, which TanStack renders as<title>.description,keywords(an array is joined with ", ") androbotsbecome named meta tags.openGraphbecomesog:*property tags: each image addsog:image, plusog:image:width,og:image:heightandog:image:altwhen given, andpublishedTime/modifiedTimebecomearticle:published_time/article:modified_time.twitterbecomestwitter:*name tags.canonicalbecomes<link rel="canonical">, and eachalternateLanguagesitem a<link rel="alternate">withhrefLang.jsonLdbecomes one<script type="application/ld+json">holdingJSON.stringify(jsonLd).importMapentries becometype="importmap"scripts, placed before yourscripts.meta,links,stylesandscriptsare appended as given.createRouter()installs this function as the engine's head adapter, so route modules andresolveHead()produce this shape.
Gotchas
JSON-LD is not escaped for HTML. In the server render TanStack writes a script's body as raw HTML, so a string in
jsonLdcontaining</script>(in a product description, say) ends the element early. When the data is not yours, add the script throughscriptswith<escaped:{ type: 'application/ld+json', children: JSON.stringify(data).replace(/</g, '\\u003c') }.linksitems are passed through untouched, so ahreflangkey keeps that spelling and React warns in development that the prop should behrefLang. Put language links inalternateLanguages, which writeshrefLang.
Related
Wraps a route module's head function into the head option of a TanStack route, with an optional step to change the result.
head (Next.js)Converts a HeadDescriptor into a Next.js App Router Metadata object; part of the engine's experimental, unfinished Next.js adapter.
HeadDescriptorThe plain object a route's head() returns: title, description, Open Graph and Twitter cards, hreflang links, JSON-LD and extra tags.