I have a responsive web application built with ReactJS for which I want to one day support server-side rendering.
The layout / behavior of the application changes according to the size of the viewport. But all these changes can be made not only with simple CSS content: the JS behavior and the basic HTML structure must also be changed to fit the width.
For example, I could:
Under 800 pixels wide :
<div class="app"> <div class="menu-mobile">...</div> <div class="content">...</div> <div class="mobile-left-menu">...</div> <div class="footer-mobile">...</div> </div>
800px Width :
<div class="app"> <div class="menu">...</div> <div class="main"> <div class="left-menu">...</div> <div class="content">...</div> <div class="right-menu">...</div> </div> <div class="footer">...</div> </div>
Now I want to use server side rendering for this application. But on the server, I have no width, so I donβt know what HTML structure to return to the client.
Please note that I am not looking for a solution that uses a static breakpoint on the server side, and that there is an application fix on the client. I am looking for a solution that will return to the client the proper html structure according to its device. Therefore, it should work fine if it disables javascript in its browser.
It can be argued that I could display the html needed for both and hide / show the necessary parts using regular CSS and display: none media queries, but this will complicate the application and make it display a lot of unused html elements, because in general it is unlikely so that the user moves above / below the breakpoint (I mean that if he has a mobile device, the html elements for the desktop will never be used)
Also, if I want to use the inline style, it becomes complicated because I have to display these inline styles for the correct width on the server.
I saw some people think about sniffing the UA browser in order to estimate the estimated size of their viewport. But even with some insecure detection of screen sizes, I'm not sure that we can know the device screen orientation on the server side.
Can I do anything to solve this problem?
reactjs media-queries isomorphic-javascript inline-styles serverside-rendering
Sebastien lorber
source share