如何检测我是否在 next.js 中的客户端服务器上? [英] How do I detect whether I am on server on client in next.js?

查看:30
本文介绍了如何检测我是否在 next.js 中的客户端服务器上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 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 中的客户端服务器上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