Angular, React and the Value of Transitions
Whether a framework switch becomes a move or a rebuild depends on how cheaply two worlds can run side by side for months.
Angular, React and the Value of Transitions
A decision like “we are rewriting the frontend from scratch” does not do its damage right away but with a delay of about nine months. By then, two half products exist: an old AngularJS frontend, officially frozen, that still gets hotfixes all the time because paying users work with it – and a new one that feels about sixty percent done and has no release date, because “everything has to be finished first.” From then on, every business requirement is negotiated twice: once for the old world, once for the one that has not shipped yet. The team quietly splits into keepers and rebuilders, and both groups slow down.
I have now seen this course of events up close more than once, most recently in a product team with an AngularJS application that had grown over years. The mistake was never the target framework. It was a word missing from the decision: transition. In practice, a framework switch is not a rewrite but a state in which two worlds run in parallel for months. And whether this state turns out cheap or ruinous has surprisingly little to do with Angular or React.
The driver: AngularJS gets an expiration date#
Since the end of January, what had long been looming is official: AngularJS 1.x gets one last development cycle with version 1.7 and then moves into a three-year long-term support phase. From then on, there will only be security fixes and adjustments to new browser and jQuery versions, no more features. For teams with large 1.x applications, this is the moment when “at some point we have to get out of there” becomes a date.
Accordingly, many migration decisions are being made right now. Whether the target is called Angular 5 (Angular 6 is due in the coming months) or React 16 – I find the direction secondary. What is interesting is how the decision is phrased. “We are rewriting” is a project with an end date, and that end date almost always slips. “We run both worlds in parallel and move piece by piece,” on the other hand, is a state you can steer, measure and stop if necessary.
Rewrite or move#
That brings me to the actual thesis: the quality of a frontend architecture does not show in how modern its framework is, but in how cheap it would be to run two frameworks in parallel. If you have kept domain logic framework-free – plain modules, services without DI magic, state outside the components – you move. If you have poured calculations, validations and API calls into controllers, directives and components, you rewrite, whether you want to or not. The framework was then never a frame but the load-bearing structure of the entire building, and you do not swap load-bearing structures while the building is in use.
In the product team I mentioned, we did not migrate anything at all for two weeks before the first migration step. We pulled domain logic out of directives and moved it into simple ES modules, with the existing AngularJS services as a thin shell on top. Only after that was it even possible to decide which part would be a move and which part had to remain a rebuild. The distinction was sobering in its concreteness: whatever got by without an angular.module import could be moved like a piece of furniture. Everything else had to be assessed individually.
Hybrid operation with ngUpgrade#
For the direction from AngularJS to Angular, running in parallel is not a trick but a documented, officially supported mode. The UpgradeModule starts the old application inside the new one:
// app/app.module.ts — Angular 5 hosting AngularJS 1.6
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UpgradeModule } from '@angular/upgrade/static';
@NgModule({
imports: [BrowserModule, UpgradeModule],
})
export class AppModule {
constructor(private upgrade: UpgradeModule) {}
ngDoBootstrap() {
// AngularJS boots inside Angular, not the other way around
this.upgrade.bootstrap(document.documentElement, ['orderApp'], { strictDi: true });
}
}
From this point on, both frameworks run in the same DOM tree. New Angular components can be passed down into old templates:
// expose a new Angular component to old AngularJS templates
import { downgradeComponent } from '@angular/upgrade/static';
import { InvoiceFooterComponent } from './invoice-footer.component';
angular
.module('orderApp')
.directive('invoiceFooter', downgradeComponent({ component: InvoiceFooterComponent }));
Conversely, old services can be passed up into the new dependency injection. That sounds convenient, and that is exactly where the danger lies: if you use the bridge too often, you intertwine both worlds more tightly than they were before the migration. Two change detection mechanisms run at the same time: AngularJS’s digest cycle and Angular’s zone-based detection. Every event that crosses the boundary is translated into both worlds, and bugs at this seam are unpleasant to track down. Hybrid operation is a tool for getting through a transition period – not for extending it. It needs an end that someone says out loud.
Route by route: the strangler approach#
The idea behind it is much older than any of the frameworks involved. Back in 2004, Martin Fowler described how new systems grow around old ones like a strangler fig around its host tree – the Strangler Application. Translated to a frontend, that means: a thin shell owns the router and decides per route which world renders. There is now even a small framework of its own for exactly this pattern, single-spa, but at its core, routing discipline is enough.
flowchart LR B[Browser] --> S[Router shell] S --> O["/orders<br/>AngularJS 1.6"] S --> I["/invoices<br/>Angular 5"] S --> R["/reports<br/>Angular 5"] O -. "moves over<br/>route by route" .-> I
Splitting along routes has three properties that I have not found in this combination in any other migration split:
- Every step can be rolled back individually – switching a route back is a router entry, not a rollback drama.
- Progress is measurable and visible to everyone: this many routes old, this many new.
- The boundary is about the domain, not the technology. “Invoices have moved” is something the product owner understands too; “the directive layer has been ported” is something nobody outside the team understands.
The big-bang alternative has none of these properties. It only has a deadline, and deadlines do not defend themselves.
Incidentally, the route split also changes the team dynamics. Instead of a keepers group and a rebuilders group, there is one team that runs the same application and rebuilds it route by route along the way. Every completed step lands in production and gets put through its paces by real users – feedback arrives after weeks, not after a year and a half. That is unspectacular, but that is exactly where its value lies.
Domain logic that survives the move#
The cheapest thing to move is code that knows nothing about any framework. In the application I mentioned, the invoice total logic was such a candidate – previously spread across a directive and two controllers, afterward a single module:
// billing/invoice-totals.js — plain ES module, no framework imports
export function calculateInvoiceTotal(items, taxRate) {
const net = items.reduce((sum, item) => sum + item.price * item.quantity, 0);
const tax = net * taxRate;
return { net, tax, gross: net + tax };
}
The old world uses the module through an AngularJS component:
// old world: AngularJS 1.6 component wrapping the plain module
import { calculateInvoiceTotal } from '../billing/invoice-totals';
angular.module('orderApp').component('invoiceFooter', {
bindings: { items: '<', taxRate: '<' },
controller: function InvoiceFooterController() {
this.$onChanges = () => {
this.totals = calculateInvoiceTotal(this.items, this.taxRate);
};
},
template: '<strong>{{ $ctrl.totals.gross | currency }}</strong>',
});
The new world – deliberately React 16 here, to show that the direction does not matter – uses the same module unchanged:
// new world: React 16 component, same module untouched
import { calculateInvoiceTotal } from '../billing/invoice-totals';
export function InvoiceFooter({ items, taxRate }) {
const totals = calculateInvoiceTotal(items, taxRate);
return <strong>{totals.gross.toFixed(2)} €</strong>;
}
The direction of dependencies is the actual rule here, not the module format:
flowchart TD OLD["AngularJS components<br/>old world"] --> CORE["Domain logic modules<br/>without framework import"] NEW["React or Angular components<br/>new world"] --> CORE CORE -. "forbidden:<br/>core knows no UI" .-> OLD
More can be kept framework-free than you might think at first glance: calculations and validations as pure functions, API access behind a module of its own instead of $http or HttpClient directly in components, application state in a store of its own. Redux is a good example here, because it is deliberately built to be framework-neutral – react-redux and ng-redux are just thin bindings. A Redux store including its reducers moves from AngularJS to React without a single changed line. What remains are thin components that take data and emit events. Those are exactly what then get rewritten – and that is the part of the work that goes quickly and carries little risk.
Custom Elements as a neutral bridge#
If you are not migrating from AngularJS to Angular but, say, have to reconcile Angular and React in the same product, you have no UpgradeModule. The most neutral bridge emerging here is the web platform itself: Custom Elements. A component registered in the DOM as <invoice-footer> can be rendered by any world – React simply writes the tag into JSX, AngularJS into a template, a server-rendered CMS into its HTML stream.
With Angular Elements, an official labs project has been announced for exactly this, which is supposed to become tangible with Angular 6: packaging Angular components as Custom Elements, without an adapter per target framework. I find the direction remarkable, because it pulls the bridge out of the framework dispute and puts it on a browser standard. You have to stay honest about the browser situation: Custom Elements v1 run natively in Chrome and Safari; elsewhere, polyfills are still needed for now. For internal applications, that is usually acceptable; for public sites, it needs to be weighed.
The honest price: two bundles#
Running in parallel is not free, and I do not believe in downplaying that. If you ship AngularJS and Angular – or Angular and React – at the same time, you send two framework runtimes over the wire. Depending on the combination, that quickly adds up to 150 kilobytes of additional compressed JavaScript before the first line of your own code is loaded. On top of that come two rendering models in the same browser tab, duplicate date and number formatting, and in the worst case duplicate HTTP layers with separate caches.
The freshly released webpack 4 at least makes the bundle side easier to handle: splitChunks replaces the old CommonsChunkPlugin and automatically pulls shared modules – such as the framework-free domain logic packages – into shared chunks. But tools only ease the price; they do not remove it. That is why an honest transition includes a budget: a measured upper limit for the total weight, a date after which the old bundle may no longer grow and a target quarter in which it disappears. Running in parallel is a loan. You may take it out – you should just know the interest rate and write down the repayment plan.
What I take away from this#
The LTS announcement for AngularJS has just handed many teams a deadline, and deadlines tempt you into heroic decisions. The big-bang rewrite is one such heroic decision: it feels good on the day of the decision and, nine months later, like two half products. The transition is the unspectacular alternative – hybrid operation where it exists, a strangler split along the routes, Custom Elements as a bridge, an honestly quantified load weight.
But what really occupies me about the topic is looking back at your own architecture. Running in parallel is a test bench: it reveals how much of the application really belongs to us and how much to the framework. Domain logic in plain modules, state outside the components, API access behind boundaries of your own – that does not only pay off during a migration, but during a migration the bill comes due mercilessly. And since, after this switch, the next one will come at some point, I write the new world right away so that it makes the transition after next cheap. That may be the most useful question to ask of any frontend design, with no migration pressure at all: How expensive would it be to start a second world next to this application?
Further reading#
- Angular documentation: Upgrading from AngularJS: https://angular.io/guide/upgrade
- Martin Fowler: StranglerApplication (2004): https://martinfowler.com/bliki/StranglerApplication.html
- Angular blog: Stable AngularJS and Long Term Support: https://blog.angular.io/stable-angularjs-and-long-term-support-7e077635ee9c
- single-spa: https://single-spa.js.org/
Revised and translated from German with the help of AI.