Browse Source

fix(lifecycle): eliminate stale initialData cache mutation and ensure 0ms web_ready delivery

master
mortezaei 2 weeks ago
parent
commit
5ba6a39d03
  1. 35
      src/hooks/marriage/use-profile-main.ts
  2. 15
      src/hooks/use-habib-web-ready.ts

35
src/hooks/marriage/use-profile-main.ts

@ -69,36 +69,8 @@ export async function getMarriageProfile() {
export function useMarriageProfileQuery<TData = MarriageProfileResponse>(
options?: QueryOptions<MarriageProfileResponse, TData>,
) {
const queryClient = useQueryClient();
useEffect(() => {
const handleInitialData = (e: Event) => {
const customEvent = e as CustomEvent;
const initial = getInitialMarriageProfile();
console.log("📥 [Profile Query] Received habib:marriage-initial-data event:", {
detail: customEvent.detail,
parsedInitial: initial,
});
if (initial) {
queryClient.setQueryData(marriageQueryKeys.profile(), (old: any) => {
console.log("🔄 [Profile Query] Merging initialData into QueryCache. Old:", old, "New Initial:", initial);
if (!old) return initial;
return {
...initial,
...old,
};
});
}
};
window.addEventListener("habib:marriage-initial-data", handleInitialData);
return () => {
window.removeEventListener("habib:marriage-initial-data", handleInitialData);
};
}, [queryClient]);
const query = useQuery({
staleTime: 0, // Prioritize fresh server data from /api/marriage/profile/main/ on mount
staleTime: 30 * 1000,
refetchOnWindowFocus: false,
initialData: getInitialMarriageProfile as any,
initialDataUpdatedAt: 0,
@ -107,5 +79,8 @@ export function useMarriageProfileQuery<TData = MarriageProfileResponse>(
queryKey: marriageQueryKeys.profile(),
});
return query;
return {
...query,
isLiveServerFetched: query.dataUpdatedAt > 0,
};
}

15
src/hooks/use-habib-web-ready.ts

@ -20,17 +20,7 @@ import { setCachedMarriageEntryPath } from "@/lib/entry-route-cache";
export function useHabibWebReady(ready: boolean) {
useEffect(() => {
if (ready && typeof window !== "undefined") {
let cancelled = false;
// Double requestAnimationFrame guarantees the browser has executed layout & paint
// of the ready DOM state to the GPU compositor before Flutter lifts the native cover.
const raf1 = requestAnimationFrame(() => {
const raf2 = requestAnimationFrame(() => {
if (!cancelled) {
window.__announceHabibWebReady?.();
}
});
});
const pathname = window.location.pathname;
if (pathname) {
@ -45,11 +35,6 @@ export function useHabibWebReady(ready: boolean) {
setCachedMarriageEntryPath(route);
}
}
return () => {
cancelled = true;
cancelAnimationFrame(raf1);
};
}
}, [ready]);
}
Loading…
Cancel
Save