Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions packages/router/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import { createAdapter } from "./core/createAdapter.js";
import { executeLoaders, createLoaderRequest } from "./core/loaderCache.js";
import type { RouteDefinition } from "./route.js";

const noopSubscribe = () => () => {};
const getServerSnapshot = () => null;

export type RouterProps = {
routes: RouteDefinition[];
/**
Expand Down Expand Up @@ -61,9 +64,7 @@ export function Router({
const [blockerRegistry] = useState(() => createBlockerRegistry());

// Hydration-aware initial value: null during SSR/hydration, real on client-only mount
const noopSubscribe = useCallback(() => () => {}, []);
const getSnapshot = useCallback(() => adapter.getSnapshot(), [adapter]);
const getServerSnapshot = useCallback(() => null, []);
const initialEntry = useSyncExternalStore(
noopSubscribe,
getSnapshot,
Expand All @@ -75,13 +76,8 @@ export function Router({
initialEntry,
);

// Subscribe to navigation changes and sync state
// Subscribe to navigation changes (wrapped in transition)
useEffect(() => {
// Immediately sync with current snapshot on mount
// This handles the case where the snapshot changed between SSR and hydration
setLocationEntry(adapter.getSnapshot());

// Subscribe to future changes (wrapped in transition)
return adapter.subscribe(() => {
startTransition(() => {
setLocationEntry(adapter.getSnapshot());
Expand Down