w3c / ServiceWorker

Service Workers
https://w3c.github.io/ServiceWorker/
Other
3.63k stars 313 forks source link

should document.open() inherit the controller? #1313

Open wanderview opened 6 years ago

wanderview commented 6 years ago

What should happen if the current window is controlled by a service worker and script then calls document.open()? Should the service worker controller by inherited or not?

Per the html spec a new window should be created for document.open(). AFAIK, though, only firefox implements this and there is some discussion around aligning with the behavior where the window is preserved. @annevk has been looking at this recently.

Even with that in mind, though, should the controller be preserved or cleared?

I wrote a demo here:

https://sw-document-open.glitch.me/

The iframe shows its controller after the document.open(). You can also open the console to see if the "dynamic.js" script load inserted by the document.write() is intercepted.

Results are:

What is the desired behavior? My gut feeling is that it should probably be inherited similar to what we do for about:blank iframes, etc.

annevk commented 6 years ago

Aside: is the controller associated with a document or a Window object?

I'd argue for preserving it. The less we reset during document.open() the better.

wanderview commented 6 years ago

Aside: is the controller associated with a document or a Window object?

No. The service worker controller is part of the environment:

https://html.spec.whatwg.org/#concept-environment-active-service-worker

Edit: The "no" is to the question about document. Its part of the window since its associated with the environment (I believe).

annevk commented 6 years ago

Okay, note that the global never changes with initial about:blank, but the document does. And with document.open() the document never changes and ideally it also never changes the global (but currently does in the specification and Firefox).

wanderview commented 6 years ago

Right. We have a related issue to the controller inheritance for about:blank when the final load is not controlled:

https://github.com/w3c/ServiceWorker/issues/1232

I don't think it impact this, but wanted to mention it as a current issue with inheritance.

futurist commented 6 years ago

From a developer's point of view, I believe most of developer want to keep current environment if they wrote document.open, and want to patch something instead of recreate for all.

At lease, all the global vars/functions are preserved, and the code may depend on the inherit behavior if they depend on some features in the global env.

For me, the global env should include both window & service worker, but I don't read the specs deeply, maybe mismatched with that.