router.refresh is not working in first load

can anyone tell me why if(!firstTime) is not working ? when i put it inside setTimout for 10s it start working but when i am throttling the network it again stops working? Does anyone have reliable solution for this?
"use client";

import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { useEffect, useState, useTransition } from "react";

export default function useUpdateRole() {
const { update } = useSession();
const router = useRouter();
const [firstTime, setFirstTime] = useState(false);
const [isPending, startTransition] = useTransition();
function RefreshThePage() {
startTransition(() => {
router.refresh();
});
}
async function UpdateAndRefresh() {
await update();
RefreshThePage();
}
useEffect(() => {
if (!firstTime) {
UpdateAndRefresh();
setFirstTime(true);
}
const interval = setInterval(UpdateAndRefresh, 1000 * 60 * 60);
return () => clearInterval(interval);
}, [update]);
useEffect(() => {
const visibilityHandler = async () =>
document.visibilityState === "visible" && UpdateAndRefresh();
window.addEventListener("visibilitychange", visibilityHandler, false);
return () =>
window.removeEventListener("visibilitychange", visibilityHandler, false);
}, [update]);

return null;
}
"use client";

import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { useEffect, useState, useTransition } from "react";

export default function useUpdateRole() {
const { update } = useSession();
const router = useRouter();
const [firstTime, setFirstTime] = useState(false);
const [isPending, startTransition] = useTransition();
function RefreshThePage() {
startTransition(() => {
router.refresh();
});
}
async function UpdateAndRefresh() {
await update();
RefreshThePage();
}
useEffect(() => {
if (!firstTime) {
UpdateAndRefresh();
setFirstTime(true);
}
const interval = setInterval(UpdateAndRefresh, 1000 * 60 * 60);
return () => clearInterval(interval);
}, [update]);
useEffect(() => {
const visibilityHandler = async () =>
document.visibilityState === "visible" && UpdateAndRefresh();
window.addEventListener("visibilitychange", visibilityHandler, false);
return () =>
window.removeEventListener("visibilitychange", visibilityHandler, false);
}, [update]);

return null;
}
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server