The reason for this issue lies in server-side rendering as Next.js is executed on the server side where the 'window' object does not exist since it pertains to the client side only.
One common practice is to create a function that initializes the 'window' object once the code is running on the client browser:
(Assuming you are working with ReactJS based on the component structure and fragment usage in the provided code snippet)
export default function TestPage() {
const [audioContext, setAudioContext] = useState<AudioContext | null>(null)
useEffect(() => {
if (typeof window !== 'undefined') {
const val = window.AudioContext = window.AudioContext || window.webkitAudioContext;
setAudioContext(val)
// Alternatively, carry out your operations here directly without setting state
}
}, [])
useCallback(() => {
if (audioContext !== null) {
const audioCtx = new audioContext();
// Implement your additional logic related to audio context here
}
}, [audioContext ])
return <>Hello</>
}