I've got an easy issue but no way.
Answered
Verin posted this in #help-forum
Answered by Plott Hound
Go back to your original code and try wrapping anything that tries to access the document in this
if (typeof window !== "undefined") {
// Client-side-only code
}
if (typeof window !== "undefined") {
// Client-side-only code
}
56 Replies
@Verin Why is this happening and how to?
Plott Hound
The “window†and “document†are client-side APIs and because Next pre-renders each page — server-side, we can’t access those APIs simply because they don’t exist (server-side) yet.
share the code that is causing it
VerinOP
which file code do I have to share?
user / dashboard / page.tsx? or what?
Plott Hound
somewhere in your code you are trying to access the window. if you didnt intentionally do it then maybe it is an npm lib you are using? i have no idea without seeing your code
VerinOP
I never use window object in my code.
From the error message, Is this error in the /user/dashboard/page?
From the error message, Is this error in the /user/dashboard/page?
Plott Hound
its happening on the root
'/' and '/user/dashboard'Do you think there is an issue in this code?
Plott Hound
all i can see are two components but no actual code. what is in default layout and dashboard?
VerinOP
default layout is not problem because this code is working before and did not updated.
So If there is an issue, that is just dashboard.
Shall I share the dashboard code?
Shall I share the dashboard code?
Its working well in the dev mode.
But I got this error when building.
But I got this error when building.
VerinOP
share code?
Plott Hound
yes
VerinOP
I did not still solve this problem.
Plott Hound
are you going to share the code?
VerinOP
How to share it? file or copy and pase here?
Plott Hound
yes
VerinOP
Plott Hound
can i see the header and sidebar?
VerinOP
Plott Hound
looks fine so far, show me the sidebar and header
Plott Hound
ok the issue is in the sidebar
you are accessing the document there
Plott Hound
You need to make sure the client has mounted before you access the document
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);
if (!mounted) return null;let me know how you get on
VerinOP
sec
Plott Hound
sorry just removed my first suggestion as it didnt work
VerinOP
I add this code as you guide.
Plott Hound
put it before everything else
before the first useEffect in your code
Plott Hound
ok instead try this:
const Sidebar = ({ sidebarOpen, setSidebarOpen }: SidebarProps) => {
const pathname = usePathname();
const trigger = useRef<any>(null);
const sidebar = useRef<any>(null);
const { data: session } = useSession();
// useState initialization moved inside useEffect for server-side safety
const [sidebarExpanded, setSidebarExpanded] = useState(false);
// Effect for localStorage access
useEffect(() => {
const storedSidebarExpanded = localStorage.getItem("sidebar-expanded") === "true";
setSidebarExpanded(storedSidebarExpanded);
}, []);
// Close on click outside
useEffect(() => {
const clickHandler = ({ target }: MouseEvent) => {
if (!sidebar.current || !trigger.current) return;
if (
!sidebarOpen ||
sidebar.current.contains(target) ||
trigger.current.contains(target)
)
return;
setSidebarOpen(false);
};
document.addEventListener("click", clickHandler);
return () => document.removeEventListener("click", clickHandler);
}, [sidebarOpen]);
// Close if the Esc key is pressed
useEffect(() => {
const keyHandler = ({ key }: KeyboardEvent) => {
if (!sidebarOpen || key !== "Escape") return;
setSidebarOpen(false);
};
document.addEventListener("keydown", keyHandler);
return () => document.removeEventListener("keydown", keyHandler);
}, [sidebarOpen]);
// UseEffect for handling class addition/removal on body
useEffect(() => {
localStorage.setItem("sidebar-expanded", sidebarExpanded.toString());
if (sidebarExpanded) {
document.querySelector("body")?.classList.add("sidebar-expanded");
} else {
document.querySelector("body")?.classList.remove("sidebar-expanded");
}
}, [sidebarExpanded]);
// Your component's return statement goes here
};VerinOP
still not working
Plott Hound
Same error?
VerinOP
yes
Plott Hound
Go back to your original code and try wrapping anything that tries to access the document in this
if (typeof window !== "undefined") {
// Client-side-only code
}
if (typeof window !== "undefined") {
// Client-side-only code
}
Answer
Plott Hound
That should do it
VerinOP
If I success the build with this code, then I think any actions will not work on the browser.
btw, it's not working
Plott Hound
you know the cause of the issue now - trying to access document before it has mounted. i'll look over your code again and see if i can find a solution but essentially you cannot access document before it is mounted on the client, even if you add 'use client'
VerinOP
will you access to my pc and solve it?
VerinOP
Hello
That is working.
@Verin That is working.
Plott Hound
you got it working?
VerinOP
sorry, i'll be in 20
VerinOP
I've open the new post.
https://nextjs-forum.com/post/1220282635147022367#message-1220282635147022367
Will you go through this?
https://nextjs-forum.com/post/1220282635147022367#message-1220282635147022367
Will you go through this?