In this lesson, we'll explore how to efficiently fetch data for our routes in Remix using loaders
. We'll delve into the best practices for maximizing the benefits of loaders
in our Remix app.
How and when do loaders work
Remix primarily relies on server-side rendering (SSR). SSR is a technique where the server prepares the HTML content by executing the necessary code and fetching the data before sending it to the client's browser.
In Remix, the loader function handles data fetching on the server. A loader
is tied to a specific route in the application; its job is to get all the data needed for that route. This means when a user navigates to a route, the loader
for that route runs on the server, fetches data, and then the data is used to render the page.
Let's walk through a basic example of how to use a loader
in Remix. We will create a simple route that says "Hello World!". To start, we’ll create a new file named hello.tsx
inside the src/routes
directory of our Remix project. Thanks to Remix's file-based routing system, this new file will automatically correspond to a route in the application, accessible at http://localhost:3000/hello
.
Initially, this hello.tsx
file doesn't have any content, so if we navigate to /hello
in the browser, we'll see a blank page.