Any way to persist/cache the WordPress data in Frontity should the back end go down?

I had a client ask if it was possible for the front end to persist (still load and contain data) if the back end were to go down. I told him that between Vercel’s configuration (does stale-while-revalidate help?) and obviously if someone had loaded the Frontity site and the WordPress went down, the data would persist for the remainder of the session.

Are there things currently possible to do improve the persistence of the Frontity website?

Is this of interest to the development team? This would add a huge “pro” for using Frontity’s decoupled setup.

In broad strokes there are a couple of approaches that you could take with it :slight_smile:

First, mind that frontity does already partly work offline! For example, go to mars.frontity.org, click on the “Nature” tab, switch your wifi off and then click on “Home” (or click the browser’s back button). You’ll notice that the front page still shows up. This is because by default frontity will not try to fetch new data for a post/category/taxonomy, etc. that was already loaded but rather load it from an internal cache. So this data will always be available until a user closes the tab or refreshes the page. But I’m guessing that this is not good enough :sweat_smile:

Now, if you are looking for something very simple, you could hack it together by saving the content in localStorage after every fetch. And then checking if the user is online or not with navigator.onLine and if they are offline, get the content from the localStorage instead. I think this approach is not very scalable though.

If you need something more powerful, we are going to eventually support creating PWAs and adding service workers, but that is still in the planning phase - it would require the ability to extend frontity’s webpack configuration. We are discussing this feature in: Progressive Web App

1 Like

Apart from the stale-while-revalidate and the other options that @mmczaplinski mentioned, you can also use cache on top of Frontity/WordPress that will ensure that new users will get the HTML and JS/CSS assets if the origin server goes down, like the Always Online feature of Cloudflare: https://www.cloudflare.com/always-online/ :slight_smile:

He there, I ended up making a FetchHelper that takes a path, and acts as a middleman for a requests. It auto-stores tmp.json files and loads them into state if desired (app’s data service goes down). Pretty easy to get something like that working. LocalStorage would work, but json files are more stable.

1 Like

That’s really cool @brandonleejennings, thanks for sharing :slightly_smiling_face: