[Website] Make all iframes controlled by the service worker #2923
+834
−140
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Explores making all iframes controlled by the Playground service worker.
Iframes created as about:blank / srcdoc / data / blob are not controlled by this
service worker. This means that all network calls initiated by these iframes are
sent directly to the network. This means Gutenberg cannot load any CSS files,
TInyMCE can't load media images, etc.
Only iframes created with
srcpointing to a URL already controlled by this service workerare themselves controlled.
Explored solution
We inject a
iframes-trap.jsscript into every HTML page to override a set of DOMmethods used to create iframes. Whenever an src/srcdoc attribute is set on an iframe,
we intercept that and:
iframes-trap.jsis also loaded and executed inside the iframeto cover any nested iframes.
As a result, every same-origin iframe is forced onto a real navigation that the SW can control,
inside editors like TinyMCE) go through our handler
so all fetches (including
without per-product patches. This replaces the former Gutenberg-only shim.
Downsides
When a DOM reference to an element inside an iframe is grabbed early on, rewriting the HTML inside that iframe invalidates those reference. I think it breaks "bold", "italic", etc buttons in TinyMCE at the moment (but it doesn't break the "Add Media" feature).
This is a deal-breaker in the current PR. I think we can make it work without destroying the DOM nodes already in the iframe. TinyMCE seems to be doing
iframe.contentWindow.contentDocument.write( newHTML )anddocument.write()makes a controlled iframe uncontrolled again. We'll need to wrap every part of the process and replace thedocument.write()logic with something closer toinnerHTMLor a redirection toloader.html?initialHTML={markup}.References
Fixes #2919
Fixes #42
cc @akirk @brandonpayton @ellatrix @draganescu