This post was inspired by a recent project. I was making an HTTP request and ran into some issues. I posted my question on stackoverflow and received numerous responses that not only helped me to solve the problem but also helped me discover the concept of isomorphism. Im sure some new developers can relate to the knee jerk reaction to using jQuery for all AJAX requests. Until recently, I was that developer! I had become so dependent on it, I did not stop to think that there could be better alternatives. So lets talk about isomorphism!
The Basics: Conceptualization
Lets start with the general definition of Isomorphism:
Isomorphism means corresponding or similar in form and relations.
So in relation to web development, a JavaScript application deemed “isomorphic” is an application that can run both client-side and server-side (in most cases it is a single-page application that can be run on server).
Isomorphic Libraries
Now that we have an idea of what isomorphism means, lets tackle the concept of isomophic libraries. An example of an isomorphic library would be ReactJS. ReactJS is an open source library developed by Facebook, that operates as the V in MVC (Model, View, Controller). What makes ReactJS isomorphic is its ability to be rendered server side. It means a NodeJS server is able to parse your ReactJS components and return the HTML to the browser.
Isomorphic library V.s jQuery library
I believe the argument can be made, that choosing an isomophic library for data fetching over jQuery AJAX request is more a developer preference. For instance, if you are concerned with optimal performance you may opt for an isomorphic fetching library. JQuery performance or the lack there of, may not be noticeable in smaller applications but for large production grade applications, this is the less than ideal choice.
For those who may be interested in the debate of if we as developers need jQuery, check out this post:
https://mmikowski.github.io/why-jquery/
The author gives in depth reasons as to why we need jQuery and when to use it.
Advantages of Isomorphism
- For Isomorphic SPA’s, the first page request is fast and subsequent ones are even faster; as opposed to common SPAs, where the first request is used to load the application and then a round trip is made to fetch what was requested.
- There is less code, as it is shared by both the client and the server.
Image above illustrates the differences between an Isomorphic application and a server rendered applcation. In an Isomorphic website, server rendered HTML is returned to the client on the first page request. The HTML contains special markup that allows an asynchronously loaded client-side SPA to attach and handle future rendering on the client. As the user changes views or pages, the client-side SPA completely takes over navigation and rendering, only using the server for API requests as needed.
Why it matters?
Get gURLmeetsCode’s stories in your inbox
Join Medium for free to get updates from this writer.
The initial SPA page can be slow to load because of the time it takes to load and parse client side JavaScript. While this delay is only a matter of a few seconds or milliseconds, users can become impatient and leave the site causing a company to lose revenue.
Practical Use:
Here is a snippet of code, displaying how you would integrate the npm isomorphic library, isomorphic-fetch.
require('es6-promise').polyfill();require('isomorphic-fetch');
fetch('//offline-news-api.herokuapp.com/stories').then(function(response) {if (response.status >= 400) {throw new Error("Bad response from server");}return response.json();}).then(function(stories) {console.log(stories);});
The key to understanding how the fetch works is within the .then() promise and callback function. The promise represents the eventual result of an asynchronous operation. It is a placeholder into which the successful result value or reason for failure will materialize. In this case, a promise fulfilled is a sucessful callback response and a rejection is due to a failed callback.
Additional Resources:
If you are interested in integrating ismorphic libraries in your projects, you should check out some of these:










