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

查看:32
本文介绍了如何在 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天全站免登陆