Sub topic 15
Titles, meta tags, and stylesheets per route
This chapter assumes you have a working framework-mode app with a root route, nested routes, and loaders. Each route module can describe its own <head> content. You do this with two exports, meta and links, and React Router combines them for the matched routes.
How head content is assembled
The root route renders <Meta /> and <Links /> inside <head>. Without them, the exports below do nothing.
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 />;
}The meta export
meta returns an array of descriptors. Each descriptor becomes one tag. It receives data, the loader's return value for that route.
import type { Route } from "./+types/detail";
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 },
];
}Use { title } for the title element, { name, content } for standard meta tags, and { property, content } for Open Graph tags.
Check data before using it. If the loader threw a 404 response, data is undefined for that route.
Meta from several routes
When several routes match a URL, the deepest route that exports meta decides the result. Its array replaces the parents' arrays. React Router does not merge them for you.
To keep a parent's tags, read them from matches and spread them in:
export function meta({ matches }: Route.MetaArgs) {
const parentMeta = matches.flatMap((match) => match?.meta ?? []);
return [...parentMeta, { title: "New contact" }];
}Because the later title appears after the parent's, you may end up with two title descriptors. Filter out the parent's title if that matters to you.
The links export
links returns descriptors for <link> tags. The most common use is a stylesheet scoped to a route.
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" },
];The ?url suffix makes the bundler give you the built file's URL instead of injecting the CSS itself. The stylesheet link is rendered only while that route is matched. Links from all matched routes are combined, so a parent layout's stylesheet stays on the page when a child route is active.
Common errors
- Forgetting
<Meta />or<Links />in the rootLayout, so nothing appears. - Writing a
<title>element inside a route component instead of usingmeta. - Reading
data.contactwithout handling the error case. - Expecting parent meta tags to survive a child's
metaexport. - Importing CSS without
?urland then passing the result ashref.
Exercise
A child route exports meta returning only [{ title: "Edit" }]. Its parent layout exports meta with a description tag. Which tags appear in the document head, and how would you also keep the description?
Show answer
Only the title "Edit" appears, because the deepest route's meta replaces the parent's. To keep the description, read matches in the child's meta, collect each match's meta array, and spread it before the child's own title.