I built a no-code alternative to Workbox
Tags: webdev, javascript, pwa, nocode
Published: 2021-12-13 07:09:35


![Illustration of the Progressier no-code caching strategy builder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojsljj5cer2l1d2xo9wh.png)
A couple of days ago, we finally released an exciting new [Progressier](https://progressier.com?ref=devto) feature which our customers have been relentlessly requesting since we launched our beta.

With our [caching strategy builder](https://progressier.com/features/caching-strategy-builder), anyone who owns a web app can easily configure caching strategies -- with toggles and dropdown rather than complicated [regex](https://en.wikipedia.org/wiki/Regular_expression) and hard-coded [service workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API).

## Why should I care about caching strategies?

By default, browsers tend to cache resources somewhat unpredictably through the [HTTP cache](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) mechanism. You have little control over how resources are cached and updated.

Instead with the [Cache interface](https://developer.mozilla.org/en-US/docs/Web/API/Cache) available in service workers, you can define very precisely how each type of resource is fetched, cached and refreshed.

With the right strategies, you can:

1. make your app functional offline
2. improve load times
3. keep key resources fresh without having to use a [versioning trick](https://css-tricks.com/strategies-for-cache-busting-css/#query-strings)
4. decrease your server costs

Pretty cool, in theory. In practice? Not so easy. Earlier this year, Google nearly [gave every web app developer an ultimatum](https://javascript.plainenglish.io/your-pwa-is-going-to-break-in-august-2021-34982f329f40?source=friends_link&sk=73d1c45cd3f21023726d96e14f915494): make your app work offline or lose the install functionality. [As I postulated](https://javascript.plainenglish.io/your-pwa-is-going-to-break-in-august-2021-34982f329f40?source=friends_link&sk=73d1c45cd3f21023726d96e14f915494) when they abandoned that plan, making a website work offline consistently is harder than most people think.

In 2021, most developers use [Workbox](https://developers.google.com/web/tools/workbox) -- a JavaScript library that abstracts the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and [Cache API](https://developer.mozilla.org/en-US/docs/Web/API/Cache). Workbox simplifies things… but isn't quite simple enough yet to my taste. Testing strategies is a hassle. Hard-coding lists of URLs in a service worker isn't exactly future-proof. The order of your rules can lead to unexpected behaviors. And writing Regex is [a notorious pain in the ass](https://www.reddit.com/r/learnprogramming/comments/acmorf/why_most_programmers_hate_regex/).

## Caching is a minefield

[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS), [preflight requests](https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request), [opaque responses](https://intercom.help/progressier/en/articles/5740926-how-to-deal-with-opaque-responses-in-service-workers), [cache-control headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control)… these are some of the concepts that make caching hard to grasp. They're abstract and opiniated -- and truthfully most developers don't really know about them.

So the challenge was double. First, how does one build a _universal_ service worker that works with any domains and resources? And second, how does one build a user interface that doesn't have a learning curve for this _inherently complicated process_?

The key is that everyone deals with caching the same way: by copy-pasting code snippets from the [Workbox site](https://developers.google.com/web/tools/workbox) or the [Google Dev Site](https://developers.google.com/web/tools/workbox) and swapping sample values with their own variables. Progressier lets you define these variables with toggles and dropdowns instead of hard-coding them in your service worker.

![Screenshot of Progressier caching strategy builder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4r43cnp6oa0chru8daka.jpeg)
_Specify rules, choose a strategy, see matching resources, precache URLs, auto-retry them - all in one interface with inputs and toggles instead of code_

And then there are variable-looking things that in our day and age just aren't needed anymore. One of the ways Progressier simplifies the process is by shielding you from them automatically. A good example of this is caching expiration dates.

## A modern take on caching

You shouldn't have to worry about expiration dates anymore. Because caching expiration dates are no longer needed. They're an antiquated way of invalidating cached resources -- reminiscent from the time the only way to deal with caching was the unpredictable HTTP Cache interface.

With the Cache interface available in service workers, there's absolutely no need to set expiration dates anywhere. You have much better control on things now:

- You can keep resources systematically fresh by not using caching at all (Network Only)
- You can keep resources fresh but fall back to a cached version when the network isn't available (Network First)
- You can fetch resources from the cache first to reduce load times but systematically revalidate them every time (Stale-while-Revalidate)
- You can get resources from the cache and never revalidate them to reduce server costs (Cache First)

## Caching strategies in plain English

With Progressier, you simply choose one of the strategies above and select which resources to apply it to. Examples of rules:
_- "Apply Network First to all resources from cdn.whatever.com"_
_- "Apply Cache First to all Font files"_
_- "Apply Network Only to api.mydomain.com/very-important-data.json"_
_- "Apply Stale-While-Revalidate to all images hosted on my domain"_

And because Progressier is a no-code tool, you specify these rules in plain English -- just the way I did it above. That may seem frivolous. But if you have to modify them two years from now, you'll be glad not to have to decipher a bunch of regular expressions.
You can also visualize how each of the strategies intercept your resources (as rules may have overlap).

![Illustration for the caching strategy rules in Progressier](https://cdn-images-1.medium.com/max/1200/1*l0ptH67Z5lJKaizP7RgvUw.jpeg)
_A visual indicator tells you how many actual resources from your app match each of these strategies._

##And all other caching features too

Caching strategies is the key to making a web app functional offline. For a complete offline experience, there are a few other mechanisms that one must know about as well.

Here are some of the things you can configure your web app to do automatically with a few clicks in Progressier:

- Precache resources required for your app to launch while offline
- Retry failed requests later when network connectivity is lost
- Build redundancy by providing fallback URLs for important resources
- Override headers that prevent caching (especially useful for no-code builders like Bubble).
- Show an offline alert to warn users that they've lost network connectivity

## Simple is hard

Steve Jobs said "_Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains._"

Building the user interface of a caching strategy builder might be the _hardest simple thing_ I've ever had to do. And while Progressier definitely won't move mountains, it does put caching within reach of any developers -- whether they write code or use no-code builders. If this sounds interesting, you can try it for free [here](https://progressier.com?ref=devto).