Check out a free preview of the full Progressive Web Applications and Offline course:
The "Background Sync Strategies" Lesson is part of the full, Progressive Web Applications and Offline course featured in this preview video. Here's what you'd learn in this lesson:

Steve reviews best practices and techniques for working with background sync.

Get Unlimited Access Now

Transcript from the "Background Sync Strategies" Lesson

>> Steve Kinney: All right, some considerations. You can't pass state then, right? You can't say, hey, schedule this and take this object. Unfortunately, that's not how it works, we don't pass state back and forth between our two threads. That is a world of sadness waiting to happen. But you can use any of those durable storage mediums, like Mike just showed IndexedDB, that's a great option for keeping dynamic data.

[00:00:25] When your Service Worker attempts to sync, it can read that data out of IndexedDB. Like I said before, if you register multiple sync events it will do the most recent one, overwrite the previous. This is probably a good thing so you don't accidentally favor, it's like six server request favor in the same ones.

[00:00:42] But you have to think about this, right? It could be like, hey, I wanna read through all the ones that haven't been synchronized. We said before, just like caching, just like intercepting network requests, just like push notifications, there's no one-size-fits-all strategy for implementing sync. It all depends on the use case of your application.

[00:01:08] So there might be some cases where you would probably wouldn't, like again again, we said the idea of a credit card. You wouldn't, in those cases, want to just say, yeah I got this, right, cuz then you have to provide a user interface for cancelling it if they change their mind, stuff along those lines.

[00:01:25] But there are certain things that you can queue up. Cool, but I think one my favorites is any kind of Twitter application. I can't write a tweet usually when I'm offline, even on a native app. But I can go through and favor a bunch of tweets. And if I'm offline, the application doesn't say a word to me.

[00:01:45] It says yeah, favorative, red heart into it. Later on, when this native app connects to server it will synchronize all of those favorited tweets. For me as a user, I don't even notice. Let's take a look at a quick example of background sync and one way that you could implement it.

[00:02:08] And again, this is a very naive example. To actually get it working in more than a few minutes and more than a byte-size amount of code, there are little things that we'll have to consider along the way. But let's actually see it in action.
>> Speaker 2: Is it possible to differentiate that you have an unresponsive server, or against that you really do not have network?

>> Steve Kinney: So there are ways for the navigator to tell if it's online or not. So you do have that option. Again, the trickier to solve is which our Jay calls LieFi which is a inconsistent Internet connection. Those are the trickier problems to solve. No Internet connection, somewhat easy problem to solve.

[00:02:52] Bad Internet connection is much harder.