How to respond with different page.tsx file based on user's {user_agent} header in the reaquest body
Answered
✨AkeaKun✨ posted this in #help-forum
can we even do that using default next.js setup in serverless environments or we need cutom node.js server?
Answered by B33fb0n3
I like to use https://www.npmjs.com/package/react-device-detect
It can be used serverside and give me the variable
It can be used serverside and give me the variable
isMobile. Like that I can determine serverside if I would render the mobile version or desktop version 👍20 Replies
@✨AkeaKun✨ can we even do that using default next.js setup in serverless environments or we need cutom node.js server?
I like to use https://www.npmjs.com/package/react-device-detect
It can be used serverside and give me the variable
It can be used serverside and give me the variable
isMobile. Like that I can determine serverside if I would render the mobile version or desktop version 👍Answer
@✨AkeaKun✨ that's a life saver. thanks a lot.
sure thing. Please mark solution
@✨AkeaKun✨ that's a life saver. thanks a lot.
i know its kinda off topic, but what exactly are you trying to achive with this, as if its just styles using CSS is way better then hoping its correct (can be better responsive and worl well on laptop with smaller phone size)
i was trying to lower the amount of data sent to mobile devices. the desktop app is gonna be pretty huge and featureful. i'm still not sure if i'll use this approach tho
but good to know...
ah yeah that makes sense (kinda why on some sites they have
mobile.site.com and redirect to it - ie facebook and youtube)exactly
okay i'm gonna close this one. thanks for help
(just one last thing, i am not 100% sure if the js bundle will really get any difference if same page, so possibly to have best impact you should have it another page and maybe rewrite to make same as i think bundle will just have all for that page and be large)
So, we need to re-write the entire page with only necessary things for mobile and send that file instead.
that's what i thought actually. these big companies have multiple versions of their app
im thinking so, as like even with dynamic import, the initial js is still same
const Page = () => {
if(isMobile) {
return <MobileVersion/>
}
return <DeskTopVersion/>
}
if(isMobile) {
return <MobileVersion/>
}
return <DeskTopVersion/>
}
server will compile all of them but the client should only get one right?
yeah like you could think this should be fancy, but the returned "html" will be smaller... however all the JS needed for desktop version (client) will still be sent to this
damn
then i guess all the hassel will be for nothing
i'll try to optimize the app in other ways
sounds good, and enjoy! 👍