fix: only run middleware compiler in client env #5703
main fromrefs/pull/5703/head·merged by schiller-manuel(GitHub profile, opens in a new tab) Summary by CodeRabbit#
- Refactor
- Improved compilation robustness by making middleware handling optional and client-only, adding guards to skip undefined entries, and consolidating identifier processing to avoid runtime errors and prevent middleware-related code from running on server builds.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: start-plugin-core
View your CI Pipeline Execution ↗ for commit c7cfa35bbdc2adbb96269def1e66da50a6fef56a
Command Status Duration Result nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded 5m 40s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 20s View ↗
☁️ Nx Cloud last updated this comment at
2025-10-30 21:52:38UTCMore templates
- tanstack-router-solid-example-basic
- tanstack-router-solid-example-basic-devtools-panel
- tanstack-router-solid-example-basic-file-based
- tanstack-router-solid-example-basic-non-nested-devtools
- tanstack-router-solid-example-basic-solid-query
- tanstack-router-solid-example-basic-solid-query-file-based
- tanstack-router-solid-example-basic-ssr-streaming-file-based
- tanstack-router-solid-example-kitchen-sink-file-based
- tanstack-router-solid-example-kitchen-sink-solid-query
- tanstack-router-solid-example-kitchen-sink-solid-query-file-based
- tanstack-router-solid-example-quickstart-file-based
- tanstack-solid-start-example-bare
- tanstack-solid-start-example-basic
- tanstack-solid-start-example-basic-auth
- tanstack-solid-start-example-basic-cloudflare
- tanstack-solid-start-example-basic-netlify
- tanstack-solid-start-example-basic-nitro
- tanstack-start-example-basic-solid-query
- tanstack-solid-start-example-basic-static
- tanstack-router-react-example-authenticated-routes
- tanstack-router-react-example-authenticated-routes-firebase
- tanstack-router-react-example-basic
- tanstack-router-react-example-basic-default-search-params
- tanstack-router-react-example-basic-devtools-panel
- tanstack-router-react-example-basic-file-based
- tanstack-router-react-example-basic-non-nested-devtools
- tanstack-router-react-example-react-query
- tanstack-router-react-example-basic-react-query-file-based
- tanstack-router-react-example-basic-ssr-file-based
- tanstack-router-react-example-basic-ssr-streaming-file-based
- tanstack-router-react-example-basic-virtual-file-based
- tanstack-router-react-example-basic-virtual-inside-file-based
- tanstack-router-react-example-deferred-data
- tanstack-router-i18n-paraglide
- tanstack-router-react-example-kitchen-sink
- tanstack-router-react-example-kitchen-sink-file-based
- tanstack-router-react-example-kitchen-sink-react-query
- tanstack-router-react-example-kitchen-sink-react-query-file-based
- tanstack-router-react-example-large-file-based
- tanstack-router-react-example-location-masking
- tanstack-router-react-example-navigation-blocking
- tanstack-router-react-example-quickstart
- tanstack-router-react-example-quickstart-esbuild-file-based
- tanstack-router-react-example-quickstart-file-based
- tanstack-router-react-example-quickstart-rspack-file-based
- tanstack-router-react-example-quickstart-webpack-file-based
- router-monorepo-react-query
- router-mono-simple
- router-mono-simple-lazy
- tanstack-router-react-example-scroll-restoration
- tanstack-search-validator-adapters
- tanstack-start-example-bare
- tanstack-start-example-basic
- tanstack-start-example-basic-auth
- tanstack-start-example-basic-cloudflare
- tanstack-start-example-basic-react-query
- tanstack-start-example-basic-static
- tanstack-start-bun-hosting
- tanstack-start-example-clerk-basic
- tanstack-start-example-convex-trellaux
- tanstack-start-example-counter
- tanstack-start-i18n-paraglide
- tanstack-start-example-large
- tanstack-start-example-material-ui
- tanstack-start-streaming-data-from-server-functions
- tanstack-start-example-supabase-basic
- tanstack-start-tailwind-v4
- tanstack-start-example-trellaux
- tanstack-start-example-workos
- tanstack-router-react-example-view-transitions
- tanstack-router-react-example-with-framer-motion
- tanstack-router-react-example-with-trpc
- tanstack-router-react-example-with-trpc-react-query
@tanstack/arktype-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@5703@tanstack/directive-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@5703@tanstack/eslint-plugin-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@5703@tanstack/history
npm i https://pkg.pr.new/TanStack/router/@tanstack/history@5703@tanstack/nitro-v2-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@5703@tanstack/react-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@5703@tanstack/react-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@5703@tanstack/react-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@5703@tanstack/react-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@5703@tanstack/react-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@5703@tanstack/react-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@5703@tanstack/router-cli
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@5703@tanstack/router-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@5703@tanstack/router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@5703@tanstack/router-devtools-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@5703@tanstack/router-generator
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@5703@tanstack/router-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@5703@tanstack/router-ssr-query-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@5703@tanstack/router-utils
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@5703@tanstack/router-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@5703@tanstack/server-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@5703@tanstack/solid-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@5703@tanstack/solid-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@5703@tanstack/solid-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@5703@tanstack/solid-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@5703@tanstack/solid-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@5703@tanstack/solid-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@5703@tanstack/start-client-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@5703@tanstack/start-plugin-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@5703@tanstack/start-server-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@5703@tanstack/start-static-server-functions
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@5703@tanstack/start-storage-context
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@5703@tanstack/valibot-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@5703@tanstack/virtual-file-routes
npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@5703@tanstack/zod-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@5703commit:
0432e12Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/start-plugin-core/src/start-compiler-plugin/compilers.ts (1)
116-116: Consider simplifying the return statement.The
returnstatement here returns the new array length frompush(), which is unused. This works correctly but is slightly confusing.Apply this diff for clarity:
- return identifier.paths.push(path) + identifier.paths.push(path) + return📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 6a62499c5e88aa913fd094e105c9b7f533f87246 and 0432e12b034f82052e5b926e6107299e134d27f6.
📒 Files selected for processing (1)
packages/start-plugin-core/src/start-compiler-plugin/compilers.ts(6 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Use TypeScript in strict mode with extensive type safety across the codebase
Files:
packages/start-plugin-core/src/start-compiler-plugin/compilers.ts
packages/{*-start,start-*}/**
📄 CodeRabbit inference engine (AGENTS.md)
Name and place Start framework packages under packages/-start/ or packages/start-/
Files:
packages/start-plugin-core/src/start-compiler-plugin/compilers.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
🔇 Additional comments (3)
packages/start-plugin-core/src/start-compiler-plugin/compilers.ts (3)
26-42: LGTM! Correct use ofPartial<Identifiers>for conditional inclusion.The type change to
Partial<Identifiers>correctly supports the conditional inclusion ofcreateMiddleware, ensuring type safety when not all identifiers are present.
73-75: LGTM! Proper guards for optional identifiers.The null/undefined checks at all three locations (import resolution, call expression handling, and transformation phase) correctly guard against missing identifiers when
createMiddlewareis excluded in server builds.Also applies to: 95-97, 144-146
147-152: LGTM! Safe transformation with proper guards.The transformation phase correctly accesses
identifier.pathsandidentifier.handleCallExpressiononly after confirming the identifier exists, maintaining type safety throughout.@@ -39,4 +39,8 @@3939 handleCallExpression: handleCreateIsomorphicFnCallExpression,4040 paths: [],4141 },42- createMiddleware: {42+ }43+44+ // createMiddleware only performs modifications in the client environment45+ // so we can avoid executing this on the client46+ if (opts.env === 'client') {⚠️ Potential issue | 🟠 Major
Fix contradictory comment.
The comment states "avoid executing this on the client" but the condition
opts.env === 'client'means the code WILL execute in the client environment. The comment should say "avoid executing this on the server" or be reworded for clarity.Apply this diff to fix the comment:
- // createMiddleware only performs modifications in the client environment - // so we can avoid executing this on the client + // createMiddleware only performs modifications in the client environment + // so we can avoid including it in the server build if (opts.env === 'client') {📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// createMiddleware only performs modifications in the client environment // so we can avoid including it in the server build if (opts.env === 'client') {🤖 Prompt for AI Agents
In packages/start-plugin-core/src/start-compiler-plugin/compilers.ts around lines 44 to 46, the comment is contradictory: it says "avoid executing this on the client" while the if condition checks opts.env === 'client' (meaning it runs on the client). Update the comment to correctly describe the behavior — e.g., "createMiddleware only performs modifications in the client environment so we can avoid executing this on the server" or reword to state that the block runs only for the client — ensuring the comment matches the condition.✅ Addressed in commit c7cfa35
Sign in to join this conversation.
Caution
Review failed#
The pull request is closed.
Walkthrough#
Reworked the compiler plugin to accept a Partial, add client-only middleware initialization (env === 'client'), and introduce defensive guards and local identifier handling across import resolution, call-expression collection, namespace resolution, and final transformation phases.
Changes#
packages/start-plugin-core/src/start-compiler-plugin/compilers.tsIdentifiersobject toPartial<Identifiers>; initializecreateMiddlewareand its identifier only whenenv === 'client'; added guards to skip undefined identifier entries; use localidentifiervars instead of direct map access; update name/binding checks and push call paths toidentifier.paths; iterate identifiers with null checks and callidentifier.handleCallExpressionin final transform.Sequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20–30 minutes
Possibly related PRs#
Partial<Identifiers>change, moves middleware init to client-only, and applies similar guards/local identifier handling incompilers.ts.Poem#
Pre-merge checks and finishing touches#
❌ Failed checks (1 warning)
@coderabbitai generate docstringsto improve docstring coverage.✅ Passed checks (2 passed)
📜 Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 0432e12b034f82052e5b926e6107299e134d27f6 and c7cfa35bbdc2adbb96269def1e66da50a6fef56a.
📒 Files selected for processing (1)
packages/start-plugin-core/src/start-compiler-plugin/compilers.ts(6 hunks)Comment `@coderabbitai help` to get the list of available commands and usage tips.