T
TanStack5mo ago
fair-rose

Loader context missing in production

Hey everyone! I usually don’t reach out for support unless I’ve been running in circles, and at this point, I’ve exhausted my options. I’m running into an issue where some loader data isn’t available in production builds, even though everything works perfectly in development. I’m using the latest version of Start along with BetterAuth for authentication. Here’s the loader used across all relevant pages:
loader: async ({ context }) => {
return {
user: context.user,
};
},
loader: async ({ context }) => {
return {
user: context.user,
};
},
The problem is that on my index route (/), the user isn’t passed through in production, but it is in development. Other routes like /account use the exact same loader setup, and they work fine in both environments. I’m accessing the data like this:
const { user } = Route.useLoaderData();
const { user } = Route.useLoaderData();
Has anyone run into something similar or have any insights on what might be going wrong? Appreciate any help!
5 Replies
fair-rose
fair-roseOP5mo ago
With some further testing, I discovered that the issue seems specific to the index route itself. When I moved the page from / to /testing, the context loaded correctly and user was available. But when I replaced the index route (/) with a completely basic test page using the same loader setup, the context did not load. Here's the code for the test page at /:
import { createFileRoute } from "@tanstack/react-router";

export const Route = createFileRoute("/")({
component: RouteComponent,
loader: async ({ context }) => {
return {
user: context.user,
};
},
});

function RouteComponent() {
const { user } = Route.useLoaderData();

return <div>{user?.id}</div>;
}
import { createFileRoute } from "@tanstack/react-router";

export const Route = createFileRoute("/")({
component: RouteComponent,
loader: async ({ context }) => {
return {
user: context.user,
};
},
});

function RouteComponent() {
const { user } = Route.useLoaderData();

return <div>{user?.id}</div>;
}
So it looks like the loader context is getting skipped or ignored specifically on the index route in production. Still no idea why.
national-gold
national-gold5mo ago
can you please create a minimal complete example repo and create a github issue for this?
fair-rose
fair-roseOP5mo ago
Sure! Looks like I may have figured it out while creating a reproduction repo. The issue happens when / is included in the server.prerenderer routes in my app.config.ts. Removing / from the prerender list fixed the missing context issue in production. Can’t believe I overlooked that! I would imagine this IS in this case, intended behavior?
national-gold
national-gold5mo ago
if you prerender, the loader would not run when serving the prerendered route. yes
fair-rose
fair-roseOP5mo ago
Yeah, I had it in there for some initial testing, and didn't even think of that when this issue popped up. Thank you very much! Probably wouldn't have realized if I didn't start creating the example repo.

Did you find this page helpful?