Loaders are useful for one-time loading of data from the server to the client and can only be used in routes in your app
directory.
For a more general purpose data solution, see load and query.
Loaders run on the server, either once during build-time for normal routes, or once on each request for +ssr
routes.
Loaders and their imports are removed from client bundles, so you can access private information from within the loader. The data returned from the loader will be passed to the client, and so should be clear of private information. To access the loader data on the client, you must use the useLoader
hook:
The useLoader hook is automatically type safe.
Loaders receive a single argument object:
The params
key will provide values from any dynamic route segments:
app/user/[id].tsx
The path
key is the fully resolved pathname:
app/user/[id].tsx
Only for ssr
type routes. This will pass the same Web standard Request object as API routes.
Most JavaScript values, including primitives and objects, will be converted to JSON.
You may also return a Response
:
A final handy pattern for loaders is throwing a response to end it early:
You can combine this with the redirect utility function:
Edit this page on GitHub.