From b916a5560ea32b305f2e7d924d8733eaf92ecfac Mon Sep 17 00:00:00 2001 From: sleitor Date: Tue, 7 Apr 2026 02:50:21 +0000 Subject: [PATCH 1/2] fix(router-core): treat null return from beforeLoad as no-op for route context When beforeLoad returns null, treat it the same as undefined (no-op) instead of storing it as __beforeLoadContext: null. This is consistent with how the context route option already handles null returns via ?? undefined, and prevents null from silently interfering with context accumulation. Closes #7110 --- .changeset/fix-beforeload-null-context.md | 9 +++++ .../react-router/tests/routeContext.test.tsx | 37 +++++++++++++++++++ packages/router-core/src/load-matches.ts | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 .changeset/fix-beforeload-null-context.md diff --git a/.changeset/fix-beforeload-null-context.md b/.changeset/fix-beforeload-null-context.md new file mode 100644 index 00000000000..5399fc50176 --- /dev/null +++ b/.changeset/fix-beforeload-null-context.md @@ -0,0 +1,9 @@ +--- +'@tanstack/router-core': patch +--- + +fix(router-core): treat null return from beforeLoad as no-op for route context + +When `beforeLoad` returns `null`, it is now treated the same as `undefined` (no-op) instead of being stored as `__beforeLoadContext: null`. This is consistent with how the `context` route option already handles null returns via `?? undefined`, and prevents null from silently interfering with context accumulation. + +Fixes #7110 diff --git a/packages/react-router/tests/routeContext.test.tsx b/packages/react-router/tests/routeContext.test.tsx index d11f86420e6..d5e7b8d9e84 100644 --- a/packages/react-router/tests/routeContext.test.tsx +++ b/packages/react-router/tests/routeContext.test.tsx @@ -2630,6 +2630,43 @@ describe('useRouteContext in the component', () => { expect(content).toBeInTheDocument() }) + + test('route context preserved when beforeLoad returns null', async () => { + const rootRoute = createRootRoute({ + beforeLoad: () => null, + component: () => { + const context = rootRoute.useRouteContext() + return
{JSON.stringify(context)}
+ }, + }) + const routeTree = rootRoute.addChildren([]) + const router = createRouter({ routeTree, history, context: { foo: 'bar' } }) + + render() + + const content = await screen.findByText(JSON.stringify({ foo: 'bar' })) + + expect(content).toBeInTheDocument() + }) + + test('route context preserved when async beforeLoad returns null', async () => { + const rootRoute = createRootRoute({ + beforeLoad: async () => null, + component: () => { + const context = rootRoute.useRouteContext() + return
{JSON.stringify(context)}
+ }, + }) + const routeTree = rootRoute.addChildren([]) + const router = createRouter({ routeTree, history, context: { foo: 'bar' } }) + + render() + + const content = await screen.findByText(JSON.stringify({ foo: 'bar' })) + + expect(content).toBeInTheDocument() + }) + test('route context (sleep in beforeLoad), present in the root route', async () => { const rootRoute = createRootRoute({ beforeLoad: async () => { diff --git a/packages/router-core/src/load-matches.ts b/packages/router-core/src/load-matches.ts index 3e99c828936..a2b6d798cf6 100644 --- a/packages/router-core/src/load-matches.ts +++ b/packages/router-core/src/load-matches.ts @@ -490,7 +490,7 @@ const executeBeforeLoad = ( } const updateContext = (beforeLoadContext: any) => { - if (beforeLoadContext === undefined) { + if (beforeLoadContext === undefined || beforeLoadContext === null) { inner.router.batch(() => { pending() resolve() From ee99b339b7f66fdf1703a2ab30a008396776a3c5 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 02:52:09 +0000 Subject: [PATCH 2/2] ci: apply automated fixes --- packages/react-router/tests/routeContext.test.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/react-router/tests/routeContext.test.tsx b/packages/react-router/tests/routeContext.test.tsx index d5e7b8d9e84..daaa0656494 100644 --- a/packages/react-router/tests/routeContext.test.tsx +++ b/packages/react-router/tests/routeContext.test.tsx @@ -2630,7 +2630,6 @@ describe('useRouteContext in the component', () => { expect(content).toBeInTheDocument() }) - test('route context preserved when beforeLoad returns null', async () => { const rootRoute = createRootRoute({ beforeLoad: () => null,