Skip to content

Commit c568caf

Browse files
inline isServer for proper DCE (#7996)
1 parent 4fa1df7 commit c568caf

4 files changed

Lines changed: 12 additions & 11 deletions

File tree

packages/react-router/src/link.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ export function useLinkProps<
166166
const router = useRouter()
167167
const innerRef = useForwardedRef(forwardedRef)
168168

169-
// Determine if we're on the server - used for tree-shaking client-only code
170-
const _isServer = isServer ?? router.isServer
171-
172169
const {
173170
// custom props
174171
activeProps,
@@ -220,7 +217,9 @@ export function useLinkProps<
220217
//
221218
// Note: `location.hash` is not available on the server.
222219
// ==========================================================================
223-
if (_isServer) {
220+
// The expression must stay inlined in the `if` so bundlers fold the
221+
// browser-build constant `isServer = false` and drop this server block.
222+
if (isServer ?? router.isServer) {
224223
const safeInternal = isSafeInternal(to)
225224

226225
// If `to` is obviously an absolute URL, treat as external and avoid

packages/react-router/src/useRouterState.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ export function useRouterState<
5656

5757
// During SSR we render exactly once and do not need reactivity.
5858
// Avoid subscribing to the store (and any structural sharing work) on the server.
59-
const _isServer = isServer ?? router.isServer
60-
if (_isServer) {
59+
// The expression must stay inlined in the `if` so bundlers fold the
60+
// browser-build constant `isServer = false` and drop this server block.
61+
if (isServer ?? router.isServer) {
6162
const state = router.stores.__store.get() as RouterState<
6263
TRouter['routeTree']
6364
>

packages/solid-router/src/useRouterState.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ export function useRouterState<
3333
// During SSR we render exactly once and do not need reactivity.
3434
// Avoid subscribing to the store on the server since the server store
3535
// implementation does not provide subscribe() semantics.
36-
const _isServer = isServer ?? router.isServer
37-
if (_isServer) {
36+
// The expression must stay inlined in the `if` so bundlers fold the
37+
// browser-build constant `isServer = false` and drop this server block.
38+
if (isServer ?? router.isServer) {
3839
const state = router.stores.__store.get() as RouterState<
3940
TRouter['routeTree']
4041
>

packages/vue-router/src/useRouterState.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export function useRouterState<
3939
// During SSR we render exactly once and do not need reactivity.
4040
// Avoid subscribing to the store on the server since the server store
4141
// implementation does not provide subscribe() semantics.
42-
const _isServer = isServer ?? router.isServer
43-
44-
if (_isServer) {
42+
// The expression must stay inlined in the `if` so bundlers fold the
43+
// browser-build constant `isServer = false` and drop this server block.
44+
if (isServer ?? router.isServer) {
4545
const state = router.stores.__store.get() as RouterState<
4646
TRouter['routeTree']
4747
>

0 commit comments

Comments
 (0)