Sub topic 4
The root route: document shell, Links, Scripts
This chapter assumes you have a project from create-react-router, so app/root.tsx already exists. It also assumes you know React components, JSX, and the basics of an HTML document (html, head, body). Route config was covered earlier. Here you will look at the one route that every URL passes through.
What the root route is
app/root.tsx is the parent of every route in app/routes.ts. You don't register it. The framework always uses it. It has two jobs:
- It renders the HTML document, meaning everything from
<html>to</html>. - It renders an
<Outlet />where the matched child route appears.
React Router doesn't use a separate index.html template. The document is a React component, so you can use props, hooks, and exports to control it.
flowchart TDReplace the placeholder above with the real diagram below.
The Layout export and the default export
A root module can export both a Layout function and a default component. They have different roles.
Layoutis the document shell. It receiveschildrenand renders<html>,<head>, and<body>.- The default export (often called
App) is the root route's own UI. It usually returns<Outlet />.
Here is a complete shell.
import type { ReactNode } from "react";
import {
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "react-router";
import type { Route } from "./+types/root";
import stylesheet from "./app.css?url";
export const links: Route.LinksFunction = () => [
{ rel: "stylesheet", href: stylesheet },
];
export function Layout({ children }: { children: 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 />;
}children is whatever the router renders inside the document. Normally that is App. If an error occurs, it is the ErrorBoundary output, and during a client-only first load it is HydrateFallback. The same shell wraps all three, so error pages keep your fonts, styles, and scripts.
What each piece does
| Component | Renders | Notes |
|---|---|---|
| `Meta` | `<title>`, `<meta>` and similar tags | Collects each matched route's `meta` export |
| `Links` | `<link>` tags | Collects each matched route's `links` export |
| `ScrollRestoration` | Nothing visible | Saves and restores scroll position across navigations |
| `Scripts` | `<script>` tags | Loads the JavaScript that hydrates the page |
Links
The links export returns an array of link descriptors. Each descriptor becomes a <link> element. Any route can export links, and <Links /> renders the descriptors from every route that currently matches. When you navigate away from a route, its stylesheets leave the page. The root's descriptors stay for every page.
Import CSS with the ?url suffix, as above. Vite then gives you the final fingerprinted URL, and you decide where it is linked. A plain import "./app.css" also works, but it gives you less control over placement.
Scripts
Scripts is the most important element in the shell. The server sends complete HTML, and Scripts adds the JavaScript that makes it interactive. This is called hydration.
If you remove <Scripts />, the app still renders on first load. Nothing is broken visibly. But Link clicks become full page loads, and Form submissions become plain browser posts. Pending state and fetchers stop working. This can look like "the app works but feels slow", so check for Scripts first when interactivity disappears.
Put Scripts last in body, after ScrollRestoration. Use each of these components once. Rendering Meta, Links, or Scripts twice duplicates tags.
Document-level concerns
Some settings belong in the shell, not in a route.
langon<html>. Screen readers and browsers use it to pick pronunciation and spelling rules.charSetandviewportmeta tags. These are static, so write them by hand.- Global fonts and the base stylesheet. Add them through the root
linksexport.
Per-page titles and descriptions belong in each route's meta export. Meta merges them. A later chapter covers meta in detail.
Common errors
- Hydration mismatch warnings. The server HTML and the first client render must be identical. Avoid
Date.now(),Math.random(), orwindowchecks inLayout. - Missing styles on error pages. This happens when the shell is written inside
Appinstead ofLayout. An error boundary then renders without<Links />. - A blank page with no error. This usually means
Applacks an<Outlet />, so child routes have nowhere to render. - Two
<html>elements. IfAppalso returns<html>, remove it.Layoutowns the document.
Exercise
A teammate moves the <html>, <head>, and <Scripts /> elements out of Layout and into the default App component. The app works, but any thrown 404 shows an unstyled page with no interactivity. Why?
Show answer
The ErrorBoundary export renders in place of App, so it no longer gets the document shell. Without <Links /> the stylesheet is missing, and without <Scripts /> the page is not hydrated. Putting the shell in the Layout export fixes it, because Layout wraps App, ErrorBoundary, and HydrateFallback.