gauntface / web-push-book

Web Push Book
https://web-push-book.gauntface.com
226 stars 55 forks source link

Opening scoped (subdirectory) page on notification click #118

Open piotrekwitkowski opened 5 years ago

piotrekwitkowski commented 5 years ago

cc @gauntface

Type of Issue

Is this an issue with the demos, the content or is this a content request?

Content Request / Content Issue

Details....

If a bug with the demo What was the expected outcome vs actual outcome and what browsers are affect, please include versions.

I am opening (or refocusing) page on notification click as described here.

If the service worker is registered at the domain root, the Jeff Posnick's method does the trick:

const urlToOpen = new URL('/page', self.location.origin).href;` 

However, if the service worker is scoped, the new URL will omit the subdirectory completly, because its second argument is base

new URL('/page', 'https://domain.com/sub/').href; => https://domain.com/page

It can be solved by gluing self.registration.scope already available in the service worker and page:

self.registration.scope + 'page' => https://domain.com/sub/page

Can such information be useful in the context of the web push book? Should I issue a PR?

Thanks for great tutorials @gauntface!