Frontends Kept Simple
Overwhelmed by the sheer number of choices for your pet project's frontend? Find out how to skip the headaches and heavyweight frameworks and keep your web applications simple.
Not long ago a colleague told me a story: he had launched a side-project meaning to iterate fairly fast (yet still safely), and he picked node.js with TypeScript for the backend logic. During the day he works as a backend developer (mostly in ecosystems other than node.js/JS/TS), though in the past he had written some frontend code as well.
Then the moment came to add a UI layer – and that’s where his frustration began.
Almost none of the patterns he remembered had stayed the same. React? These days the community is excited about large end-to-end foundations such as Next.js, and even the official docs now advise choosing “one of the React-powered frameworks”. Build tools? Barely any new project relies on Webpack the way they did a few years ago.
My friend was flooded with an overwhelming amount of new terms, concepts, machinery and obscure configurations. It seemed to him that there was no easy answer to his fairly simple needs; learning a dozen additional tools merely to expose a basic frontend hardly looked like the right path! Going through the examples online felt a bit like the meme:
Image source: the classic “how to draw an owl” meme
Frustrating enough already?
Could our frontends be dead simple instead?
Image source: devhumor.com
My claim is that despite one more round of “frontend fatigue” (a term for the state of having more tools and tool combinations at hand than we can reasonably take in), this maze still has paths that lead to simple solutions.
I’ll share a few tricks that ought to make life (and coding) easier, at least during the bootstrapping phase, without first having to earn a PhD in tooling.
As a frontend/TypeScript developer myself, I frequently feel that pressure and frustration as well. And you know what – it doesn’t feel right. My aim is to lower the cognitive load – working smart beats working hard!
This blogpost sets out to look at a few ways of starting simple frontend projects – put differently, to get a working prototype without the frustration. I won’t be comparing frameworks or libraries, however. Nor will I discuss particular approaches to things like state management, styling, testing and so on.
The state of the frontend in 2024
Some tooling remains necessary to unlock certain benefits, such as TypeScript transpilation or optimizing the production bundle. Many of the most common needs, however, are now covered by solutions that require next to no configuration (or can lean heavily on native mechanisms), and in a moment I’ll demonstrate how to get “simple”, “simpler” and “dead simple” project starters.
Enter Vite
The new kid on the block – a one-stop-shop for handling source files & assets, transpiling TypeScript and hot module replacement – is now Vite, intended to replace configuration-heavy tools such as Webpack.
The obligatory meme:
Image source: xkcd.com
If you, dear reader, now suspect I’m simply swapping one standard for another, that’s a fairly reasonable doubt. Bear with me, though, as I’ll try to explain why I made this choice.
Vite is a batteries-included kind of tool that needs very little configuration right from the start; it seems to have been built to cover the most common needs with no questions asked – you concentrate on the business logic and it handles everything else.
On top of that, it ships with a collection of well-established starter projects for the most popular UI libraries in both language flavors (JavaScript and TypeScript):
Image source: vitejs.dev
Lately Vite has turned into a de-facto standard on a wide range of platforms, among them Vue, React, Svelte, Qwik and more.
StackBlitz
“Trying Vite Online” was made possible together with StackBlitz, which provides a fully-integrated online development environment: Visual Studio Code running in the browser, plus a live preview powered by WebContainers. Let’s have a look at the React example in TypeScript (react-ts):
Open the example on StackBlitz
Once more – StackBlitz gives you an environment in which development can begin right away; the code is already in place and the preview loads by itself – edit some code and watch the change take effect instantly!
You don’t need to know any npx create … incantations to bootstrap a new project, and you don’t have to clutter your local machine with yet another code directory if the selected starter project isn’t to your liking. If it does suit you, though, you’re only a few clicks from linking your GitHub account to StackBlitz and saving the code via the “Create repository” button:
hooray for GitHub!
From there, it’s up to you – keep working on StackBlitz or clone the repository to your machine!
Now let’s examine the code. As promised, Vite’s configuration is pretty simple and comes down to three elements:
- the “vite” dependency
- the vite.config.ts file (with a
react()plugin/directive) - the “vite” command (script)
Together they provide a fully working setup, ready for both development and production.
“But I don’t want React!”
Fair enough. Let’s try a “vanilla” project instead – one that doesn’t force any particular framework on you. To keep things simple, I’ll also assume JavaScript is sufficient:
Open the example on StackBlitz
The minimal configuration is even simpler than before – this time you only need:
- the “vite” dependency
- the “vite” command (script)
Note that no vite.config.js was required this time – since no particular framework or library is involved, an explicit Vite configuration isn’t necessary. It just works, using default settings suited to an ordinary web app.
As both approaches described so far rely on Vite, there’s also a build script that produces a production-ready bundle of static code & assets. The output directory can be served immediately by any HTTP server you like.
Native modules for a “dead simple” setup
For years, the only way to use the import and export statements defined by the JavaScript modules standard was to hook in some sort of code bundler, such as Webpack, Parcel, Rollup (or Vite).
That was because browsers didn’t understand the native syntax and instead needed JavaScript code to be included through one or several <script src=”...”> elements. The first option depended on a huge bundle of code – every source file plus its dependencies – “packed” into one file. The second (several script elements) divided the code into chunks but was awkward to maintain over time (mainly because the order in which scripts loaded/executed could soon become hard to reason about).
A little-known fact: for quite a while now (since ~May 2018!), every major browser has supported native modules through the <script type=”module”> tag! As a result, code can easily be divided into separate modules, values can be exported and imported, and no external tooling is needed to make it all work. Here’s an example:
Open the example on StackBlitz
Note that there’s only a single script tag pointing to the “main” JavaScript entrypoint: <script type="module" src="components/app.js></script>
From there, the code in components/app.js pulls in further code using import statements. And all that without any bundler – just good old HTML and JavaScript!
Code can now take advantage of practically all the goodies of the constantly evolving ECMAScript standard, and for a minimalist, simple setup, code bundlers can easily be left out.
The obligatory meme that sums it all up:
Image source: imgflip.com + my own sense of a good meme
A word about frameworks
Let’s step back for a moment and touch on frameworks once more. Another way to get a project that’s ready for development/production out of the box is to go with a full-blown ecosystem such as Next.js from day one. Every required piece is there immediately – the backend, the frontend, the configuration and all the “glue” logic. The template can be found on StackBlitz too.
Bear in mind, though, that this comes with tradeoffs – in my view Next.js has a rather steep learning curve. Some of my colleagues say it’s easy to get started with but hard to master.
I probably wouldn’t file it under “simple frontends”, and certainly not when the intended audience is far from dedicated to advanced frontend development. That said, it may be an excellent fit if you’re aiming for a fullstack/isomorphic solution from the outset and want to make justified use of modern frontend architecture patterns such as server-side rendering, server components and so on.
Summary
I’m convinced that simplicity is what matters most, particularly in a technology landscape that never stops changing. As some tools get replaced by others, I looked into which one offers the simplest approach while implicitly covering the most common needs of new projects. I also think tools like StackBlitz add an extra layer of smooth, welcoming developer experience: you can get hands-on with code immediately, in a sensibly configured environment, almost completely isolated from your local machine.
Along this short journey I also discovered that going back to the “roots” can pay off, since browsers have been catching up with the standards for some time now (despite the early hiccup). Lots of modern features no longer seem to need complex polyfill-transpile-build pipelines and are well supported natively.