fix(router-core): treat null return from beforeLoad as no-op for route context#7114
fix(router-core): treat null return from beforeLoad as no-op for route context#7114sleitor wants to merge 2 commits intoTanStack:mainfrom
Conversation
…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
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThis pull request fixes route context handling in TanStack Router by treating Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
View your CI Pipeline Execution ↗ for commit ee99b33
☁️ Nx Cloud last updated this comment at |
Bundle Size Benchmarks
Trend sparkline is historical gzip bytes ending with this PR measurement; lower is better. |
Summary
When
beforeLoadreturnsnull, it is now treated the same asundefined(no-op) instead of being stored as__beforeLoadContext: null.Problem
When a route's
beforeLoadfunction returnsnull(e.g. from a server function),useRouteContext()returns an empty object{}instead of preserving the parent context. This happens because:nullis stored as__beforeLoadContext: nullObject.assign(context, ..., null)or{...parentContext, ...null}, null is silently ignoredMeanwhile, the
contextroute option already handles this correctly withroute.options.context(contextFnContext) ?? undefined.Fix
In
updateContextwithinload-matches.ts, treatnullthe same asundefined— skip storing it as__beforeLoadContext. This makesbeforeLoadconsistent with thecontextoption 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 whenbeforeLoadreturnsnull.Closes #7110
Summary by CodeRabbit
beforeLoadhook returnsnull. Null values are now treated as no-op, aligning with standard context behavior.