user17981 February 2016

Access screen height and width in a service worker

I want my service worker to get the height and width of the screen.

I've tried screen.height, self.innerHeight, window.innerHeight, but it seems service workers run on a different context where these are undefined.

Is there a way to access device information like height and width within the context of a service worker?

Answers


Marco February 2016

Service Workers (and all workers in general) can't access the DOM.

I can see two ways to achieve what you want:

  1. Store those values in IndexedDB (or in a Cache) in your page and access them in the worker;
  2. Send those values from the page to the service worker using postMessage.

Obviously, you can use the first solution always, you can't use the second solution when your service worker is running and there isn't any page open.

Post Status

Asked in February 2016
Viewed 1,603 times
Voted 11
Answered 1 times

Search




Leave an answer