Search code examples
expressnext.js

How do I detect whether I am on server on client in next.js?


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.


Solution

  • 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