krux / postscribe

Asynchronously write javascript, even with document.write.
MIT License
986 stars 156 forks source link

Error - `window is not defined` for React/Nextjs #514

Open rwsabhishek opened 1 year ago

rwsabhishek commented 1 year ago

I am facing the below error in react/next js - window is not defined

MY SOLUTION - So to resolve this for now, I have to edit the node module postscribe.js file and add following conditions in line 1335. image image

Edited Code on line 1335 - var work={}; if (typeof window !== "undefined") work = window.document.createElement('div');

It will be helpful if you add this condition at your module js file. So i will use the latest version

thehungrycoder commented 1 year ago

Found same issue just now. @rwsabhishek thanks for the hint. Hopefully it'll be part of core package.

constmoon commented 1 year ago

Try dynamic import when importing components that using postscribe.

import dynamic from 'next/dynamic'

const ExternalScriptComponent = dynamic(() => import('@/components/...'), {
  ssr: false,
})