From 8c88574ae2c55955bfcdde1d1b69553ed97cb957 Mon Sep 17 00:00:00 2001 From: Manuel Schiller <6340397+schiller-manuel@users.noreply.github.com> Date: Sat, 8 Aug 2026 01:20:46 +0200 Subject: [PATCH] inline isServer for proper DCE --- packages/react-router/src/link.tsx | 7 +++---- packages/react-router/src/useRouterState.tsx | 5 +++-- packages/solid-router/src/useRouterState.tsx | 5 +++-- packages/vue-router/src/useRouterState.tsx | 6 +++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/react-router/src/link.tsx b/packages/react-router/src/link.tsx index 0ce9b25c94d..fc33e6b5947 100644 --- a/packages/react-router/src/link.tsx +++ b/packages/react-router/src/link.tsx @@ -166,9 +166,6 @@ export function useLinkProps< const router = useRouter() const innerRef = useForwardedRef(forwardedRef) - // Determine if we're on the server - used for tree-shaking client-only code - const _isServer = isServer ?? router.isServer - const { // custom props activeProps, @@ -220,7 +217,9 @@ export function useLinkProps< // // Note: `location.hash` is not available on the server. // ========================================================================== - if (_isServer) { + // The expression must stay inlined in the `if` so bundlers fold the + // browser-build constant `isServer = false` and drop this server block. + if (isServer ?? router.isServer) { const safeInternal = isSafeInternal(to) // If `to` is obviously an absolute URL, treat as external and avoid diff --git a/packages/react-router/src/useRouterState.tsx b/packages/react-router/src/useRouterState.tsx index 1e281645af7..f3fbb2ad6e9 100644 --- a/packages/react-router/src/useRouterState.tsx +++ b/packages/react-router/src/useRouterState.tsx @@ -56,8 +56,9 @@ export function useRouterState< // During SSR we render exactly once and do not need reactivity. // Avoid subscribing to the store (and any structural sharing work) on the server. - const _isServer = isServer ?? router.isServer - if (_isServer) { + // The expression must stay inlined in the `if` so bundlers fold the + // browser-build constant `isServer = false` and drop this server block. + if (isServer ?? router.isServer) { const state = router.stores.__store.get() as RouterState< TRouter['routeTree'] > diff --git a/packages/solid-router/src/useRouterState.tsx b/packages/solid-router/src/useRouterState.tsx index bf68d81ed44..382e35d2d2e 100644 --- a/packages/solid-router/src/useRouterState.tsx +++ b/packages/solid-router/src/useRouterState.tsx @@ -33,8 +33,9 @@ export function useRouterState< // During SSR we render exactly once and do not need reactivity. // Avoid subscribing to the store on the server since the server store // implementation does not provide subscribe() semantics. - const _isServer = isServer ?? router.isServer - if (_isServer) { + // The expression must stay inlined in the `if` so bundlers fold the + // browser-build constant `isServer = false` and drop this server block. + if (isServer ?? router.isServer) { const state = router.stores.__store.get() as RouterState< TRouter['routeTree'] > diff --git a/packages/vue-router/src/useRouterState.tsx b/packages/vue-router/src/useRouterState.tsx index 13b6f852369..7d3d536f57d 100644 --- a/packages/vue-router/src/useRouterState.tsx +++ b/packages/vue-router/src/useRouterState.tsx @@ -39,9 +39,9 @@ export function useRouterState< // During SSR we render exactly once and do not need reactivity. // Avoid subscribing to the store on the server since the server store // implementation does not provide subscribe() semantics. - const _isServer = isServer ?? router.isServer - - if (_isServer) { + // The expression must stay inlined in the `if` so bundlers fold the + // browser-build constant `isServer = false` and drop this server block. + if (isServer ?? router.isServer) { const state = router.stores.__store.get() as RouterState< TRouter['routeTree'] >