Skip to content

HTML5, CSS3 and ECMAScript as a Baseline

In the spring of 2017, CSS Grid, Flexbox, fetch and native ES2015 carry so far that the first architecture question in the frontend deserves to be asked again: How far do I get without a framework?

≈ 10 min readSoftwareArchitecture

HTML5, CSS3 and ECMAScript as a Baseline

Baseline means something specific here: the set of HTML, CSS and JavaScript capabilities that runs in all of a project’s browsers without a single dependency. No npm install, no framework, no plugin – only what the platform ships out of the box. This set is not a fixed quantity. It depends on which browsers a project actually has to serve, and it shifts with every browser release. If you know it, you can precisely answer a question that otherwise gets answered by reflex: what exactly do I need a dependency for – and what not?

This question is especially worth asking right now, because over the last few months the baseline has moved further up than it has in a long time.

An unusual spring#

In March, Chrome 57, Firefox 52 and Safari 10.1 shipped CSS Grid Layout within a few weeks of each other. I’ve never before seen a major layout feature land practically simultaneously and without prefixes in three major browsers. Usually something like this dragged on for years – think of the three different Flexbox syntaxes that coexisted for a long time. Grid is usable in its final syntax from day one.

But Grid is only the most visible part. If you can limit your target browsers to current versions of Chrome, Firefox, Safari and Edge, here’s what you have in May 2017 without any dependency:

  • Flexbox and CSS Grid for layout, plus @supports for clean feature queries in the stylesheet
  • CSS custom properties (widely supported since last year, IE11 excepted)
  • Native ES2015: classes, arrow functions, destructuring, template literals, promises
  • async/await from ES2017 in the current versions of the evergreen browsers
  • fetch as a built-in HTTP client, since Safari 10.1 there too
  • Custom Elements v1 and Shadow DOM v1 in Chrome and Safari
  • Service workers in Chrome and Firefox – Safari and Edge are still missing here, and honesty requires saying so

Two or three years ago, every single item on this list was a reason to install a library. That’s exactly why the question matters to me again. In a code review recently, I went through a frontend that dragged along jQuery, a grid system, an AJAX helper library and a class emulator – well over 100 KB together before the first line of its own code ran. In 2013, there was a good reason for each of these dependencies. In 2017, the platform handles all four tasks itself. Nobody had ever reversed the decision, because nobody had ever looked at it as a decision.

Layout now belongs to the platform#

The example I use to play this through is deliberately ordinary: a product overview, cards in a grid, data from an HTTP interface. The classic case for which you’d otherwise install Bootstrap along with its jQuery dependency.

The grid first. With Grid, the layout is a handful of lines, and the fallback for older browsers isn’t a second codebase but a layer underneath:

/* baseline for every browser: a simple flex wrap */
.products {
  display: flex;
  flex-wrap: wrap;
}

.product-card {
  flex: 1 1 240px;
  margin: 8px;
}

/* enhancement for browsers with grid support */
@supports (display: grid) {
  .products {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    grid-gap: 16px;
  }

  .product-card {
    margin: 0;
  }
}

I find two things about this remarkable. First: repeat(auto-fill, minmax(240px, 1fr)) replaces what grid systems needed classes like col-md-4 and a cascade of media queries for over the years – without a single media query. Second: @supports turns the browser matrix into a design decision instead of a compatibility hack. IE11 and the current Edge get the Flexbox grid, modern browsers get Grid. The pages don’t look pixel-identical, and that’s fine – the idea that every browser version has to render identically has cost us a lot of unnecessary code for years.

You have to stay honest about Edge: so far it only supports the old Grid syntax with the -ms prefix, which dates back to the IE10 era. If Edge is in your target set, treat it like IE11 for Grid for now and use the fallback. That’s exactly what the @supports query is for.

Loading data without a library#

The second building block is fetching data. What used to require $.ajax or your own XHR wrapper is now handled by fetch – and with async/await, the control flow finally reads like synchronous code. How tedious the same task was three years ago with callback chains I recorded back then in a separate post; the distance between these two worlds is surprisingly short.

async function loadProducts() {
  const response = await fetch('/api/products', {
    headers: { 'Accept': 'application/json' }
  });

  // fetch resolves on HTTP errors too, so check explicitly
  if (!response.ok) {
    throw new Error('Request failed with status ' + response.status);
  }

  return response.json();
}

The comment in the code marks the pitfall I run into most often in reviews: fetch doesn’t reject the promise on a 404 or 500, only on network errors. If you come from jQuery, you expect the opposite. You have to learn quirks like this once – but in doing so you learn the platform, not a library’s opinion, and that knowledge goes stale much more slowly.

For IE11: no fetch, no promises, no async/await. If you have to support it, compile with Babel and load two small polyfills. That’s a build step I otherwise like to avoid – but it’s manageable, targeted and above all temporary. The polyfill goes away as soon as IE11 drops out of the matrix. In my experience, a framework dependency never goes away.

Components directly in the browser#

The third building block is the most interesting because it comes closest to what you’d otherwise reach for a framework for: reusable components. Custom Elements v1 have arrived in Chrome and Safari, and with them you can define the product list as your own HTML element:

