Next.js Discord

Discord Forum

how to identify if the person is mobile or not on an async page?

Unanswered
Sammy Kunimatsu posted this in #help-forum
Open in Discord
I'm trying to see if the person is on mobile or desktop with javascript on an async page, but it can't return me, how to do it?

5 Replies

What is an ”async page”? I guess you’re referring to a server component.

First of all, you need to do it in client components.

There’s a native property that can indicate the device of user: navigator.userAgent
However, it’s better to determine it with viewport width, since that user agent isn’t reliable
And it depends on what you are trying to do. Like detecting a touch screen device is much easier. “Mobile device” is something abstract and hard to be determined.
@Sammy Kunimatsu It seems that it is not possible to use the navigator on a server component page.
you can only access navigator in a client component. if you want to access the user agent on the server-side, you can use the [User-Agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) header though as fuma said it's unreliable
Yep, as mentioned above, you must do it in a client component