Spend any time in web development and you will be struck by the daunting pace at which the technology landscape changes. The must-have technologies of today quickly become the legacy spaghetti code of yesterday. In some cases, adopting new technology is as simple as adding a new library. Other times, large scale architectural changes need to be made. For those looking to move from traditional server side MVC apps to newer client side single page apps, the migration path is not easy nor clear cut.
Here at Bitsight, we are taking on this challenge in a structured, manageable way. In this article, we hope to share some of our lessons learned.
Why Choose Single Page Apps
Why did we decide to go through all this trouble in the first place? The primary reason is that single page apps offer an enhanced user experience by being more responsive and dynamic. Through only rendering sections of the current page, we do away with the feel of traditional web apps, with their frequent page reloads, and move closer to what feels like a native desktop or mobile apps.
Single page apps also offer opportunities for performance optimization by reducing the number of network requests made to the server. Without clever caching strategies, traditional MVC apps may be subject to redundant requests and renders of shared resources across their many views.
Finally, the process of this migration has forced us to improve our code structure. We are moving toward a better separation of concerns between the browser and server, as well as enabling UI reusability through well-defined components.
What Needs to be Done
Traditional MVC apps are driven by server-side route handlers which render separate views for each URL. In Bitsight’s case, we had multiple views/pages driven by a Django app server. Every time that a user navigated to a new URL, a round trip to the server was made and a new page rendered. The migration to a single page app means planning for a complete change in this behavior. In a single page app, the route handling is moved to the client side. At Bitsight, we made the decision to move to using React/Redux and its associated route handler. In this new framework, every time the client navigates to a new URL, the code dynamically re-renders portions of the existing page, and new requests to the server are limited to API calls. As a result of this change in control, we gain the flexibility of rendering and delivering a much smaller set of data per navigation.
What We Learned
Here are five things that helped smooth our transition to the single page app.
1. Pick the Right Framework
It can be trickier than you'd assume to pick the right framework. Internet searches will lead to many different opinions about which is the "right" framework. At Bitsight, we decided to go with React and Redux for the following reasons:
- It has a strong ecosystem of corporate and developer support.
- The syntax and structure of the code leads to better module structure and organization.
- When using the framework, there should be a reasonable expectation that given a complex UI, an implementation using the framework will perform well.
2. A Well Defined API
Single page apps are heavily dependent on well-defined APIs for data retrieval. At Bitsight, we were already busy crafting a public API for our customers to use for their own app development needs, so our transition to a single page app became a good case of ‘eating your own dog food.’ As we built out our app, we were able to find deficiencies in our API and fix them. If your organization does not have a strong API or SDK, you will want to start planning that out before you begin transitioning to a single page architecture. It can be helpful to involve your frontend engineers in the process of creating these APIs. Otherwise, your developers may find themselves quickly blocked by the lack of access to data.
3. Define a Container / Component Structure
In a single page app, sections of the UI are replaced dynamically based on user interaction. Planning which parts of the view are dynamic will go a long way in helping you organize your modules. This is where working with your design teams to come up with well-defined wireframes will speed up your development process.