#Loader context missing in production

6 messages · Page 1 of 1 (latest)

red fjord
#

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,
    };
},

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();

Has anyone run into something similar or have any insights on what might be going wrong?

Appreciate any help!

red fjord
#

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>;
}

So it looks like the loader context is getting skipped or ignored specifically on the index route in production. Still no idea why.

ivory rose
#

can you please create a minimal complete example repo and create a github issue for this?

red fjord
#

Sure!

red fjord
#

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?

ivory rose
#

if you prerender, the loader would not run when serving the prerendered route. yes