Sub topic 4
The root route: document shell, Links, Scripts
This chapter assumes you can read React components and TypeScript, know what a <head> and <body> are, and have a project from the previous chapters with app/routes.ts in place.
What the root route does
In framework mode, app/root.tsx is the parent of every route. React Router renders it on every request. It owns the HTML document: the <html>, <head>, and <body> tags. No other file in your app needs to write them.
The root route exports up to three pieces:
Layout: the document shell. It wraps the root component and the rootErrorBoundary.default: the root component. It usually renders only<Outlet />.ErrorBoundary: the fallback for errors that no deeper route handles.
A working root.tsx
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 />;
}What each component does
| Component | Where it goes | Purpose |
|---|---|---|
| `Meta` | `<head>` | Renders the title and meta tags from the `meta` exports of matched routes. |
| `Links` | `<head>` | Renders `<link>` tags from the `links` exports, such as stylesheets and icons. |
| `Outlet` | Root component | Marks where the matched child route renders. |
| `ScrollRestoration` | End of `<body>` | Restores scroll position when users move back and forward. |
| `Scripts` | End of `<body>` | Loads the JavaScript that hydrates the page. |
Hydration means the browser JavaScript attaches to HTML the server already sent, so links and forms become client-side navigation. Without Scripts, the page still shows, but it behaves like a plain multi-page site.
Adding a stylesheet
Export links from the root route. Links renders what it returns.
import type { Route } from "./+types/root";
import appStyles from "./app.css?url";
export const links: Route.LinksFunction = () => [
{ rel: "stylesheet", href: appStyles },
{ rel: "icon", href: "/favicon.ico" },
];The ?url suffix makes the bundler return the built file's URL instead of inlining the CSS. Put this export in root.tsx alongside the code above.
Common errors
- Missing
LinksorMeta: styles and titles never appear, with no error message. - Missing
Scripts: the page renders but nothing is interactive. - Putting
Scriptsin<head>: keep it at the end of<body>, after the content. - Writing
<html>in the root component: the rootErrorBoundaryrenders without the root component, so put the document tags inLayout. Otherwise error pages can lose their document. - Using hooks that need loader data in
Layout:Layoutalso renders during errors, when route data may not exist. Keep it simple.
Exercise
A page renders styled and correct, but links and forms cause full page reloads, and the browser console shows no client-side navigation. Which root route piece is most likely missing, and where does it belong?
Show answer
The Scripts component is missing. It belongs at the end of <body> in Layout, after {children}. Without it the browser never loads the JavaScript that hydrates the page, so navigation stays as plain document requests.
Exercise
Why should <html>, <head>, and <body> live in Layout and not in the default App component?
Show answer
The root ErrorBoundary renders in place of App when an error occurs. Layout wraps both, so the document shell appears in normal pages and in error pages.