Remix: The root route

The root route file, app/root.jsx, is the global layout of the entire app.

It’s responsible for rendering the “outer” HTML structure, and embedding scripts and styles.

Here’s the one created by the default app:

import { cssBundleHref } from '@remix-run/css-bundle'
import {
  Links,
  LiveReload,
  Meta,
  Outlet,
  Scripts,
  ScrollRestoration,
} from '@remix-run/react'

export const links = () => [
  ...(cssBundleHref ? [{ rel: 'stylesheet', href: cssBundleHref }] : []),
]

export default function App() {
  return (
    <html lang='en'>
      <head>
        <meta charSet='utf-8' />
        <meta name='viewport' content='width=device-width, initial-scale=1' />
        <Meta />
        <Links />
      </head>
      <body>
        <Outlet />
        <ScrollRestoration />
        <Scripts />
        <LiveReload />
      </body>
    </html>
  )
}

NOTE: if you see errors (red lines) in VS Code, add this jsconfig.json file to the root of the project:

{
  "compilerOptions": {
    "jsx": "react-jsx"
  },
}

You can customize anything in here, but this is a starting point that will provide the general HTML structure of your app.

You can see that inside the <head> tag we embed some meta tags and then we have the <Meta /> and <Links /> components which are imported from @remix-run/react.

Any route can export 2 functions called meta and links which return an array of metatags and import links (favicon, stylesheet, …) to add to the document head, and those will be embedded by those components.

This route itself exports a links function which embeds the optional CSS bundler that Remix provides, and that is added to the page by the <Links /> component.

Then inside the <body> tag we first embed <Outlet /> which is basically the “space” for the child route to be embedded in. Here’s where the content will appear.

Then we embed <ScrollRestoration /> which is responsible for making sure that if the page reloads we keep the scroll position in the right place, <Scripts /> which add the client JavaScript (React and any other library you need), and <LiveReload /> which is responsible for adding the scripts to implement the live reloading feature. You know, when you save a file in the editor and the UI refreshes in the browser through “hot module reloading”.

We’ll later see how to modify this to include custom CSS and adding Tailwind support.

Lessons in this unit:

0: Introduction
1: Create your first Remix app
2: ▶︎ The root route
3: File based routing
4: Linking your pages
5: Styling with CSS and Tailwind
6: Create a navigation
7: Dynamic routes and nested routes
8: Connecting a database
9: Data mutations using forms and actions
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. Launching May 21, 2024. Join the waiting list!