Skip to content
Source
Sign inSign up
TanStack/router

fix: only run middleware compiler in client env #5703

Mergedschiller-manuel(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/5703/headmerged 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

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

      Cohort / File(s)Change Summary
      Compiler middleware conditional logic
      packages/start-plugin-core/src/start-compiler-plugin/compilers.ts
      Switched from a concrete Identifiers object to Partial<Identifiers>; initialize createMiddleware and its identifier only when env === 'client'; added guards to skip undefined identifier entries; use local identifier vars instead of direct map access; update name/binding checks and push call paths to identifier.paths; iterate identifiers with null checks and call identifier.handleCallExpression in final transform.

      Sequence Diagram(s)

      sequenceDiagram
          participant Compiler
          participant Plugin
          participant Identifiers
      
          Compiler->>Plugin: compileStartOutputFactory(partialIdentifiers, env)
          alt env == 'client'
              Plugin->>Identifiers: createMiddleware()
              Identifiers-->>Plugin: add middleware identifier (identifier.paths)
          else env != 'client'
              Note over Plugin: middleware identifier not created
          end
      
          Plugin->>Plugin: import resolution (guard undefined identifiers)
          Plugin->>Plugin: collect call-expression paths -> push to identifier.paths
          Plugin->>Plugin: namespace/member resolution -> compare identifier.name, push paths
          Plugin->>Plugin: final transform -> for each identifier (skip undefined) call handleCallExpression
          Plugin-->>Compiler: transformed AST

      Estimated code review effort

      🎯 3 (Moderate) | ⏱️ ~20–30 minutes

      • Attention areas:
        • Verify all null/undefined guards are correctly placed and exhaustive
        • Confirm middleware initialization is strictly client-only and doesn't leak into server builds
        • Ensure all uses of identifier.name, identifier.paths, and identifier.handleCallExpression follow the new local-variable pattern
      • TanStack/router#5703 — Makes the same Partial<Identifiers> change, moves middleware init to client-only, and applies similar guards/local identifier handling in compilers.ts.

      Poem

      🐰 I hopped through ASTs with care,

      client-side only, light as air, guards in place, no server fright, paths collected, names set right. A little hop — the plugin's bright! 🥕

      Pre-merge checks and finishing touches

      ❌ Failed checks (1 warning)
      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.You can run @coderabbitai generate docstrings to improve docstring coverage.
      ✅ Passed checks (2 passed)
      Check nameStatusExplanation
      Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title Check✅ PassedThe pull request title "fix: only run middleware compiler in client env" directly aligns with the primary changes described in the raw summary. The summary confirms that the main modification is adding conditional inclusion of createMiddleware only when env is 'client', along with supporting infrastructure changes like partial identifiers and null/undefined checks. The title is concise, specific, and clearly conveys the key fix without vague terms or unnecessary details.

      📜 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.
    3. View your CI Pipeline Execution ↗ for commit c7cfa35bbdc2adbb96269def1e66da50a6fef56a

      CommandStatusDurationResult
      nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded5m 40sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded20sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-10-30 21:52:38 UTC

    4. More templates

      @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@5703

      commit: 0432e12

    5. Actionable 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 return statement here returns the new array length from push(), 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 of Partial<Identifiers> for conditional inclusion.

      The type change to Partial<Identifiers> correctly supports the conditional inclusion of createMiddleware, 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 createMiddleware is 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.paths and identifier.handleCallExpression only after confirming the identifier exists, maintaining type safety throughout.

      Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)

Sign in to join this conversation.