Skip to content

fix(router-core): treat null return from beforeLoad as no-op for route context#7114

Open
sleitor wants to merge 2 commits intoTanStack:mainfrom
sleitor:fix-7110
Open

fix(router-core): treat null return from beforeLoad as no-op for route context#7114
sleitor wants to merge 2 commits intoTanStack:mainfrom
sleitor:fix-7110

Conversation

@sleitor
Copy link
Copy Markdown
Contributor

@sleitor sleitor commented Apr 7, 2026

Summary

When beforeLoad returns null, it is now treated the same as undefined (no-op) instead of being stored as __beforeLoadContext: null.

Problem

When a route's beforeLoad function returns null (e.g. from a server function), useRouteContext() returns an empty object {} instead of preserving the parent context. This happens because:

  1. null is stored as __beforeLoadContext: null
  2. When building context via Object.assign(context, ..., null) or {...parentContext, ...null}, null is silently ignored
  3. The resulting context loses any parent-contributed values

Meanwhile, the context route option already handles this correctly with route.options.context(contextFnContext) ?? undefined.

Fix

In updateContext within load-matches.ts, treat null the same as undefined — skip storing it as __beforeLoadContext. This makes beforeLoad consistent with the context option behavior.

Tests

Added two tests to routeContext.test.tsx:

  • route context preserved when beforeLoad returns null (sync)
  • route context preserved when async beforeLoad returns null (async)

Both verify that parent context ({ foo: 'bar' }) is preserved when beforeLoad returns null.

Closes #7110

Summary by CodeRabbit

  • Bug Fixes
    • Fixed route context handling when the beforeLoad hook returns null. Null values are now treated as no-op, aligning with standard context behavior.

…e 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 TanStack#7110
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 57bcc6e5-684a-4123-b7ae-cb489b78c048

📥 Commits

Reviewing files that changed from the base of the PR and between 5a81726 and ee99b33.

📒 Files selected for processing (3)
  • .changeset/fix-beforeload-null-context.md
  • packages/react-router/tests/routeContext.test.tsx
  • packages/router-core/src/load-matches.ts

📝 Walkthrough

Walkthrough

This pull request fixes route context handling in TanStack Router by treating null returns from beforeLoad as no-ops, preventing unintended null values from being stored in the route context. The fix aligns beforeLoad behavior with the existing context route option.

Changes

Cohort / File(s) Summary
Changesets Documentation
.changeset/fix-beforeload-null-context.md
Added Changesets entry documenting the patch fix that treats null returns from beforeLoad as undefined (no-op), preventing null from being stored in route context.
Core Logic
packages/router-core/src/load-matches.ts
Modified executeBeforeLoad's updateContext to treat null the same as undefined, causing early return without updating __beforeLoadContext when beforeLoadContext is null or undefined.
Test Coverage
packages/react-router/tests/routeContext.test.tsx
Added two new tests verifying that route context remains unchanged when root route's beforeLoad returns null (both sync and async cases).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A null return once caused quite the fright,
Making context objects when there should be none in sight!
But now we've fixed this tricky case,
Treating null like undefined—keeping context clean and bright! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: treating null return from beforeLoad as a no-op for route context, which directly addresses the problem in issue #7110.
Linked Issues check ✅ Passed The PR fully addresses issue #7110 by fixing beforeLoad null handling, adding tests to verify context preservation, and making behavior consistent with loader/context handling.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the null handling in beforeLoad: the changesets entry, test cases for context preservation, and the load-matches.ts modification.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link
Copy Markdown
Contributor

nx-cloud bot commented Apr 7, 2026

View your CI Pipeline Execution ↗ for commit ee99b33

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ⛔ Cancelled 1h 8m 59s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1m 34s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-07 04:03:42 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

Bundle Size Benchmarks

  • Commit: 5a81726f0a2f
  • Measured at: 2026-04-07T02:54:32.365Z
  • Baseline source: history:796406da66cf
  • Dashboard: bundle-size history
Scenario Current (gzip) Delta vs baseline Raw Brotli Trend
react-router.minimal 87.48 KiB 0 B (0.00%) 275.76 KiB 76.05 KiB ████▁▁▁▁▁▂▃
react-router.full 90.78 KiB +4 B (+0.00%) 286.94 KiB 78.80 KiB ▆▆▆█▁▁▁▁▁▂▂▃
solid-router.minimal 35.56 KiB -2 B (-0.01%) 107.25 KiB 31.90 KiB ████▁▁▁▁▁▂█▇
solid-router.full 40.03 KiB -3 B (-0.01%) 120.79 KiB 35.93 KiB ████▁▁▁▁▁▂▆▆
vue-router.minimal 53.38 KiB -2 B (-0.00%) 153.07 KiB 47.96 KiB ████▁▁▁▁▁▂▄▃
vue-router.full 58.25 KiB +4 B (+0.01%) 168.53 KiB 52.15 KiB ████▁▁▁▁▁▂▄▄
react-start.minimal 102.01 KiB -2 B (-0.00%) 324.00 KiB 88.16 KiB ▅▅▅█▁▂▂▂▂▃▃▃
react-start.full 105.38 KiB 0 B (0.00%) 334.35 KiB 90.94 KiB ▆▆▆█▁▁▁▂▂▃▃
solid-start.minimal 49.66 KiB -3 B (-0.01%) 153.50 KiB 43.71 KiB ▇▇▇▇▁▁▁▂▂▂██
solid-start.full 55.17 KiB 0 B (0.00%) 169.74 KiB 48.57 KiB ▇▇▇▇▁▂▂▂▂▃█

Trend sparkline is historical gzip bytes ending with this PR measurement; lower is better.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 7, 2026

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@7114

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@7114

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@7114

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/@tanstack/nitro-v2-vite-plugin@7114

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@7114

@tanstack/react-router-devtools

npm i https://pkg.pr.new/@tanstack/react-router-devtools@7114

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/@tanstack/react-router-ssr-query@7114

@tanstack/react-start

npm i https://pkg.pr.new/@tanstack/react-start@7114

@tanstack/react-start-client

npm i https://pkg.pr.new/@tanstack/react-start-client@7114

@tanstack/react-start-server

npm i https://pkg.pr.new/@tanstack/react-start-server@7114

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@7114

@tanstack/router-core

npm i https://pkg.pr.new/@tanstack/router-core@7114

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@7114

@tanstack/router-devtools-core

npm i https://pkg.pr.new/@tanstack/router-devtools-core@7114

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@7114

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@7114

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/@tanstack/router-ssr-query-core@7114

@tanstack/router-utils

npm i https://pkg.pr.new/@tanstack/router-utils@7114

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@7114

@tanstack/solid-router

npm i https://pkg.pr.new/@tanstack/solid-router@7114

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/@tanstack/solid-router-devtools@7114

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/@tanstack/solid-router-ssr-query@7114

@tanstack/solid-start

npm i https://pkg.pr.new/@tanstack/solid-start@7114

@tanstack/solid-start-client

npm i https://pkg.pr.new/@tanstack/solid-start-client@7114

@tanstack/solid-start-server

npm i https://pkg.pr.new/@tanstack/solid-start-server@7114

@tanstack/start-client-core

npm i https://pkg.pr.new/@tanstack/start-client-core@7114

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/@tanstack/start-fn-stubs@7114

@tanstack/start-plugin-core

npm i https://pkg.pr.new/@tanstack/start-plugin-core@7114

@tanstack/start-server-core

npm i https://pkg.pr.new/@tanstack/start-server-core@7114

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/@tanstack/start-static-server-functions@7114

@tanstack/start-storage-context

npm i https://pkg.pr.new/@tanstack/start-storage-context@7114

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@7114

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@7114

@tanstack/vue-router

npm i https://pkg.pr.new/@tanstack/vue-router@7114

@tanstack/vue-router-devtools

npm i https://pkg.pr.new/@tanstack/vue-router-devtools@7114

@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/@tanstack/vue-router-ssr-query@7114

@tanstack/vue-start

npm i https://pkg.pr.new/@tanstack/vue-start@7114

@tanstack/vue-start-client

npm i https://pkg.pr.new/@tanstack/vue-start-client@7114

@tanstack/vue-start-server

npm i https://pkg.pr.new/@tanstack/vue-start-server@7114

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@7114

commit: ee99b33

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 7, 2026

Merging this PR will not alter performance

✅ 6 untouched benchmarks


Comparing sleitor:fix-7110 (ee99b33) with main (796406d)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (5a81726) during the generation of this report, so 796406d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Route.useRouteContext() returns an empty object instead of null

1 participant