Sub topic 15
Titles, meta tags, and stylesheets per route
This chapter assumes you have a framework-mode project with app/routes.ts and a root route, and that you know how loaders return data to components. You will learn how each route sets its own <title>, meta tags, and stylesheets, and how React Router merges them as the URL changes.
Where head content comes from
In framework mode you don't write <title> or <link> tags inside route components. Each route module exports two functions instead:
metareturns descriptors for<title>,<meta>, and other head tags.linksreturns descriptors for<link>tags, mostly stylesheets and icons.
The root route renders two components that turn those descriptors into real tags: <Meta /> and <Links />. Both go inside <head>.
import {
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "react-router";
export function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}
export default function App() {
return <Outlet />;
}If <Meta /> or <Links /> is missing, the exports in every route are silently ignored.
The meta export
A meta function returns an array of descriptor objects. Each object becomes one tag.
import type { Route } from "./+types/detail";
import { data } from "react-router";
import { getContact } from "~/contacts.server";
export async function loader({ params }: Route.LoaderArgs) {
const contact = await getContact(params.contactId);
if (!contact) {
throw data("Contact not found", { status: 404 });
}
return { contact };
}
export function meta({ data }: Route.MetaArgs) {
if (!data) {
return [{ title: "Contact not found" }];
}
return [
{ title: `${data.contact.name} | Contacts` },
{ name: "description", content: `Details for ${data.contact.name}` },
{ property: "og:title", content: data.contact.name },
];
}
export default function ContactDetail({ loaderData }: Route.ComponentProps) {
return <h1>{loaderData.contact.name}</h1>;
}The descriptor shapes are:
| Descriptor | Rendered tag |
|---|---|
| `{ title: "Home" }` | `<title>Home</title>` |
| `{ name: "description", content: "..." }` | `<meta name="description" content="...">` |
| `{ property: "og:title", content: "..." }` | `<meta property="og:title" content="...">` |
| `{ tagName: "link", rel: "canonical", href: "..." }` | `<link rel="canonical" href="...">` |
| `{ "script:ld+json": { ... } }` | A JSON-LD script tag |
meta receives data (the route's loader data), params, location, and matches. It runs on the server for the first request and in the browser on later navigations, so keep it free of server-only imports.
Why data can be undefined
When the loader throws, as it does for the 404 above, there is no loader data. The meta function still runs, and data is undefined. Always handle that case, as the example does. Reading data.contact.name without a check would crash while the error boundary renders.
How parent and child meta combine
Every matched route can export meta. React Router uses the result from the deepest (leaf) route that exports one, and it does not merge arrays from parents automatically. A child that returns only { title } therefore drops a parent's description tag.
To keep parent tags, read them from matches and spread them in:
import type { Route } from "./+types/detail";
export function meta({ matches, data }: Route.MetaArgs) {
const parentMeta = matches
.slice(0, -1)
.flatMap((match) => match?.meta ?? []);
return [
...parentMeta,
{ title: data ? `${data.contact.name} | Contacts` : "Contact" },
];
}Because the parent list can include a title, the leaf's own title must come last. Browsers use the last <title> in the document, so the later entry wins in practice.
The links export
links returns an array of <link> descriptors. Unlike meta, links from all matched routes are combined, and a route's links are removed when you navigate away from it. That gives you per-route CSS without leaking styles between pages.
With Vite, add ?url to a CSS import to get its built URL instead of injecting it globally:
import type { Route } from "./+types/detail";
import detailStyles from "./detail.css?url";
export const links: Route.LinksFunction = () => [
{ rel: "stylesheet", href: detailStyles },
{ rel: "icon", href: "/favicon.ico", type: "image/x-icon" },
];Put shared styles in root.tsx so every page gets them. Put page-specific styles in the route that needs them.
Preloading and other link types
Descriptors also cover rel: "preload", rel: "preconnect", and rel: "canonical". A preload for a font looks like this:
export const links: Route.LinksFunction = () => [
{
rel: "preload",
href: "/fonts/inter.woff2",
as: "font",
type: "font/woff2",
crossOrigin: "anonymous",
},
];Common mistakes
- Forgetting
<Meta />or<Links />in the root. Nothing renders and no error appears. - Writing a
<title>inside a component. It can duplicate or conflict with themetaoutput. - Importing CSS without
?urland expecting it to be route-scoped. A bareimport "./x.css"is bundled as ordinary CSS and may apply beyond the route. - Assuming parent meta carries over. The leaf route's result replaces it unless you merge via
matches. - Skipping the undefined check on
data. A thrown loader makesdataundefined.
Exercise
A /contacts/:contactId route exports meta that returns only [{ title: "Contact" }]. Its parent layout route exports meta that returns a description tag. The page shows a title but no description. Why, and how do you fix it?
Show answer
React Router uses the meta result from the deepest matching route and does not merge parent arrays. The leaf's result replaced the parent's. Fix it by reading matches in the leaf's meta, spreading the parent routes' meta entries into the returned array, and putting the leaf's own title last.
Exercise
You want a stylesheet applied only while the user is on the contact detail page. Which export do you use, and how do you import the CSS file?
Show answer
Export links from the detail route module and import the stylesheet with the ?url suffix, for example import detailStyles from "./detail.css?url". Return { rel: "stylesheet", href: detailStyles } from links. React Router removes the link when the route is no longer matched.