Anyone who has been working in web development for a while will be familiar with this now-legendary meme from xkcd. It illustrates just how fragile the technical infrastructure of a website or app can be.

That’s why it’s important to use fallbacks to build a robust front end, either to quietly and discreetly avoid the error or to communicate it to our users.

The issue of robustness is so key that it is actually one of the four principles of the Web Content Accessibility Guidelines (WCAG)! So how can we provide a modern front end without excluding users who use browsers and hardware that are only partially compatible or not compatible at all?

The answer lies in implementing multiple safety nets in HTML, CSS, and JavaScript, so that we are prepared for the worst-case scenario and can provide our people with the best possible experience.

The main difference between the use of fallbacks and progressive enhancement lies in the directions in which the two approaches aim.

While progressive enhancement is about automatically making certain features available as long as they are supported (forward), fallbacks are about what happens when something is not available or accessible (backward).

For example, we can offer a shiny latest CSS feature that makes interacting with our website even more enjoyable. But if this feature isn’t available in some browsers, no one will notice, because a version that’s supported by older browsers is available instead. Progressive enhancement in action!

Another example: If JavaScript cannot be loaded for a critical application, we provide a fallback that either offers basic functionality or, at the very least, displays a message explaining what the problem is.

While one doesn’t necessarily exclude the other, their approaches are quite different. My personal recommendation is to always work with progressive enhancement as a guiding principle and to consider fallbacks whenever possible.

To evaluate newly introduced features intended for production, we must first determine the extent to which a feature is supported.

Since 2023, the Interop project has been in place, in which major browser vendors collaborate to release latest features for the web platform more or less simultaneously. This serves as a general overview.

Related to this is the Baseline project, which shows, for specific features, the extent to which they are supported and how long they have been available. However, it is extremely important to note that it does not track browser support for accessibility features built into the web platform!

Another vital resource for checking compatibility is Can I Use. Here, you can get a quick overview of browser support for various features.

It has been revealed that let’s start with the foundation of the World Wide Web: HTML. Modern tech stacks place a strong emphasis on client-side rendered JavaScript, which can offer many benefits to people in more complex applications. However, there can be just as many reasons why JavaScript might fail to load, so we need a strategy to address this.

In a recent development, in the event of a total failure, we should redirect users to an appropriate error page where they receive information about what went wrong and what not only they, but also we, can do to prevent this in the future. If you want to know how to write error messages that actually help users rather than frustrate them, check out Amy Hupe’s amazing article.

Because this kind of error feedback is so essential, the static site generator Astro, for example, offers the ability to add custom 404 and 500 error pages. You could discuss other possible error codes with your back-end team to implement additional safeguards.

But what if we also want to make individual sections or components more robust in case JavaScript isn’t available? For this, we can use the  tag, which, according to Can I Use, has excellent browser support. Here’s a highly simplified example of a fictional dashboard component:.

If JavaScript is not available, we will still display the components, but explain in simple and clear terms that they cannot be loaded at the moment and why that is the case.

We give users the option to reload that specific component so they can resolve the matter themselves. We also offer them the option to contact us directly via email if the problem persists.

Here, you could add a pre-written text to the  tag, which could include, for example, an internal error code specific to that component in the subject line and have a corresponding body text. This way, they would only need to click the button and could send the email directly using their email program of choice. Support would then have all the necessary information to resolve the problem without needing to ask for additional details.

No matter what your use case is, this pattern can be universally adapted and extended, and ensures that it works even if everything else goes wrong.

It has been revealed that let’s stick with our fictional dashboard component. Say we want to use the CSS anchor() function to display the data, which is fairly new and has solid, but not great, browser support.

Recent findings suggest that since this is a feature that can include multiple attributes, we need to check whether the ones we want to use are actually available. This is exactly where the @supports CSS rule comes into play! Here, it’s recommended to use the and operator: It should only be used if all attributes are available. If not, the more robust CSS with better browser support is used.

Here’s another fictional, simplified example:.

This approach also serves as documentation: Once the feature has been around for a while and is more well-established, you can look directly for the @supports rule in your code base and check whether it is still necessary, along with its fallback.

Javascript offers us the ability to make our front end even more resilient with minimal effort. To do this, we can use the if…else statement to check whether a specific feature is available. For example, if we want to use the relatively upcoming Navigation API, we could check for it as follows:.

If we use a component-based approach, where HTML, CSS, and JavaScript are all contained in a single file, we can also use the conditional ternary operator to display this component or parts of it, or do nothing at all.

Our fictional dashboard component relies on data from the back end, and it only makes sense to display it once the data has successfully reached the front end. So, for example, we could do the following:.

Depending on the use case, it makes sense to display a fallback or even nothing at all. You could also define the conditions more specifically, depending on how robust you want it to be.

My experience so far at various companies has been that the topics of fallbacks and robustness haven’t played as prominent a role as they really should have.

This is surprising, because as we’ve learned, it doesn’t take much to implement various safety nets on the front end, as long as you think ahead about what (shouldn’t) happen if something goes wrong.

HTML, CSS, and JavaScript provide us with specific elements and functions to ship robust code and thus avoid upsetting our users and, in the worst case, causing them to turn away from us.

→ Read original article