Front-end Architecture

Much more than CSS and HTML

Walter Gandarella • December 27, 2024

Hey, folks! Today we're going to dive deep into a topic that's making waves in the web development world: architecture in the front-end ecosystem. That's right, folks, if you thought front-end was just about neatly separating CSS from HTML, get ready for quite a journey!

Let's start by deconstructing that simplistic idea. Front-end has evolved a lot in recent years, and today we're talking about a complexity that rivals back-end in many respects. It's no wonder terms like "back-end of the front-end" and "micro front-ends" have emerged. But hold on, let's take it step by step.

The Evolution of Front-end

Do you remember when everything was a mess, with HTML, CSS, and JavaScript all mixed up on a single page? Well, we went through a phase of separating these responsibilities, creating distinct files for each language. That was quite an advancement, but the world didn’t stop there.

Over time, web applications became more complex. Pages needed to be more dynamic, with more elaborate interactions. And that's when a migration began: part of the logic that was previously on the server started being executed in the client's browser. Front-end gained muscle!

But, as Uncle Ben from Spider-man would say, "with great power comes great responsibility." And with this increasing complexity, the need to better organize front-end code arose. It was no longer feasible to throw everything into a giant JavaScript file and expect it to work.

Layered Architecture

Just imagine: you're working on a project with 100 other developers. Each one handles a specific part of the application. How do you ensure everyone is speaking the same language? How do you prevent the code from turning into a digital Tower of Babel?

That's where architecture comes in. It's not just about dividing files or using modern frameworks. It's about creating structures that allow the code to grow in an organized way, that are easy to maintain, and that stand the test of time.

Think of architecture like the design of a city. You don't just build buildings randomly, right? You need to plan where the streets, neighborhoods, and sewage systems will be. In code, it's the same: you need to define where the different responsibilities will be, how components will communicate, and how to handle the application state.

When we talk about front-end architecture, we're not just talking about separating HTML, CSS, and JavaScript into different files (though that is important too!). We're talking about a much deeper organization, which can include:

  • Presentation Layer: Responsible for the visual interface (HTML/CSS)
  • Application Layer: Manages state and navigation
  • Domain Layer: Implements business rules
  • Data Layer: Handles communication with APIs and storage

The Clean Architecture

This layered division is not much different from what we see in back-end with the Clean Architecture. And it's no coincidence! The principles of good software architecture are universal.

The Back-end for Front-end (BFF)

Hold on, it's not a new boy band! BFF, in this context, stands for "Backend For Frontend". It's an intermediate layer between the front-end and various back-end microservices. (We've even published an article about BFF here on DevCafe!)

Think of it this way: back in the day, you had a monolith in the back-end that did everything. Then came microservices, which are great for the back-end, but can complicate life on the front-end. Imagine having to make dozens of calls to different APIs just to assemble a single page!

BFF API Namespacing

BFF solves that. It acts as an aggregator, making all these calls for the front-end and delivering the data already processed, in the way the front-end needs. Less headache for front-end devs, less network traffic, everyone happy!

Micro Front-ends: Divide to Conquer

If you thought microservices were just a back-end thing, surprise! The concept of micro front-ends is here to prove otherwise. The idea is to apply the same principles of microservices to the front-end.

Organisation in Verticals

Instead of having a monolithic front-end application, break it down into smaller pieces, each responsible for a specific functionality. Each micro front-end can be developed, tested, and deployed independently.

Imagine an e-commerce site. It could have one micro front-end for the product catalog, another for the shopping cart, another for the search system... Each of these could be developed by different teams, even using different technologies if it makes sense.

SOLID in Front-end? Yes, it's possible!

SOLID Principles

You have probably heard of the SOLID principles if you work with object-oriented programming. But applying these principles in the front-end? This may seem strange at first glance, but it makes perfect sense when we think about modern components.

The Single Responsibility Principle (S of SOLID), for example, is very applicable: a component should have a single reason to change. If you have a component that both fetches data and renders it, while also managing state, it might be time to divide those responsibilities.

Frameworks: Villains or Good Guys?

Ah, frameworks! React, Angular, Vue... Did they come to make our lives easier or to complicate everything? The answer, as always in programming, is: it depends!

Modern frameworks have brought important concepts, such as componentization, which greatly help in organizing code. But it is true that they also add their own layer of complexity.

The question is: the complexity of frameworks usually comes to solve real problems we face in larger applications. The trick is knowing when this additional complexity is justified. For a simple website with a few pages? It might be an overkill. For a complex web application used by millions of people? That’s a different story.

Performance: Just because you can, doesn’t mean you should

An important point when we talk about front-end architecture is performance. Just because we have more powerful computers and faster connections doesn’t mean we can waste resources.

Do you remember that website that makes your laptop want to take off? Well, there’s probably some JavaScript running wild over there. A good front-end architecture also cares about optimizing resource usage.

And it’s not just for your computer, no. Think about the users who will access your site from a mobile phone with an unstable 3G connection. Every kilobyte counts! That’s where techniques like lazy loading, code splitting, and asset optimization come in.

The Human Factor in Architecture

One aspect of architecture that is sometimes forgotten is the human factor. We are not just talking about code, but about how teams are organized.

The architecture of your system often reflects the structure of your organization. It’s the famous "Conway's Law." If you have very separate teams, you will probably end up with a more modular system. If you have a single cohesive team, you might end up with a monolith.

There is no absolute right or wrong here. The important thing is that the chosen architecture makes sense for the reality of your team and your project.

Trade-offs: The Magic Word of Architecture

In the end, architecture is about making choices. Every decision you make brings advantages and disadvantages. Want to use micro front-ends? Great for scalability, but adds complexity in integration. Prefer a front-end monolith? Simpler at first, but it can become difficult to maintain as it grows.

The secret is to understand your context well, the needs of your project and your team. There is no silver bullet in architecture. What works for Facebook may be a nightmare for your task list app.

And remember: architecture is not something you define once and forget. It is a continuous process of evaluation and adaptation. What works today may not be ideal tomorrow as your project evolves.

The Maturity of Front-end

Front-end development has grown a lot in recent years. Frameworks like React, Angular, and Vue.js have brought new possibilities and challenges. React, for example, calls itself a library, giving you the freedom to organize your code as you wish. Angular, on the other hand, is more opinionated, providing a more complete and structured ecosystem.

And do you know what’s even more interesting? This evolution continues to happen. Concepts like Server Components, Islands Architecture, and Resumability are changing the way we think about front-end architecture.

In the end, it’s important to remember that architecture is not about following rigid rules or using the latest technologies. It’s about creating solutions that solve real problems, are sustainable in the long term, and facilitate teamwork. And yes, sometimes the best code is the one that solves the current problem in the simplest way possible, even if it doesn’t follow all the latest standards.

After all, as they say: the best code is the one that works and that other people can understand and maintain. And if you are just starting out, don’t be overwhelmed by all these concepts. Start with the basics, understand the fundamentals well, and gradually incorporate more advanced concepts as your application and your team grow.

Anyway, folks, we hope this journey through the world of front-end architecture has been enlightening. The next time someone says that front-end is just about "making a pretty website," you’ll know: there’s so much more under the hood! Keep studying, experimenting, and above all, building amazing things. The future of the web is in your hands!


Latest related articles