WICG / background-sync

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

Uncaught (in promise) TypeError: Cannot read property 'register' of undefined(…) #127

Open wklm opened 8 years ago

wklm commented 8 years ago

Hi,

I've got a problem with requesting a periodic sync. Error message is:

Uncaught (in promise) TypeError: Cannot read property 'register' of undefined(…)

and my registration object looks like this:

ServiceWorkerRegistration {installing: null, waiting: null, active: ServiceWorker, scope: "http://localhost:3000/", onupdatefound: null…}

So it has no requested field "periodicSync"

this is my code:

if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw', { scope: '/' }); navigator.serviceWorker.ready.then(function(registration) { console.log(registration) registration.periodicSync.register({ tag: 'get-latest-news', // default: '' minPeriod: 1000, // default: 0 powerState: 'avoid-draining', // default: 'auto' networkState: 'avoid-cellular' // default: 'online' }).then(function(periodicSyncReg) { console.log("success"); }, function() { console.log("failure"); }) }); }

What am I doing wrong?

jkarlin commented 8 years ago

I don't believe that periodic sync is implemented in any browser. Chrome has implemented one-shot sync, as defined in https://wicg.github.io/BackgroundSync/spec/. I believe that Mozilla is implementing one-shot as well.

wklm commented 8 years ago

@jkarlin background sync is marked as enabled in chrome 49 (https://www.chromestatus.com/feature/6170807885627392) Could you please suggest me how can I implement it in my app? Are there any good tutorials around?

jkarlin commented 8 years ago

What's implemented is the background sync specification, which currently only includes one-shot syncs.

The closest thing to a tutorial that I'm aware of is @jakearchibald 's article. There is also a demo.

wklm commented 8 years ago

@jkarlin thank you :+1: