When does chrome.offscreen.createDocument() resolve?

408 views
Skip to first unread message

Gaurang Tandon

unread,
Sep 13, 2024, 7:10:12 AMSep 13
to Chromium Extensions
Hi! I'm trying to understand when does the chrome.offscreen.createDocument() promise resolve, precisely?

For example: if our offscreen document has a script of type=module, does the promise resolve earlier than the script has been executed?

Similarly, if we append an iframe inside the offscreen document, and the iframe has its own scripts with type=module, then does the createDocument() promise resolve earlier than the iframe is loaded/its script has been executed?

A precise answer is important, because we send a message to the offscreen document as soon as the createDocument promise is resolved. If its script has not been parsed when we do that, our message would be lost, because the offscreen document wouldn't have setup the listeners.

Best,
Gaurang

woxxom

unread,
Sep 13, 2024, 10:06:25 AMSep 13
to Chromium Extensions, Gaurang Tandon
Use the native web platform messaging for better reliability, performance, and more supported data types. The offscreen script explicitly calls navigator.serviceWorker.startMessages() when ready, example: https://stackoverflow.com/a/77427098

Gaurang Tandon

unread,
Sep 13, 2024, 11:14:16 AMSep 13
to Chromium Extensions, woxxom, Gaurang Tandon
Thanks for your response woxxom. Just so that I understand correctly, can you please clarify what you mean by better reliability and better performance? It's good to know that this API supports more data types, although we're not using them for now. Are you suggesting that the createDocument() promise can resolve before <script>s on the DOM have finished processing?

woxxom

unread,
Sep 13, 2024, 1:06:34 PMSep 13
to Chromium Extensions, Gaurang Tandon, woxxom
It's better and faster because you control when the messaging cycle starts explicitly and it doesn't broadcast the message into all running chrome-extension:// contexts (if you embed a web_accessible_resources iframe there can be hundreds of them), it doesn't serialize the data into a string internally, it can send binary types directly.

I simply suggest not to rely on an inferior technology: in Chromium/Chrome the messaging and chrome.storage are inferior to the web platform APIs.

Jackie Han

unread,
Sep 16, 2024, 9:26:06 PMSep 16
to woxxom, Chromium Extensions, Gaurang Tandon
This is a good question!

From the source code, I found this comment:

  // Add a reference so that we can respond to the extension once the
  // offscreen document finishes its initial load.
  // Balanced in either `OnBrowserContextShutdown()` or
  // `SendResponseToExtension()`.


But I don't understand what 'initial load' means. Maybe 'document_start' or 'document_end'.


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/6831aed9-8229-4f95-81c2-71d8b6a78b63n%40chromium.org.

woxxom

unread,
Sep 17, 2024, 1:48:33 AMSep 17
to Chromium Extensions, Jackie Han, Chromium Extensions, Gaurang Tandon, woxxom
Logically, I'd expect it to happen after the entire static module tree finishes running in the main document as well as in the embedded frames that were specified in the initial html, i.e. not added dynamically.
Reply all
Reply to author
Forward
0 new messages