How do I detect I am on server vs client in Next.js

Dave Stein picture Dave Stein · Mar 21, 2018 · Viewed 28.2k times · Source

I am using a customer express server with Next.js. It's running within a container. I am doing an http request with isomorphic-fetch to get data for my render. I'd like to do localhost when running on server and mysite.com when running on client. Not sure the best way to accomplish this. I can do it hackily by doing const isServer = typeof window === 'undefined' but that seems pretty bad.

Answer

kenberkeley picture kenberkeley · Jan 2, 2020

Now (2020 Jan) it should be typeof window === 'undefined' since process.browser is deprecated

Refer to https://github.com/zeit/next.js/issues/5354#issuecomment-520305040