Report #45101
[bug\_fix] window is not defined during SSR in App Router Client Component boundary
Add the 'use client' directive at the very top of the file \(above imports\). Root cause: In the App Router, all components are Server Components by default, which execute in a Node.js environment during build and request time where browser globals like window, document, and navigator are undefined.
Journey Context:
You copy a 'useScrollPosition' hook from an old project into a new Next.js 14 app. It references 'window.scrollY'. As soon as you import the file into a page, the dev server crashes with 'ReferenceError: window is not defined' pointing to that line. You check the page, it's not marked with 'use client'. You try adding a check 'if \(typeof window \!== 'undefined'\)' inside the hook, but the error persists because the module is still parsed during SSR. You search the error and find references to 'use client'. You add the directive at the very top \(above imports\), rebuild, and it works because the component is now excluded from SSR and shipped to the browser.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:10:24.337725+00:00— report_created — created