Resource Route in Remix: What It Is and How It Works
resource route is a route file that handles data requests without rendering any UI. It is used to provide JSON or other data responses directly, acting like an API endpoint inside your Remix app.How It Works
A resource route in Remix works like a special endpoint that only sends data back to the client, without showing any page or user interface. Imagine it as a waiter who only brings you the food (data) but does not decorate the table (UI). This lets your app fetch data easily using standard HTTP methods like GET or POST.
When you create a resource route, Remix expects you to export functions like loader or action that handle requests and return data. The route file itself does not export a React component, so Remix knows not to render a page but just to send the data response.
This approach helps keep your data logic clean and separate from your UI, making your app faster and easier to maintain.
Example
This example shows a resource route that returns a JSON list of users when requested.
import { json } from '@remix-run/node'; export async function loader() { const users = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' } ]; return json(users); }
When to Use
Use resource routes when you need to provide data to your frontend or other clients without rendering a page. They are perfect for API-like endpoints inside your Remix app.
Common use cases include fetching JSON data for client-side JavaScript, handling form submissions with action functions, or serving files and other resources.
Resource routes help keep your app organized by separating data handling from UI rendering, making your code easier to read and maintain.
Key Points
- Resource routes do not export React components.
- They handle data requests using
loaderandactionfunctions. - They return data responses like JSON directly to the client.
- They are useful for API endpoints inside Remix apps.
- They keep data logic separate from UI rendering.