Building Server-Driven UIs
7 min readServer-Driven UIs known as SDUI is an architectural pattern that aims to reduce client-side complexity by building user-interfaces from the server over APIs. This is transforming the way apps are developed, fostering dynamic and flexible UIs.
This is not just theory. Some of the biggest names in the tech industry adopted Server-Driven UIs, such as: Airbnb, Instagram and Shopifyβjust to name a few. Spoiler: we also use this at N26.
In this post we will explore what are Server-Driven UIs, their benefits and how to implement them π
Table of contents
- Understanding Server-Driven UIs
- Building Server-Driven UIs
- Benefits of Server-Driven UIs
- Challenges of Server-Driven UIs
- Conclusion
Understanding Server-Driven UIs
The best way to understand Server-Driven UIs is by comparing it with the traditional approach.
In the traditional world, the data comes from the server π», which acts as the source of truth and the UI is driven by each of those clients (Web, iOS, Android). Each client is responsible for fetching the data and transforming it into a user-interface.
With SDUI, the definition of the user-interface shifts from the client to the server, unifying this logic for all of them. The clients will fetch the UI from the server and render it.
Let's illustrate this with an example, using the Apple Podcasts webapp:
Now, let's represent the user-interface of the Top Technology Podcasts page using a JSON to simulate the server response:
Traditional
A contract is defined between the client and the server, adhering to a specific structure and shape.
The client will fetch the data from the server and will transform it into a user-interface.
Server-Driven UI
With SDUI the server will send a tree-like structure of components to the client. Each component represents a part of the UI and contains information about what to render and the properties the component needs.
The client will traverse this tree π³, rendering each component as specified by the server.
Building Server-Driven UIs
Now that we understand the concept behind Server-Driven UIs, let's explore how to build them in a practical scenario.
We will break down the process into the following steps:
- Define the component tree π³: Define the components and the tree that represents the user-interface.
- Implement the components π§©: Create a component for each definition.
- Build the rendering engine βοΈ: Traverse the JSON tree and render each component as specified.
Define the component tree
Based on the JSON response we defined previously, here's how we're going to break down the user-interface into components:
Title
: The main title of the page.SectionTitle
: The title of a section.PodcastList
: A list of podcasts.Podcast
: The podcast item.EpisodeList
: A list of episodes.Episode
: The episode item.
Implement the components
After defining the tree, it's time to build the components. I will be doing it with React and Tailwind but you can use any other library.
Toggle components code π
Build the rendering engine
This is where the magic happens πͺ. We are going to implement our rendering engine by traversing the component tree and matching each node with the corresponding component.
To do that, first we define a map of components that we will use to match the type
of each node with every component.
Then, we create a component that using recursion will traverse the tree and render it. In case you're not familiar with recursion here's a video from Sam Selikoff that explains it very well.
Here's how all the pieces come together πΉοΈ, feel free to play around with the response to see how the user-interface reacts π
Benefits of Server-Driven UIs
Immediate changes and faster iterations
Server-Driven UIs allow you to push immediate bug fixes β‘οΈ and updates without having to release an update to the clients.
This is especially useful in environments like mobile apps, where updates typically require going through a review process and waiting for users to install the new update.
Reduce client-side complexity
By shifting the UI logic π§ to the server, Server-Driven UIs make the client ligther and simpler π°. The client only needs to focus on rendering the components provided by the server.
Dynamic user-interfaces
Defining the UI on the server enables highly adaptable and dynamic interfaces that can be tailored on-the-fly π―, such as:
Personalization: Customize layouts and content for different user segments, devices, or use cases.
A/B Testing: Experiment with different designs and features by serving different variants to user groups.
Release flags: Gradually roll out new features by enabling them for a subset of users.
Challenges of Server-Driven UIs
- State management π€Ή: Synchronizing state between the client and the server can be challenging, specially when dealing with complex interactions as the server needs to keep up with the client's state and update the UI accordingly.
- Offline support π: Server-Driven UIs rely on the server, managing the user experience when offline can be challenging. Caching and pre-fetching stategies can help mitigate this.
- Performance considerations β‘οΈ: Efficient network requests, payload optimization, low latency along with a good client-side rendering performance are crucial to ensure a smooth user experience.
Conclusion
Server-Driven UIs represent a paradigm shift in how we approach user interface development, offering a powerful tool for building dynamic and flexible apps β€οΈ.
However it's key to asses whether SDUI aligns with your project's needs and constraints. It's not a one-size-fits-all, but for the right use casesβsuch as content-heavy user-interfaces and apps requiring frequent updates it can significantly enhance delivery speed.
Enjoyed the article? π