WICG / background-sync

A design and spec for ServiceWorker-based background synchronization
https://wicg.github.io/background-sync/spec/
Apache License 2.0
640 stars 85 forks source link

Sync event listener is not allowed to pass data back to app #139

Open BairDev opened 7 years ago

BairDev commented 7 years ago

Stupid Question, Derived from Confusion

While playing with a Service Worker and especially with the sync feature/event, I've noticed, that the event listener for the sync event or, more precisely, the promise in the custom method returnAPromise in the event.waitUntil() line of, say

if (event.tag === 'my-sync-tag') {
    event.waitUntil(returnAPromise());
}

is not allowed to return any data (did not try it with a primitive value though). Those would appear in the callback of the promise in the app, like here:

registration.sync.register('my-sync-tag')
    .then(function(data)
        {
            // do stuff with data, probably displaying them
        });

I've found this hint here:

At the end. you cannot use postmessage to send response directly.Because it's illegal.

Even this hint is not related to the behavior of the sync feature, but to the type of data, which can be sent over some kind of messaging system (two way communication between the app and the SW).

Regarding this: You can create a workaround with a MessageChannel, which is also demonstrated by this example. but an approach like this one, in which the app tries to get the fetched data directly won't ever work as far as I know.

Now, I've looked here and there for a specification of this behavior, but did not find much. Even in this section (5.3: SyncEvent) of the spec nothing indicates, that sharing the data between the app and the SW is not allowed for synchronization tasks (in contrast to intercepting requests with the very same SW for example).

Is there any reason, why this is not specified and not explained anywhere?

toxic-johann commented 7 years ago

@BairDev I am the one who wrote that answer on stackoverflow. Actually, i just found that problem when i try to just send response through message channel. But I am kind of understand why the specification that do not write define the behavior you mentioned.

  1. Because sync event just let us know that we are online and can do sth. We can do not only just sending a request. For example, you can send multiple request or put the data in the indexedDB etc.
  2. What's more. Sync event is aim to solve upload problem in lie-fi.And in the same time, most people may close the page of our website. We can't ensure the page can get the response of sync event.

So after all, I suggest you to consider backgroundSync as a message of online in backstage.Though in specification, i define itself as method to synchronize data, but what i think is, synchronize data is not equal to send a request.

BairDev commented 7 years ago

@toxic-johann Thanks for your response. You are right, I just realized, that I was not aware of the acutal meaning of background sync. There is no data sharing between app and SW involved. When I wrote that issue I just wondered, why the two promises are not chained at all: the one in the custom function, which is called in event.waitUntil(), and the one from registration.snyc.register('my-tag'). It would be interesting to know a bit more about the technical background here.

toxic-johann commented 7 years ago

@BairDev hmmm……That's a good question. I just write the promise in sendMeesageToSw for the page to communicate with worker.So that i can wait until i get the response. The event.waitUntil() is used to extend the life time of event, in order to make the worker not to be terminated.