Immutable Web Apps

Rate this content
Bookmark
Immutable web apps are designed to be built once and deployed multiple times. They use environment variables outside the bundle, ensuring consistent asset versions across different environments, which maximizes caching and reduces load times. Managing dependencies is crucial; tools like Webpack and Rollup help in recognizing UMDs, reducing bundle sizes. HiMyNameIs and Orchard CLI automate the namespace mapping and dependency loading order, respectively. The index page is the focal point for changes, dictating static asset versions. Arborist aids in building dependency trees for npm installs, ensuring proper script tag generation.

From Author:

Resolving dependencies when they are all bundled together is easy. Resolving dependencies when they are in being loaded via script tags is much more challenging. The goal of this talk is to explain how Meltwater handles dependency resolution when building native Web Component based applications that rely on packages published by many different teams.

This talk has been presented at JSNation 2022, check out the latest edition of this JavaScript Conference.

FAQ

Immutable web apps are applications built with the philosophy of building once and deploying multiple times. They aim to have their environment variables configured outside the bundle to ensure that each deployment is immutable, meaning once a version is built, it never changes. This allows for consistent testing across environments and maximizes asset caching.

Dependencies become problematic when there are too many being used by different applications, leading to duplicated JavaScript and large bundle sizes. This can slow down application performance and complicate updates and maintenance.

Immutable web apps deploy assets to a URL that includes the version number. This approach ensures that assets are consistent across different environments and are maximally cached, reducing load times and bandwidth usage.

UMDs, or Universal Module Definitions, allow developers to package code in a way that it can be used as a module in different environments. In web apps, they help manage dependencies by allowing them to be loaded globally without bundling them into the main codebase, thus reducing bundle size and duplication.

Tools like Webpack and Rollup are used to manage external dependencies by configuring them to recognize UMDs. Additionally, tools like HiMyNameIs help in generating namespace mappings, and Orchard CLI organizes the loading order of dependencies based on a dependency tree.

HiMyNameIs automates the mapping of module names to global namespaces, reducing maintenance overhead. Orchard CLI, on the other hand, uses dependency trees to determine the correct loading order of scripts, ensuring that dependencies are loaded efficiently and correctly.

By using the same assets in both staging and production environments, immutable web apps ensure that any configuration changes are the only differences between the two. This consistency helps in reducing bugs and issues that might arise due to environment discrepancies.

Andy Desmarais
Andy Desmarais
20 min
20 Jun, 2022

Comments

Sign in or register to post your comment.

Video Transcription

Available in Español: Aplicaciones Web Inmutables

1. Introduction to Immutable Web Apps

Short description:

Today we're going to talk about immutable web apps and dependencies. We had a problem with dependencies at Meltwater, so we needed to find a way to unbundle them and share them effectively. Immutable web apps allow us to build once and deploy many times, with environment variables outside the bundle. This ensures that the assets can be maximally cached, resulting in faster loading times. It also provides easy version tracking and rollbacks.

How's it going JS Nation? Today we're going to talk about immutable web apps and dependencies. First, I'm Andy Damaris, you can find me on pretty much all the social medias at Teradox and my website is teradox.tech.

What we're going to cover today is, we're going to start with the problem. There was a problem that we were having at Meltwater and then we'll move on to immutable web apps, what they are and why we're using them, dependencies without bundling them, referencing those dependencies and then last we're going to talk about how we or those dependencies successfully.

So, what's the problem? Well, the problem was dependencies. We had a lot of them and they were being created by internal teams for libraries that we could use on a regular basis for things like authentication or companies lookups or users lookups and each of these things was beginning to compound the amount of duplicated JavaScript that was being bundled together with all of the different applications we were shipping. So, we needed to figure out a way to unbundle these dependencies and allow them to be shared more effectively between the many applications that were using them.

So, immutable web apps were an important part of that journey, so we're going to cover those first. The basic philosophy of immutable web apps is that you want to build them once and deploy them many times. There are some really specific fundamentals that we need to accomplish in order to be an immutable web application. The first one is all of our environment variables need to be outside of the bundle. This allows our bundles to be built one time per version and be immutable for that specific version. They'll never change after the first time they're built. It also means that we need to deploy them to a URL where the version that we just built is included as a part of that URL. So we want that fully qualified URL to have our version number in it somewhere.

Now, what benefits does this really buy us? There's a bunch. The first is that when we're testing in staging and we're testing in production, they're using the exact same assets. The only difference will be the configuration that's changed between the two. This means that all of our assets can also be maximally cached. They can be cached for a full year in fact, which is the current browser maximum. There's a huge benefit to our customers for that. They only ever need to round-trip for that specific version of that specific asset once. And after that, it's on their local disk cache, saving huge amounts of time for secondary and tertiary loads of the site. We never have to worry about them having to go back to origin constantly for these large assets at times. The other things that get included with this are you always know which versions are deployed because your index.html page makes it very plain. Look at your script tag. What version's in the URL? That's the version you're dealing with. It also means that rollbacks are now really trivial. We're just flipping back from one specific version of a set of assets to another script tag. Another specific version of a set of assets.

2. Immutable Web Apps and Dependencies

Short description:

If you're a consumer, chances are good you already have the previous version of assets in your disk cache. The index page is the focal point for immediate changes. We break things down into three thought processes: static web hosting, delivering static assets using script tags, and APIs. To hit the API, we use a configuration block in the script tag. We have a lot of dependencies, so we need tooling to fix the problem. The first tool is UMDs, the Universal Module Definition style of bundling.

