Repository navigation
Link to the code that reproduces this issueTo Reproduce
"use client";
const Chocolate = () => {
console.log(window.location.href);
return <span>Chocolate</span>;
};
export default Chocolate;
⨯ src/app/chocolate.tsx (4:15) @ window
⨯ ReferenceError: window is not defined
at Chocolate (./src/app/chocolate.tsx:9:17)
digest: "505391162"
2 |
3 | const Chocolate = () => {
> 4 | console.log(window.location.href);
| ^
5 |
6 | return <span>Chocolate</span>;
7 | };
GET / 500 in 19msCurrent vs. Expected behaviorI expect window to be defined in client components. Provide environment informationOperating System:
Platform: linux
Arch: x64
Version: #1 SMP PREEMPT_DYNAMIC Mon Sep 30 21:38:25 UTC 2024
Available memory (MB): 30842
Available CPU cores: 16
Binaries:
Node: 20.17.0
npm: 10.8.2
Yarn: 1.22.22
pnpm: N/A
Relevant Packages:
next: 15.0.1 // Latest available version is detected (15.0.1).
eslint-config-next: N/A
react: 19.0.0-rc-69d4b800-20241021
react-dom: 19.0.0-rc-69d4b800-20241021
typescript: 5.6.3
Next.js Config:
output: N/AWhich area(s) are affected? (Select all that apply)Developer Experience Which stage(s) are affected? (Select all that apply)next dev (local) Additional contextWrapping the console.log in a |
Answered by
samcx
Oct 23, 2024
Replies: 1 comment 1 reply
|
@perkrlsn A Client Component still technically runs on the server first before being available on the Client → https://nextjs.org/docs/app/building-your-application/rendering/client-components#using-client-components-in-nextjs. https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#windowhistorypushstate → in this example, the |
1 reply
Answer selected by
perkrlsn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@perkrlsn A Client Component still technically runs on the server first before being available on the Client → https://nextjs.org/docs/app/building-your-application/rendering/client-components#using-client-components-in-nextjs.
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#windowhistorypushstate → in this example, the
windowobject is accessed only until the client module has been loaded.