class ProductList extends HTMLElement {
  async connectedCallback() {
    this.textContent = 'Loading products ...';

    try {
      const products = await loadProducts();
      this.render(products);
    } catch (error) {
      this.textContent = 'Products are currently unavailable.';
    }
  }

  render(products) {
    this.innerHTML = '<div class="products"></div>';
    const container = this.querySelector('.products');

    products.forEach(product => {
      const card = document.createElement('article');
      card.className = 'product-card';
      card.textContent = product.name;
      container.appendChild(card);
    });
  }
}

customElements.define('product-list', ProductList);

The markup then simply contains <product-list></product-list>, and the browser calls the lifecycle methods itself. No virtual DOM, no data binding, no render loop – a component of this size needs none of that. Firefox and Edge still need the polyfill from the webcomponents.org project for this, and it isn’t free: it adds noticeable weight and has to be tested across the browser matrix. If you rely on Custom Elements in production today, you should know these costs and not just the Chrome demo.

A note on the side: native ES modules in the browser are also just around the corner – Safari 10.1 already supports <script type="module">, the others have it behind flags. For the moment, bundling stays in the build; but this gap is closing too.

The order of checks#

These building blocks result in an order I now go through for every frontend requirement – not as dogma, but as a path of checks that turns dependencies into justified decisions instead of reflexes:

flowchart TD
  A[Requirement in the frontend] --> B{Can the platform do it?<br/>caniuse against the target browsers}
  B -- yes, in all target browsers --> C[Use the baseline<br/>no dependency]
  B -- yes, with exceptions --> D{Fallback via @supports<br/>or polyfill acceptable?}
  D -- yes --> E[Retrofit selectively<br/>polyfill is temporary]
  D -- no --> F[Library for exactly<br/>this one gap]
  B -- no --> F
  F --> G{Do several gaps<br/>form a pattern together?}
  G -- yes --> H[Framework – with a clear purpose]
  G -- no --> C

The decisive point is the last node. A framework isn’t forbidden in this picture – it just comes at the end of the check instead of at the beginning. If you’ve gone through the path, you can name the purpose precisely: “We need React because we have twenty views with shared state and data binding by hand is no longer manageable” is an architecture decision. “We’re using React because that’s how it’s done today” is not.

The honest other side#

So that this doesn’t sound like a plea for frontend asceticism: there are good reasons to keep installing dependencies in 2017, and they belong on the table.

The browser matrix is the biggest one. If you build an internal system for a large company where IE11 is set by group policy, you have a different baseline than someone running a website for a tech-savvy audience. For the IE11 world, the platform approach means: Babel, several polyfills, two layout paths, twice the testing. Beyond a certain point, a library that abstracts away these differences is simply the cheaper solution – and that’s exactly why jQuery is still legitimate this year:

  • when IE versions are in the target set whose quirks jQuery has reliably smoothed out for years
  • when an existing plugin ecosystem – date pickers, tables, upload widgets – is in production use and a rebuild creates no business value
  • when a team maintains a large, long-grown jQuery codebase and a technology switch would bring more risk than benefit

The polyfill math has to be honest too. A fetch polyfill is small; the Web Components polyfill isn’t. If you retrofit five platform features, you may end up loading more kilobytes than with a compact library that brings everything along – and you carry the testing burden for every combination yourself. The baseline question is not “the platform at any price,” but: what runs everywhere anyway, and what does the rest really cost?

And finally: for large, long-lived applications with lots of shared state, the platform doesn’t solve the core problem. HTML, CSS and ECMAScript say nothing about how state and presentation stay consistent when thirty components show the same data. This problem is real, and frameworks rightly address it. But it’s a specific problem – not every website has it, and a product overview with three views almost certainly doesn’t.

A collection of material as a by-product#

While sorting out this topic, I created a public collection of material in April: introduction-html5-css3-ecma on GitHub. It gathers the platform’s inventory in sections – HTML5 markup and the APIs from geolocation through canvas, web workers and WebSockets to server-sent events, plus CSS3 selectors and animations, the JavaScript fundamentals around prototypes and closures, and ES2015+ syntax up to promises, async/await and generators. None of it is exotic; for me, the value lies in having it side by side. Only when the inventory lies written down in front of you do you see how much of it currently exists twice in your own project – once natively in the browser and once as a dependency in the bundle.

Bottom line#

In 2017, the baseline of HTML5, CSS3 and current ECMAScript is no longer a makeshift compromise but a solid foundation: Grid and Flexbox for layout, fetch and async/await for data, Custom Elements for components on a small scale. If you start with it, you cut three costs at once – fewer dependencies that need maintaining and updating, less build complexity, and a lower barrier to entry for everyone new to the project, who only needs to know web standards at first.

Above all, though, the baseline changes the quality of the framework decision. It no longer happens at the start of the project out of habit, but later, deliberately, with a purpose you can name. Sometimes the answer is still React or Angular – and that’s perfectly fine, because now it’s clear which problem it solves and which one the platform has long solved itself. More often than I would have thought two years ago, though, the answer is: you don’t need one at all.

Further reading#

Revised and translated from German with the help of AI.