And if you're a consumer already, chances are good you already have that previous version of assets in your disk cache ready to go. So you won't even have to pay download costs again for it.

So we've talked about all of our assets but what about the index page? Well, the index page is the one part of an immutable web app that you don't cache. It's our focal point for where all of our changes are allowed to show up immediately.

So we like to break things down into three different thought processes. You don't have to break it down this way. It's just an exercise in a way to think of things. So the first piece is just our static web hosting. It hosts our index HTML page. That index HTML page is pretty static. There's not a whole lot of dynamic nature to it. And it dictates the versions of all of the static assets that we're going to deliver. Those static assets are then delivered using script tags. And those script tags use the fully versioned URL that we were talking about. In this case, version 1.2.3 to be able to deliver those assets.

Then we also have our APIs, and our APIs could be on a different server, could be on the same server. But we dictate which API we're going to hit by that configuration block you're seeing in the script tag. That configuration block tells our application where we should go to hit that API and what the route should be, removing that environment variable from our JavaScript code.

So I've lost over immutable web apps pretty quickly here. There's some more nuance, a lot more detail that you could dig into. And if that's something that's interesting to you, please check out immutablewebapps.org.

So now let's dig back into the main crux of what we're talking about, which is dependencies. We have a lot of them. They're being used by a lot of different applications. So how do we fix that problem? Well, we're going to need some tooling to do this successfully. The first little bit of tooling we're going to use is UMDs. They're a specific type of bundle that we'll dig into. And then we're going to talk about HiMyNameIs, a tool for helping us discover dependency names. And then we'll talk about Orchard, which is really our tool for ordering those dependencies. So the first tool I want to discuss is UMDs, the Universal Module Definition style of bundling.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

pnpm – a Fast, Disk Space Efficient Package Manager for JavaScript
DevOps.js Conf 2022DevOps.js Conf 2022
31 min
pnpm – a Fast, Disk Space Efficient Package Manager for JavaScript
pnpm is a fast and efficient package manager that gained popularity in 2021 and is used by big tech companies like Microsoft and TikTok. It has a unique isolated node module structure that prevents package conflicts and ensures each project only has access to its own dependencies. pnpm also offers superior monorepo support with its node module structure. It solves the disk space usage issue by using a content addressable storage, reducing disk space consumption. pnpm is incredibly fast due to its installation process and deterministic node module structure. It also allows file linking using hardlinks instead of symlinks.
Yarn 4 - Modern Package Management
JSNation 2022JSNation 2022
28 min
Yarn 4 - Modern Package Management
Top Content
Yarn is a package manager that focuses on stability, performance, and security. It offers unique features like plug and play installation, support for nonmodules, and the exec protocol. Yarn is committed to being a good citizen in the open-source community and contributes to fixing dependencies. It is part of the Node.js Loader's working group and advocates for Corepack. Yarn is still experimental but is improving its user experience and security features. Contributions are welcome, and switching to Yarn can improve performance in large projects.
The Good, The Bad, and The Web Components
JSNation 2023JSNation 2023
29 min
The Good, The Bad, and The Web Components
Top Content
Web Components are a piece of reusable UI enabled by web standards and built into the web platform. They offer the potential for faster component initialization and less library overhead. Web Components can be created from scratch and utilized with existing component libraries. Shadow DOM and Declarative Shadow DOM provide benefits such as scoped CSS and server-rendered components. The tradeoff between not repeating oneself and achieving full server-side rendering support is discussed. User experience is deemed more important than developer experience.
It's Time to De-Fragment the Web
React Day Berlin 2022React Day Berlin 2022
34 min
It's Time to De-Fragment the Web
Top Content
Today's Talk introduces Mitosis, an open source project that solves the problem of building framework agnostic components. It supports JSX and Svelte syntax and outputs human-readable code for various web frameworks. Mitosis is already being used by enterprise customers and can be easily integrated with React, Svelte, and other frameworks. It saves time, allows for easy handling of framework version migrations, and enables efficient unit and integration testing.
Web Components, Lit and Santa 🎅
JSNation Live 2021JSNation Live 2021
28 min
Web Components, Lit and Santa 🎅
Web Components and the Lit library are introduced, highlighting their ability to create custom elements and replicate built-in components with different functionality. The use of semantic HTML and the benefits of web components in development are emphasized. The features of Lit, such as data binding and rendering, are discussed. The Santa Tracker is showcased as an example of web components being used in educational games. The compatibility of web components with other frameworks and their versatility in creating small widgets or large applications are highlighted.
Authoring HTML in a JavaScript World
React Summit US 2023React Summit US 2023
21 min
Authoring HTML in a JavaScript World
Watch video: Authoring HTML in a JavaScript World
This Talk by Tony Alicia focuses on authoring HTML in a JavaScript world. The speaker challenges developers to change their approach to building React components by starting with HTML first. By authoring HTML in a semantic way, readability and maintainability can be improved. Well-authored HTML provides better understanding of content and improves accessibility. It also has performance benefits by reducing DOM size. Investing time in HTML can save time and make applications more future-proof.

Workshops on related topic

Web Components in Action
JSNation Live 2021JSNation Live 2021
184 min
Web Components in Action
Workshop
Joren Broekema
Alex Korzhikov
2 authors
The workshop extends JavaScript programming language knowledge, overviews general software design patterns. It is focused on Web Components standards and technologies built on top of them, like Lit-HTML and Lit-Element. We also practice writing Web Components both with native JavaScript and using Lit-Element. In the end we overview key tooling to develop an application - open-wc.