Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: "'requestAnimationFrame' called on an object that does not implement interface Window." #16606

Open
Jack-Works opened this issue Aug 29, 2019 · 6 comments

Comments

@Jack-Works
Copy link
Contributor

Do you want to request a feature or report a bug? Bug

What is the current behavior?
TypeError: "'requestAnimationFrame' called on an object that does not implement interface Window."

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

const requestAnimationFrame = window.requestAnimationFrame;

In the WebExtension content script, globalThis !== window.
So when const requestAnimationFrame = window.requestAnimationFrame, the this binding of rAF is lost.

In the after calling, rAF rebinds to the globalThis, but globalThis doesn't implements Window interface.

PoC: Install this extension in the Firefox and you will see the error reported.

try {
    const raf = window.requestAnimationFrame
    raf(() => console.log('Okay.'))
} catch (e) {
    console.warn(e)
}
{
    "name": "rAF in content script test",
    "version": "0.0.1",
    "manifest_version": 2,
    "content_scripts": [{ "js": ["/index.js"], "matches": ["<all_urls>"] }]
}

What is the expected behavior?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Firefox, in WebExtension.

Related to #16605

@joealden
Copy link

I can confirm that this is also happening for me when trying to upgrade React to 16.9.0 in a project that uses React in a browser extension content script.

@nhunzaker
Copy link
Contributor

Interesting. I think this was introduced with this change:
3f2cafe#diff-3856e885394723dea203587a10ea16b3L39-L51

@nhunzaker
Copy link
Contributor

cc @acdlite: do you think this could be a quick fix to avoid references to window? I wonder of self would work.

@Jack-Works
Copy link
Contributor Author

Just make it raf = window.raf.bind(window) everything will be okay

@gtbono
Copy link

gtbono commented Aug 30, 2019

I made this and the tests failed, did you mean anything else?

FAIL packages/shared/tests/ReactDOMFrameScheduling-test.js
● ReactDOMFrameScheduling › warns when requestAnimationFrame is not polyfilled in the browser

TypeError: Cannot read property 'bind' of undefined

  86 |   const setTimeout = window.setTimeout;
  87 |   const clearTimeout = window.clearTimeout;
> 88 |   const requestAnimationFrame = window.requestAnimationFrame.bind(window);
     |                                                            ^
  89 |   const cancelAnimationFrame = window.cancelAnimationFrame;
  90 |   const requestIdleCallback = window.requestIdleCallback;
  91 | 

@Jack-Works
Copy link
Contributor Author

Yeah... You need to bind it when it exists...
raf = window. raf ? window.raf.bind(window) : undefined

liady added a commit to liady/react that referenced this issue Sep 9, 2019
When capturing local references of requestAnimationFrame and cancelAnimationFrame - bind them to the window object.
Fixes facebook#16606
NoxHarmonium pushed a commit to agiledigital/mule-preview-browser-extension that referenced this issue Sep 21, 2019
This fix moves the responsibility of the rendering into the extension, rather than the mule-preview
plugin. An issue was uncovered  where the latest version of React (16.9.0) does not work in Firefox
browser extensions due to a change to the scheduler (facebook/react#16606)
so React was downgraded. After those fixes were implemented, the issue with rendering only working
correctly the second time the extension was triggered was fixed.

fix #72
NoxHarmonium pushed a commit to agiledigital/mule-preview-browser-extension that referenced this issue Sep 21, 2019
This fix moves the responsibility of the rendering into the extension, rather than the mule-preview
plugin. An issue was uncovered  where the latest version of React (16.9.0) does not work in Firefox
browser extensions due to a change to the scheduler (facebook/react#16606)
so React was downgraded. After those fixes were implemented, the issue with rendering only working
correctly the second time the extension was triggered was fixed.

fix #72
NoxHarmonium pushed a commit to agiledigital/mule-preview-browser-extension that referenced this issue Sep 21, 2019
This fix moves the responsibility of the rendering into the extension, rather than the mule-preview
plugin. An issue was uncovered  where the latest version of React (16.9.0) does not work in Firefox
browser extensions due to a change to the scheduler (facebook/react#16606)
so React was downgraded. After those fixes were implemented, the issue with rendering only working
correctly the second time the extension was triggered was fixed.

fix #72
spellman added a commit to roomkey/reagent that referenced this issue Mar 17, 2020
There is a problem with Reagent in content scripts in Firefox extensions where
the UI does not update when a ratom changes.

The problem is described here
NoxHarmonium@a9e898c
with Reagent 0.9.0+, though that Reagent fork also has a branch that patches
Reagent 0.8.1. The relevant branches are:
* fix-raf-binding-0.8.1
* fix-raf-binding-0.9.0-rc1
* master (with fix on 0.9.0)

Note the "related links section" of the commit message at the above link: React
issue facebook/react#16606
From what I’ve read, this only seems to affect content scripts in browser
extensions in Firefox.

I have merely applied the fix in the commit at the above link to Reagent 0.10.0.

[#170650669]
spellman added a commit to roomkey/reagent that referenced this issue Mar 17, 2020
There is a problem with Reagent in content scripts in Firefox extensions where
the UI does not update when a ratom changes.

The problem is described here
NoxHarmonium@a9e898c
with Reagent 0.9.0+, though that Reagent fork also has a branch that patches
Reagent 0.8.1. The relevant branches are:
* fix-raf-binding-0.8.1
* fix-raf-binding-0.9.0-rc1
* master (with fix on 0.9.0)

Note the "related links section" of the commit message at the above link: React
issue facebook/react#16606
From what I’ve read, this only seems to affect content scripts in browser
extensions in Firefox.

I have merely applied the fix in the commit at the above link to Reagent 0.10.0.

[#170650669]
nopol10 added a commit to nopol10/nekocap that referenced this issue Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants