Skip to content
Source
Sign inSign up
TanStack/router

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

Opensleitor(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/7114/headopened

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.
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-router

    2. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      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 5a81726f0a2f819fae0763896cef784aa10ddc9f and ee99b339b7f66fdf1703a2ab30a008396776a3c5.

      📒 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 nameStatusExplanation
      Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title check✅ PassedThe 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✅ PassedThe 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✅ PassedAll 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✅ PassedNo 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.
    4. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit ee99b339b7f66fdf1703a2ab30a008396776a3c5

      CommandStatusDurationResult
      nx affected --targets=test:eslint,test:unit,tes...⛔ Cancelled1h 8m 59sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded1m 34sView ↗

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

    5. Bundle Size Benchmarks

      • Commit: 5a81726f0a2f
      • Measured at: 2026-04-07T02:54:32.365Z
      • Baseline source: history:796406da66cf
      • Dashboard: bundle-size history
      ScenarioCurrent (gzip)Delta vs baselineRawBrotliTrend
      react-router.minimal87.48 KiB0 B (0.00%)275.76 KiB76.05 KiB████▁▁▁▁▁▂▃
      react-router.full90.78 KiB+4 B (+0.00%)286.94 KiB78.80 KiB▆▆▆█▁▁▁▁▁▂▂▃
      solid-router.minimal35.56 KiB-2 B (-0.01%)107.25 KiB31.90 KiB████▁▁▁▁▁▂█▇
      solid-router.full40.03 KiB-3 B (-0.01%)120.79 KiB35.93 KiB████▁▁▁▁▁▂▆▆
      vue-router.minimal53.38 KiB-2 B (-0.00%)153.07 KiB47.96 KiB████▁▁▁▁▁▂▄▃
      vue-router.full58.25 KiB+4 B (+0.01%)168.53 KiB52.15 KiB████▁▁▁▁▁▂▄▄
      react-start.minimal102.01 KiB-2 B (-0.00%)324.00 KiB88.16 KiB▅▅▅█▁▂▂▂▂▃▃▃
      react-start.full105.38 KiB0 B (0.00%)334.35 KiB90.94 KiB▆▆▆█▁▁▁▂▂▃▃
      solid-start.minimal49.66 KiB-3 B (-0.01%)153.50 KiB43.71 KiB▇▇▇▇▁▁▁▂▂▂██
      solid-start.full55.17 KiB0 B (0.00%)169.74 KiB48.57 KiB▇▇▇▇▁▂▂▂▂▃█

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

    6. 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

    7. Merging this PR will not alter performance

      ✅ 6 untouched benchmarks


      Comparing sleitor:fix-7110 (ee99b33) with main (796406d)[^unexpected-base] Open in CodSpeed

Sign in to join this conversation.