Why does returning plain object from a loader work? #9837
-
When using loaders in Remix, one should -- according to the documentation -- return a response object: export async function loader() {
const users = await db.users.findMany();
const body = JSON.stringify(users);
return new Response(body, {
headers: {
"Content-Type": "application/json",
},
});
} There is also a helper function import { json } from "@remix-run/node"; // or cloudflare/deno
export const loader = async () => {
const users = await fakeDb.users.findMany();
return json(users);
}; By mistake, I forgot to wrap the returned object in a response: export const loader = async () => {
const users = await fakeDb.users.findMany();
return users;
}; I got no type error though, and the loader worked just fine with I assume, that the object is automatically serialized and wrapped in a Is this a behavior I can rely on? Or is this considered bad practice and I should wrap the returned object in |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Remix is doing |
Beta Was this translation helpful? Give feedback.
Remix is doing
json(loaderReturnData)
for you if you don't return a Response.