Calling document.write()
is generally a bad idea. Calling it
during page load forces the browser to pause rendering while waiting for a
script to execute. Even worse, calling after the page loads will cause the
entire page to be replaced with whatever is written. This means
`document.write()` is inherently bad since it cannot be executed
asynchronously. Indeed, attempting to call
document.write()
from a module
script results in
a warning in DevTools: Failed to execute 'write' on 'Document': It isn't
possible to write into a document from an asynchronously-loaded external
script unless it is explicitly opened.
Interestingly, Partytown does not override document.write()
by default
but we can enable async execution by implementing it ourselves. Note that this works
the same with and without Partytown.
Now to demonstrate intercepting post-load document.write()
calls in a script that has been made asynchronous: