buildBaseHead, localeToOgLocale
Builds the store-wide head defaults (title, description, canonical, Open Graph, Twitter card) from the store settings; the root route already applies them.
import { buildBaseHead, localeToOgLocale } from '@salla.sa/twilight-theme-engine/utils/baseHead';In plain words
Every page of a store shares some head information: the store name as the title, its description, its logo as the picture when a link is shared. buildBaseHead(settings, locale) builds that HeadDescriptor from the store settings. The engine's root route already calls it for every page, so a theme rarely needs to.
localeToOgLocale('ar') turns a language code into the form Open Graph expects: 'ar_AR'.
Signature
function buildBaseHead( settings: StoreContext | null | undefined, locale?: string, // default 'ar' path?: string // without the locale, e.g. '/cart' ): HeadDescriptor function localeToOgLocale(locale: string): string // 'ar' → 'ar_AR', 'en' → 'en_US', any other 'xx' → 'xx_XX', '' → 'ar_AR'
Try it live
- store.url
- https://demostore.salla.sa/en/dev-vgckq3fssfhjewwi/
- canonical
- https://demostore.salla.sa
- openGraph.url
- https://demostore.salla.sa
- localeToOgLocale("en")
- en_US
buildBaseHead(settings, locale, path): {…} 5 keys
openGraph: {…} 7 keys
twitter: {…} 5 keys
import { buildBaseHead } from '@salla.sa/twilight-theme-engine/utils/baseHead';
import type { HeadDescriptor } from '@salla.sa/twilight-theme-engine/utils/head';
import type { StoreContext } from '@salla.sa/twilight-theme-engine/api/store';
export function storeDefaults(settings: StoreContext, locale: string): HeadDescriptor {
return buildBaseHead(settings, locale);
}
Example
import { useTwilight } from '@salla.sa/twilight-theme-engine';
import { useTranslation } from '@salla.sa/twilight-theme-engine/i18n';
import { buildBaseHead } from '@salla.sa/twilight-theme-engine/utils/baseHead';
/** How a shared link to this store looks: the store's own Open Graph defaults. */
export function SharePreview() {
const { settings } = useTwilight();
const { locale } = useTranslation();
const { openGraph } = buildBaseHead(settings, locale);
return (
<figure className="share-preview">
{typeof openGraph?.images === 'string' && <img src={openGraph.images} alt="" width={120} />}
<figcaption>{openGraph?.title}</figcaption>
</figure>
);
}
How it behaves
It returns
{}whensettings.storeis missing.titleisstore.meta.title, elsestore.name.descriptionisstore.meta.description, elsestore.description.keywordsisstore.meta.keywords. Empty strings count as missing.The base URL is the origin of
store.url. Withoutpath,canonicalandopenGraph.urlare that origin. Withpath,canonicalis origin + path andopenGraph.urlis origin +/<locale>+ path.openGraphis{ type: 'website', siteName, title, description, url, locale, images: store.logo }andtwitteris{ card: 'summary_large_image', title, description, site, images: store.logo }, wheresiteisstore.social.twitterwith an@added when it has none.alternateLanguages(frombuildHreflangAlternates) is included only whenpathis given.createTwilightRootRoute()callsbuildBaseHead(settings, locale)with no path and adds its own tags on top, for every page.localeToOgLocalelower-cases the code and keeps only the language part ofen-GBorar_SA, mapsaranden, and doubles anything else (fr→fr_FR).
Gotchas
For a store on a shared Salla host the username is lost. The demo store's
store.urlishttps://demostore.salla.sa/ar/dev-vgckq3fssfhjewwi/, so itscanonicalishttps://demostore.salla.sa, an address that is not this store.Because the root route passes no path, every page gets a canonical link to the store origin. A route that sets its own
canonicaladds a second canonical link (TanStack Router keeps every link), and a route that does not keeps the home page as its canonical.descriptionis used as the store sends it. The demo store's description is HTML (<p class="ql-direction-rtl">…), so that markup ends up as text inside the description,og:descriptionandtwitter:descriptiontags.twitter.siteonly prepends@. The demo store sendsstore.social.twitteras a profile URL,https://x.com/SallaApp, which becomes@https://x.com/SallaApp.Pass
pathwithout the locale: with/ar/cart,openGraph.urlbecomes…/ar/ar/cart.localeToOgLocaleguesses the region by doubling the code, which is not always a real locale:ur→ur_UR,zh-Hant→zh_ZH, anden-GB→en_US.
Related
Lays additions over a base HeadDescriptor: tag lists are appended, Open Graph and Twitter merged one level, every other field replaced.
buildHreflangAlternatesBuilds the hreflang links that tell search engines the same page exists in each store language, plus an x-default.
HeadDescriptorThe plain object a route's head() returns: title, description, Open Graph and Twitter cards, hreflang links, JSON-LD and extra tags.