In web development, hydration and rehydration are two terms that often get used interchangeably. Although there is a distinction between the two, it’s not always clear to beginners what the difference is.
Rehydration is the process of taking an HTML document originally written in JSON or XML and converting it back into an XML-like tree of DOM nodes for processing and rendering on a web page. This article demonstrates how this can be done by using vanilla JavaScript code snippets from a compatible library like JQuery, which offers a powerful yet simple interface that allows developers to easily convert JSON or XML data into DOM nodes on a web page after the initial parsing process has taken place.
Hydration, on the other hand, is a term used to describe the second phase of Ajax. In this case, an XMLHttpRequest is used to fetch data from a remote server, and after that data is displayed on a web page. Ajax is essentially about the communication between two separate applications – one runs in your web browser and another on a remote server. The process of obtaining data from one or more servers is known as an Ajax request.
Before you can send off your Ajax request to retrieve some JSON or XML data from the server (after performing an initial Ajax get request), you need to first load that URL into your web browser’s address bar. The easiest way to do this is to use JavaScript (or some other client-side scripting language) and include a script tag. The JavaScript code contained within the script tag will be parsed by the browser and executed once the page has fully loaded.
Once that initial loading of your page has taken place, it’s time for you to talk to a remote server using an Ajax request. Since that process involves sending your HTTP request over a wire back to the server, it needs to be done as close as possible to when you made that request so that all of the communication can happen in parallel with both of these tasks being performed at once on both ends of those two separate variables – your web browser and your server.
This is where hydration comes in. It’s the act of converting the XMLHttpRequest object into a compatible object that can be passed along to JavaScript. It is an optional process, and does not always need to be done – sometimes one doesn’t have to use it, so there are times when hydration can be completely ignored. But usually, it’s a good idea to hydrate all XMLHttpRequest objects before passing them off to your page’s JavaScript functions.
To identify if the hydration process is impacting your page load times, I recommend starting by observing the load time of your web page.
If it takes considerably longer than you think it should, it’s possible that hydration is hindering your site’s performance by slowing down the initial loading of your page.
Once you’ve determined that Hydration is negatively affecting performance in some way, there are two main ways to resolve the issue:
- Overwrite or make changes to existing hydrated XMLHttpRequest objects.
- Overwrite or make changes to existing hydrated JavaScript functions.
To use one or more of these methods, all it takes is identifying where in your code Hydration is taking place and then modifying those areas accordingly. The modifications you make will depend on the results of your analysis, but when it comes to making performance fixes – testing the results of each fix/modification is vital.
To identify where in your code Hydration is taking place, you’ll need to study the page’s source code. This sounds like a huge task, but a variety of browser add-ons can be used to quickly analyze your page’s source code and find the area where the problem is located.
One I recommend using is Firebug, which is freely available for Google Chrome, Mozilla Firefox, and Opera.
Once you’ve used either Firebug or another similar addon to isolate the issue area in your code, it’s time for you to make some changes and see how they affect performance. It could very well be that simply modifying some JavaScript variable names has no effect on your overall load times at all.