admin管理员组

文章数量:1317906

I'm trying to register a service worker from an iframe that is created dynamically like this:

var iframe = document.createElement('iframe');
var html = '
<head><script src="/script.js"></script></head>
<body>Iframe</body>
';
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();

script.js contains the code that registers a service worker (texbook example from google's documentation).

This throws an error saying "Failed to register a ServiceWorker: The document is in an invalid state."

I tried having the service worker registration code in the 'load' event handler of the iframe and adding a timeout to account for the service worker registration code potentially being called at a bad time without any success.

In the source code of Chromium I found the following code which is where I think the error is ing from:

if (!m_provider) {
        resolver->reject(DOMException::create(InvalidStateError, "Failed to register a ServiceWorker: The document is in an invalid state."));
        return promise;
    }

What does "The document is in an invalid state" mean in this context? What is m_provider and why is it that it's not available?

I'm trying to register a service worker from an iframe that is created dynamically like this:

var iframe = document.createElement('iframe');
var html = '
<head><script src="/script.js"></script></head>
<body>Iframe</body>
';
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();

script.js contains the code that registers a service worker (texbook example from google's documentation).

This throws an error saying "Failed to register a ServiceWorker: The document is in an invalid state."

I tried having the service worker registration code in the 'load' event handler of the iframe and adding a timeout to account for the service worker registration code potentially being called at a bad time without any success.

In the source code of Chromium I found the following code which is where I think the error is ing from:

if (!m_provider) {
        resolver->reject(DOMException::create(InvalidStateError, "Failed to register a ServiceWorker: The document is in an invalid state."));
        return promise;
    }

What does "The document is in an invalid state" mean in this context? What is m_provider and why is it that it's not available?

Share Improve this question edited Jul 24, 2020 at 15:26 PotatoProgrammer asked Jul 22, 2020 at 15:02 PotatoProgrammerPotatoProgrammer 1641 gold badge1 silver badge7 bronze badges 3
  • Were you using HTTPS or localhost? – Nikhil Pathania Commented Jul 23, 2020 at 19:02
  • I'm using localhost over http – PotatoProgrammer Commented Jul 24, 2020 at 15:25
  • possibly related: stackoverflow./q/67698176/11107541 – starball Commented Jun 29, 2023 at 2:38
Add a ment  | 

1 Answer 1

Reset to default 4

This looks to be caused by a bug in Chromium: https://bugs.chromium/p/chromium/issues/detail?id=1102209

Ideally, the dynamically-created frame would inherit the services workers registered on the parent, eliminating the need to write in the iframe's contentWindow at all.

本文标签: