Skip to content

Empty context with parent / child beforeLoad, loader and validateSearch #3578

Closed
@cyrilchapon

Description

@cyrilchapon

Which project does this relate to?

Router

Describe the bug

Hello,

I'm facing a problem where I get an empty context passed down to a child route uppon validateSearch URL rewrite.

The setup looks like this

  • __root.tsx
  • _parent.tsx
    • beforeLoad (adds { session } to context)
    • loader (reads context.session)
  • _parent/child.tsx
    • validateSearch
    • beforeLoad (reads context.session)
    • loader (reads context.session)

_parent.tsx

export const Route = createFileRoute('/_some-parent')({
  component: () => <Outlet />,
  beforeLoad: async () => {
    // Fake session fetch
    await new Promise((resolve) => setTimeout(resolve, 1000));
    const session = 'fake-session';

    console.log('parent beforeLoad', session);

    return { session };
  },
  loader: async ({ context: { session } }) => {
    console.log('parent loader', session);
  },
});

_parent/child.tsx

const applicationSearchOptionsSchema = z.object({
  page: fallback(z.number().positive(), 1).default(1),
});

export const Route = createFileRoute('/_some-parent/some-child')({
  component: () => <div>Hello world</div>,
  validateSearch: zodValidator(applicationSearchOptionsSchema),
  beforeLoad: ({ context: { session } }) => {
    console.log('child beforeload', session);
  },
  loader: ({ context: { session } }) => {
    console.log('child loader', session);
  },
})

(NB: I'm using zodValidator and fallback syntax straight from the documentation)

When navigating to /child?page=1, everything behaves correctly :

> parent beforeLoad fake-session
> child beforeload fake-session
> parent loader fake-session
> child loader fake-session

But when hard navigating (not client side redirect) to /child, I'm getting an empty context with undefined session on loaders first; then after the "redirection" (it's not a redirect but you get my point) to ?page=1, it loads correctly :

> parent beforeLoad fake-session
> child beforeload fake-session
> parent loader undefined // <= HERE
> child loader undefined // <= HERE

> parent beforeLoad fake-session
> child beforeload fake-session
> parent loader fake-session
> child loader fake-session

What is even stranger; is on local I'm even also getting and undefined on the child beforeLoad and not on Stackblitz :

> parent beforeLoad fake-session
> child beforeload undefined // <= HERE
> parent loader undefined // <= HERE
> child loader undefined // <= HERE

> parent beforeLoad fake-session
> child beforeload fake-session
> parent loader fake-session
> child loader fake-session

Your Example Website or App

https://stackblitz.com/edit/github-advydsjg?file=src%2Froutes%2F_some-parent.tsx,src%2Froutes%2F_some-parent%2Fsome-child.tsx

Steps to Reproduce the Bug or Issue

  • go reproduction
  • open console inspector
  • on the preview, click on "Child"
  • if error doesn't pop, go back to home
  • copy the url, append /some-child to it then enter

Expected behavior

I would expect either the loader not to run, or the loader getting a correct context

Screenshots or Videos

0741bfe144cf4aeb97f3156146a7b501.mp4

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 132.0.6834.111 ARM64

Additional context

    "@tanstack/react-router": "^1.105.5",
    "@tanstack/zod-adapter": "^1.111.11",
    "zod": "^3.24.2",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions