How to deploy adaptive Next Js website on a single domain?

I have built an adaptive website in Next js; Which has two versions one for mobile devices and another for Desktop. I want to deploy both on the same domain and open the respective versions based on user-agent.

example: www.test.com will load mobile site if found mobile User-Agent else Desktop site

I’m able to load the respective version of the site If I have a sub-domain like m.test.com but am not able to do it on the same domain.

Can anyone please help with this?

Thanks.