问题描述
我正在将客户快递服务器与Next.js一起使用.它在容器中运行.我正在使用 isomorphic-fetch
进行http请求,以获取渲染的数据.我想在服务器上运行时执行 localhost
,而在客户端上运行时执行 mysite.com
.不确定实现此目的的最佳方法.我可以通过 const isServer = typeof window ==='undefined'
轻松地做到这一点,但这似乎很糟糕.
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.
推荐答案
现在(2020年1月),它应该是 typeof window ==='undefined'
,因为 process.browser
已弃用
Now (2020 Jan) it should be typeof window === 'undefined'
since process.browser
is deprecated
请参阅 https://github.com/zeit/next.js/issues/5354#issuecomment-520305040
这篇关于如何在next.js中检测我是否在客户端的服务器上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!