Repository: TanStack/router (ts)

Issue:
Route context not updated if new context is returned in beforeLoad
### Which project does this relate to?

Router

### Describe the bug

while updating from 1.45.4 to 1.91.3 i noticed a new behavior related to router context.
if a new context value is returned from beforeLoad it remains cached and new values for that same context key are not received.

### Your Example Website or App

https://stackblitz.com/edit/tanstack-router-hsasr5ei?file=src%2Fmain.tsx

### Steps to Reproduce the Bug or Issue

1. click "refresh" button ONCE
2. auth.token and _auth.token have the same value 
3. click "refresh" button again
4. auth.token and _auth.token have different values

5. remove the return statement from the indexRoute's beforeLoad
6. refresh the app and do 1. 2. & 3.
7. the behavior is different. auth.token and _auth.token have the same value at all times

### Expected behavior

each call to beforeLoad should receive the latest context even if some of the keys ware overridden by beforeLoad result in the previous call. this used to be the case in 1.45.4

### Screenshots or Videos

https://github.com/user-attachments/assets/b57854e5-2b6f-4b54-92dd-edf2971624cc

### Platform

- OS: mac
- Browser: firefox
- Version: 130


### Additional context

_No response_

Pull request:
fix(react-router): ensure updated context in `beforeLoad` when returning same properties
fixes #3051

Patch:
diff --git a/packages/react-router/src/router.ts b/packages/react-router/src/router.ts
index 1a0cf5726..a28ca4afe 100644
--- a/packages/react-router/src/router.ts
+++ b/packages/react-router/src/router.ts
@@ -2397,7 +2397,6 @@ export class Router<
                     context: {
                       ...getParentMatchContext(),
                       ...prev.__routeContext,
-                      ...prev.__beforeLoadContext,
                     },
                   }))
 


Test patch:
diff --git a/packages/react-router/tests/routeContext.test.tsx b/packages/react-router/tests/routeContext.test.tsx
index 5cb95a809..c62decee7 100644
--- a/packages/react-router/tests/routeContext.test.tsx
+++ b/packages/react-router/tests/routeContext.test.tsx
@@ -1407,6 +1407,59 @@ describe('beforeLoad in the route definition', () => {
     expect(mock).toHaveBeenCalledWith({ foo: 'bar', layout: 'layout' })
     expect(mock).toHaveBeenCalledTimes(1)
   })
+
+  test('child route receives updated values after the values were previously returned by the child route and then updated by the parent route', async () => {
+    const mockIndexBeforeLoadFn = vi.fn()
+    let counter = 0
+
+    const rootRoute = createRootRoute({
+      beforeLoad: () => {
+        counter++
+        return {
+          counter,
+        }
+      },
+    })
+    const indexRoute = createRoute({
+      getParentRoute: () => rootRoute,
+      path: '/',
+      beforeLoad: ({ context }) => {
+        mockIndexBeforeLoadFn(context)
+        return {
+          counter: context.counter,
+        }
+      },
+      component: () => {
+        const { counter } = indexRoute.useRouteContext()
+        return (
+          <div>
+            <span data-testid="index-page">Index page</span>
+            <span data-testid="counter">{counter}</span>
+          </div>
+        )
+      },
+    })
+    const routeTree = rootRoute.addChildren([indexRoute])
+    const router = createRouter({ routeTree, history })
+
+    render(<RouterProvider router={router} />)
+
+    const rootElement = await screen.findByTestId('index-page')
+    expect(rootElement).toBeInTheDocument()
+
+    async function check(expectedCounter: number) {
+      const counterElement = await screen.findByTestId('counter')
+      expect(counterElement).toHaveTextContent(`${expectedCounter}`)
+
+      expect(mockIndexBeforeLoadFn).toHaveBeenCalledWith({
+        counter: expectedCounter,
+      })
+    }
+
+    await check(1)
+    await router.invalidate()
+    await check(2)
+  })
 })
 
 describe('loader in the route definition', () => {