# Single Page Applications

A single-page application is a web application or website that interacts with the user by dynamically rewriting the current web page with new data from the web server, instead of the default method of a web browser loading entire new pages.

![](https://1172597814-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJ6Mj8gFbz9Ji6QL6Zi%2Fuploads%2F4PhveUraSsAbvCA63CLl%2Ffigure1.jpg?alt=media\&token=c2fb8c16-090a-431c-bf78-999251e054f5)

### How to build SPAs

SPAs can be built with plain JS or with a JS framework. In fact, the first SPAs were built with plain JS, before JS frameworks were created.

However, using a JS framework can make it easier to build SPAs by providing features such as:

* **Routing:** SPAs need to be able to update the content of the page without reloading the entire page. JS frameworks provide routing features that make this easy to do.
* **State management:** SPAs need to be able to track and manage the state of the application, such as the current page, the user's login status, and any data that the user has entered. JS frameworks provide state management features that make this easy to do.
* **Templating:** SPAs need to be able to render HTML templates to dynamically update the content of the page. JS frameworks provide templating features that make this easy to do.

In addition, JS frameworks can provide a number of other features that can be useful for building SPAs, such as two-way data binding, component-based development, and dependency injection.

\
**Two-way data binding** is a programming technique that allows data to flow from the model to the view and vice versa. This means that changes to the model are reflected in the view, and changes to the view are reflected in the model.

Two-way data binding is often used in web development to create dynamic and interactive user interfaces. For example, a two-way data binding can be used to bind a text input field to a model property. When the user types in the text input field, the model property is updated. When the model property is updated, the text input field is updated to reflect the new value.

**Component-based development (CBD)** is a software development approach that breaks down a software application into smaller, reusable components. These components can then be assembled together to create the complete application.

CBD is a popular approach for developing SPAs because it can help to improve the modularity, reusability, and maintainability of the application.

In an SPA, components are typically self-contained pieces of code that define a specific UI element or functionality. For example, a component could be used to render a list of items, a form, or a navigation menu.

Components can be reused throughout the SPA, which can help to reduce code duplication and improve the overall maintainability of the application.

**Dependency injection (DI)** is a software design pattern that allows objects to receive other objects that they need, rather than creating them themselves. This makes code more modular, reusable, and testable.

In DI, a class that needs another object is called a **client**, and the object that is needed is called a **dependency**. The client does not create the dependency itself; instead, it receives it from an **injector**. The injector is responsible for creating and providing dependencies to clients.

### **When to use an SPA:**

* Your web application is highly interactive and requires real-time updates.
* You want to provide a fluid and responsive user experience.
* You are willing to invest in the development complexity of an SPA.
* You do not need to worry about SEO as much.

### **When to use an MPA:**

* Your web application is static or has a relatively small amount of interactivity.
* You need to optimize your web application for SEO.
* You are concerned about security.
* You have a limited budget or resources for development.

Here are some examples of SPAs:

* Gmail
* Facebook
* Twitter
* Google Docs
* Netflix

Here are some examples of MPAs:

* Wikipedia
* Amazon.com
* CNN.com
* BBC.co.uk
* Apple.com

### Security

Multipage websites (MPAs) are generally considered to be more secure than single-page applications.

* **SPAs rely on JavaScript to render and update the page.** This means that attackers can potentially inject malicious JavaScript code into the page and execute it on the user's device. Multipage websites, on the other hand, do not rely on JavaScript for rendering, so attackers are less likely to be able to inject malicious code.
* **SPAs typically have fewer entry points for attackers.** All of the user's interactions with an SPA are mediated through JavaScript, which means that attackers only have one entry point to exploit. Multipage websites, on the other hand, have multiple entry points, such as links to individual pages and forms. This makes it more difficult for attackers to find and exploit vulnerabilities.
* **SPAs are more difficult to audit for security vulnerabilities.** Because SPAs are dynamic and constantly changing, it can be difficult for security professionals to keep up with all of the potential vulnerabilities. Multipage websites, on the other hand, are more static and easier to audit.
