Skip to content

[JS] Remove the global document variable #1473

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

Closed
oveddan opened this issue May 31, 2015 · 19 comments
Closed

[JS] Remove the global document variable #1473

oveddan opened this issue May 31, 2015 · 19 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@oveddan
Copy link
Contributor

oveddan commented May 31, 2015

Having a global document variable without implementing all of its methods causes interop issues with other libraries that expect the global document to have those methods, if it exists.

This occurs, for example, when trying to require jquery, and it sees a global document and attempts to call createElement on it. Use case and explaination of issue can be seen here.

According to @vjeux

There is no good reason for react-native to have a global document variable with just a single key createElement: null. We need to remove document altogether, it was added as a workaround very early in the project and is not needed anymore.

Therefore it seems the best solution would be to not attach the react-native specific document to global.

@brentvatne brentvatne changed the title Improvement: Remove the global document variable [JS] Remove the global document variable May 31, 2015
@vjeux
Copy link
Contributor

vjeux commented May 31, 2015

Can you send a pull request to remove the line that adds it? It's in InitializeJavascript js file

@oveddan
Copy link
Contributor Author

oveddan commented May 31, 2015

@vjeux sure I will look into this.

@sophiebits
Copy link
Contributor

(We'll probably want to set it to undefined.)

@oveddan
Copy link
Contributor Author

oveddan commented Jun 8, 2015

@vjeux @spicyj I dug around for a while - we don't set GLOBAL or window.document anywhere, even in InitializeJavaScriptAppEngine.js
All that we do with global.document is set createElement to null

I tried running window.document = undefined; but it doesn't work. This is because window.document is a read method.

If you try in the chrome or safari console to run the code:

window.document = undefined;
window.document;

You will see that window.document still returns the document.

The better solution would not to override document.createElement to be null.

@sophiebits
Copy link
Contributor

We really don't want a document though for React Native. Annoying that it's non-writable, non-configurable.

@ide
Copy link
Contributor

ide commented Jun 8, 2015

@jaredly I heard you were thinking about a new executor. There are a handful of issues like this one that could be closed by moving away from Chrome =)

@oveddan
Copy link
Contributor Author

oveddan commented Jun 8, 2015

@sophiebits
Copy link
Contributor

@oveddan I understand. Still annoying. :)

@vjeux
Copy link
Contributor

vjeux commented Jun 8, 2015

Maybe we could run in a web worker instead of the main js thread when debugging in chrome. This way we won't have document.

Not sure how the debugging experience is though

@oveddan
Copy link
Contributor Author

oveddan commented Jun 8, 2015

@vjeux After your comment - I just confirmed that:
When using chrome debugger, window.document is not undefined, and you get an error when trying to set it.
Same goes for safari debugger.
When not debugging, window.document is already undefined.

If we wanted to have it run in the debugger from a web worker, it would need to be done for safari too.

I feel like this would adversely effect the debugging experience. Also it would probably have issues should people want to use karma, or any other browser-based tester, to run tests.

@jaredly
Copy link
Contributor

jaredly commented Jun 8, 2015

Can't we just change Environment.canUseDOM to make it configurable? e.g. there be some global flag to disable it even in the presence of document etc.
As I understand it, that's the only reason we're messing with document. Are there other reasons that we want it not to be defined in chrome/safari?

@sophiebits
Copy link
Contributor

@jaredly It's not defined in the JSC executor, so we don't want it available when debugging either, for consistency. React isn't the only one who sniffs this. As one example, jQuery behaves differently depending on whether the window and document globals are available.

@jaredly
Copy link
Contributor

jaredly commented Jun 8, 2015

Cool. I'd say let's try a webworker then -- debugging in chrome is pretty well supported (don't know about safari)

@oveddan
Copy link
Contributor Author

oveddan commented Jun 11, 2015

@spicyj good point - for consistency it would be best for document not to exist. @vjeux If you're not working on it yet, I'll take a stab at making it run in the web worker and see what the debugging experience is like.

oveddan added a commit to oveddan/react-native that referenced this issue Jun 15, 2015
JSC executer environment, where there is no global.document,
make the chrome debugger run the javascript inside a web worker.

This fixes facebook#1473

Some issues:
- Ideally the websocket could be open inside the worker and communicated
  directly with it, but one of the messages requires access to
  localStorage, which is not accessable from within a worker.  So the
  socket is still loaded from the webpage itself, and messages that the
  worker should handle are forwarded to it.
- The worker does not have access to the global window object.  In a
  worker, the global scope is a DedicatedWorkerGlobalScope and can be
  accessed via `self`
@oveddan
Copy link
Contributor Author

oveddan commented Jun 15, 2015

@vjeux @spicyj Take a look at the PR and let me know what you think. Debugging doesn't seem to be adversely affected in chrome.

@oveddan
Copy link
Contributor Author

oveddan commented Jun 15, 2015

@spicyj On second look - the React tab is now gone from chrome when running the code in a worker, and using the debugger. Any suggestion on how to get it back?

@jaredly
Copy link
Contributor

jaredly commented Jun 16, 2015

Yeah... That's probably going to be difficult with the current devtools.
We're looking at rearchitecting things though, in which case this would be
easier
On Mon, Jun 15, 2015 at 2:10 PM Dan Oved [email protected] wrote:

@spicyj https://github.com/spicyj On second look - the React tab is now
gone from chrome when running the code in a worker, and using the debugger.
Any suggestion on how to get it back?


Reply to this email directly or view it on GitHub
#1473 (comment)
.

@browniefed
Copy link
Contributor

Just as a side note if someone stumbles upon this issue. Debugging socket.io with chrome dev tools open is apparently impossible impossible at the moment. It'll throw up the document.createElement is undefined even though a successful connection can be made. It works perfectly fine and connects with chrome debugging off.

@ccheever
Copy link
Contributor

seems like #1632 is in limbo. Is this still an issue? If so should that PR go through or is a different approach needed?

@ghost ghost closed this as completed in 8db35d4 Sep 24, 2015
MattFoley pushed a commit to skillz/react-native that referenced this issue Nov 9, 2015
…e the global document.

Summary: To make the chrome debugger environment consisten with the JSC executer environment,
where there is no `window.document`, make the chrome debugger run the javascript inside a web worker.

This fixes facebook#1473
Closes facebook#1632

Reviewed By: @martinbigio

Differential Revision: D2471710

Pulled By: @vjeux
@facebook facebook locked as resolved and limited conversation to collaborators Jul 22, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
8 participants