Re: [ServiceWorker] Specify .focus() and .openWindow() behaviour (#602)

Is this the problem we're trying to solve?

> When a user navigates to my site and my SW is active, I want it to open in my standalone home-screen mode, like what apps do

If so, being able to call `client.focus()` within a navigation fetch does not seem fit for purpose. Here's what I think you're proposing:

```js
self.onfetch = event => {
  if (event.request.desination == 'document') {
    event.respondWith(
      clients.matchAll().then(clients => {
        const client = clients.find(c => c !== event.client);

        if (clients[0]) {
          clients[0].focus()
          clients[0].navigate(event.request.url);
          return new TypeError();
        }

        return fetch(event.request);
      })
    );
  }
};
```

Problems with the above:

* We can't tell a homescreen client apart from a normal browser client
* I can't launch the homescreen version, it has to already be open
* I need to be able to tell which type of navigation focus will actually work with (not programmatic ones)
* If the user clicks on a link from a native app, we've already potentially opened a new Chrome window, to then bounce to another window
* The original window is now showing a "network error" page

A lot of these problems come down to trying to fix this too late in the process.

Here's how it works on Android:

1. The app defines a set of urls it controls
1. When a link within that url space is clicked, an intent window is shown asking the user what they want to open the link in
1. User selects the app
1. The app handles the "navigation", which can involve bouncing back to Chrome if it cannot handle that particular url

I don't think we need to solve 4, this is the web. To solve the others we need something declarative to indicate a set of urls are handled by this homescreened site. `scope` within the manifest seems ideal for this. 

Our default behaviour here could be single window, as in always use an existing homescreened client for the navigation, or always open a new client.

Being able to overwrite this default seems sensible, as part of a `navigation` event within the service worker. There you could direct the navigation at a given client, or signal that a new one should be created. You could perhaps even reject the navigation, causing it to open in plain old Chrome.

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/602#issuecomment-136707365

Received on Tuesday, 1 September 2015 12:55:47 UTC