fix(router-plugin): split object-form loaders #8755
main fromrefs/pull/8755/head·opened 🎯 Changes#
Fixes #8753.
With loader in a split grouping, loader: { handler, staleReloadMode } made the loader chunk throw Unexpected splitNode type ☝️: ObjectExpression, and the route file replaced the whole object with lazyFn(...). Now only handler is split:
loader: {
staleReloadMode: 'blocking',
handler: lazyFn($$splitLoaderImporter, 'loader'),
},
getLoaderHandlerpicks the property to split: an arrow or function, ahandler() {}method (rewritten as a function expression), or an identifier, imported or local. The reference compiler'slazyFnbranch and the virtual compiler both use it.computeSharedBindingsputs the handler in the loader's group and the other options with the route file, so a binding both read goes to?tsr-shared=1.- As in #8504's
splitProperty, the loader stays in the route file when a spread or computed key afterhandlermay replace it, or when there is no plainhandler(missing, a getter, a fallback value). It also stays when a method handler usessuper. ARCHITECTURE-CODE-SPLITTING.mddescribes the rule.
Not covered: a loader object passed by reference (loader: loaderObject, the second object-form pin in #8630) is still split whole.
Tests:
- Snapshot fixtures
loader-object-arrow,loader-object-method(shares a constant withstaleReloadMode) andloader-object-imported. code-splitter-route-options.test.ts: for each handler form, builds an app with[['loader'], ['component']]and checks the route loads its data and keepsstaleReloadMode(like #8630's first object-form pin); checks the cases that stay in the route file, and the sharing between the handler and the other options. All 9 fail onmain.compileRouteModulesgained agroupingsoption for them.pnpm nx run @tanstack/router-plugin:test:unit(936 passed, 3 expected fail),test:typesandtest:eslintpass.
AI disclosure: Claude Code wrote this change, its tests and this description.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with the relevant test commands, or tests do not apply to this pull request.
- I fully understand the code in this pull request, including any code generated with AI assistance.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
🤖 Generated with Claude Code
Summary by CodeRabbit#
- New Features
- Object-form route loaders can now be code-split by handler while keeping other loader options, such as stale reload behavior, on the route.
- Bug Fixes
- Loaders remain intact when their handler cannot be safely separated, preventing split-chunk errors for unsupported object forms.
- 1
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Repository: TanStack/router/.coderabbit.yaml
- Review profile: CHILL
- Plan: Advanced
- Run ID:
689000e2-c50b-403f-b939-c09b166bad62
📥 Commits
-
Reviewing files that changed from the base of the PR and between b1b111761bdfa7a0582f28ea78ee81fe7c59ed99 and 4d25bdc6d5d88da39e9c09f9d9e95e0e08dcdfdd.
📒 Files selected for processing: 48
-
.changeset/split-object-form-loader.mdpackages/router-plugin/ARCHITECTURE-CODE-SPLITTING.mdpackages/router-plugin/src/core/code-splitter/compilers.tspackages/router-plugin/src/core/utils.tspackages/router-plugin/tests/code-splitter-route-options.test.tspackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-arrow.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-arrow@component.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-arrow@errorComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-arrow@notFoundComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-arrow@shared.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-imported.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-imported@component.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-imported@errorComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-imported@notFoundComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-imported@shared.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-method.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-method@component.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-method@errorComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-method@notFoundComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/1-default/loader-object-method@shared.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-arrow.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-arrow@component---errorComponent---notFoundComponent---pendingComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-arrow@loader.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-arrow@shared.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-imported.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-imported@component---errorComponent---notFoundComponent---pendingComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-imported@loader.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-imported@shared.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-method.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-method@component---errorComponent---notFoundComponent---pendingComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-method@loader.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/2-components-combined-loader-separate/loader-object-method@shared.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-arrow.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-arrow@component---loader---notFoundComponent---pendingComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-arrow@errorComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-arrow@shared.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-imported.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-imported@component---loader---notFoundComponent---pendingComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-imported@errorComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-imported@shared.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-method.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-method@component---loader---notFoundComponent---pendingComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-method@errorComponent.tsxpackages/router-plugin/tests/code-splitter/snapshots/react/3-all-combined-errorComponent-separate/loader-object-method@shared.tsxpackages/router-plugin/tests/code-splitter/test-files/react/loader-object-arrow.tsxpackages/router-plugin/tests/code-splitter/test-files/react/loader-object-imported.tsxpackages/router-plugin/tests/code-splitter/test-files/react/loader-object-method.tsxpackages/router-plugin/tests/regression-helpers.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
-
📝 Walkthrough
-
The router plugin now splits eligible
handlervalues from object-form loaders while retaining other loader options in the route module. It leaves loaders unsplit when the handler cannot be safely selected. Tests cover generated modules, runtime loader data, and shared bindings.Changes: Object-form loader splitting
Layer / File(s) Summary Select and partition loader handlers
packages/router-plugin/src/core/code-splitter/compilers.ts,packages/router-plugin/src/core/utils.tsThe compiler selects eligible object-form loader handlers and keeps the other loader properties in the route module. It leaves the loader unsplit when a later spread or computed key may override the handler, no plain handler is available, or a method uses super.Group references and emit split targets
packages/router-plugin/src/core/code-splitter/compilers.tsGrouping and shared-binding analysis process loader handlers and remaining options separately. Reference-route and virtual-route compilation use the selected handler as the split target. Validate generated loader splits
packages/router-plugin/tests/code-splitter-route-options.test.ts,packages/router-plugin/tests/code-splitter/test-files/react/*,packages/router-plugin/tests/code-splitter/snapshots/react/*,packages/router-plugin/tests/regression-helpers.ts,packages/router-plugin/ARCHITECTURE-CODE-SPLITTING.md,.changeset/split-object-form-loader.mdTests and snapshots cover arrow, method, and imported handlers; unsafe handler cases; shared bindings; and runtime loader data. The regression helper accepts custom code-split groupings. Documentation and the changeset describe the object-form loader behavior. Priority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix · Severity of issue fixed: Medium
Possibly related PRs
- TanStack/router#8459: Adds
ObjectMethodsupport used when the object-form loader handler is written as a method.
- TanStack/router#8459: Adds
Merge Risk: ⚪ Minimal · up to
4d25b-
No actionable issue remains from this review; the change is ready to merge after normal checks.
Pre-merge checks:
4 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Docstring Coverage Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 30 files. (2 skipped:… Write docstrings for the functions missing them to satisfy the coverage threshold. Passed checks: 4 passed-
Check name Status Explanation Title check The title clearly and concisely describes the main change: fixing code splitting for object-form loaders. Description check The description follows the required template, explains the change and scope, documents testing, includes AI disclosure, and provides the required changeset and checklist sections. The code-understand… Linked Issues check The implementation meets #8753.getLoaderHandlerselects arrow/function handlers, methods, and identifiers. Reference compilation replaces only the handler withlazyFn; virtual compilation tracks …Out of Scope Changes check The changes stay within #8753. The compiler changes implement object-form loader splitting. The new tests, snapshot fixtures, regression-helper grouping option, architecture documentation, utility typ…Full details: Description check
-
Explanation
The description follows the required template, explains the change and scope, documents testing, includes AI disclosure, and provides the required changeset and checklist sections. The code-understanding checklist item remains unchecked, but the description is otherwise complete.
Full details: Linked Issues check
-
Explanation
The implementation meets
#8753.getLoaderHandlerselects arrow/function handlers, methods, and identifiers. Reference compilation replaces only the handler withlazyFn; virtual compilation tracks only the handler.computeSharedBindingsassigns the handler to the split group and the remaining loader options to the route file. The compiler keeps unsafe object loaders whole for later spreads or computed keys, missing or non-plain handlers, getters, and methods that usesuper. Tests cover the supported forms, option preservation, fallback cases, and shared bindings. The architecture documentation describes the rule.
Full details: Out of Scope Changes check
-
Explanation
The changes stay within
#8753. The compiler changes implement object-form loader splitting. The new tests, snapshot fixtures, regression-helper grouping option, architecture documentation, utility type update, and changeset support or document this behavior. No unrelated product behavior is introduced. Loader objects passed by reference remain unchanged, as required by the issue scope.
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 30 files. (2 skipped: 2 unsupported.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
- Fix all pre-merge checks with AI
✨ Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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 `@coderabbitai help` to get the list of available commands.-
shkumbinhasani(GitHub profile, opens in a new tab) linked this to fix #8753
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 4d25bdc6d5d88da39e9c09f9d9e95e0e08dcdfdd
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 7 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR