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

Fix for non-browser environments #102

Merged
merged 3 commits into from Jun 9, 2017
Merged

Fix for non-browser environments #102

merged 3 commits into from Jun 9, 2017

Conversation

oskarrough
Copy link
Contributor

Hi, this fixes a bug where document is undefined in non-browser environment.

I saw aacee47 but needed this extra check as well.

Hi, this fixes a bug where `document` is undefined in non-browser environment.

I saw aacee47 but needed this extra check as well.
@sindresorhus
Copy link
Owner

We already define document at the top of the file, so I don't really see what this is fixing.

@oskarrough
Copy link
Contributor Author

In the case of Ember.js server-side rendering (ember fastboot), window is defined but window.document is not. How about this line instead?

var document = typeof window.document !== 'undefined' ? window.document : {};

@sindresorhus
Copy link
Owner

Sure

@kevva
Copy link

kevva commented Jun 8, 2017

Wouldn't you need to check window too?

@oskarrough
Copy link
Contributor Author

I think you're right. How about this?

var hasDocument = !window || typeof window.document !== 'undefined';
var document = hasDocument ? window.document : {};

@@ -1,7 +1,7 @@
(function () {
'use strict';

var document = typeof window === 'undefined' ? {} : window.document;
var document = typeof window.document !== 'undefined' ? window.document : {};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be:

var document = window && typeof window.document !== 'undefined' ? window.document : {};

Copy link
Contributor Author

@oskarrough oskarrough Jun 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tak!

@sindresorhus sindresorhus merged commit 8b90ae6 into sindresorhus:gh-pages Jun 9, 2017
@el4v
Copy link

el4v commented Jun 13, 2017

this fix broke server environments
/frontend/node_modules/screenfull/dist/screenfull.js:9
var document = window && typeof window.document !== 'undefined' ? window.document : {};
^

ReferenceError: window is not defined
at /frontend/node_modules/screenfull/dist/screenfull.js:9:17

@Tazer
Copy link

Tazer commented Jun 13, 2017

Same here :(

@oskarrough
Copy link
Contributor Author

oskarrough commented Jun 14, 2017

@EgorLyutov @Tazer can you verify whether this check works for you?

var document = typeof window !== 'undefined' && typeof window.document !== 'undefined' ? window.document : {};

Checking typeof window instead of just window should avoid the reference error, I hope.

@Tazer
Copy link

Tazer commented Jun 14, 2017

@oskarrough yes when changing that it works much better. So it works for me.

sindresorhus pushed a commit that referenced this pull request Jun 14, 2017
Unfortunately the last change broke some server environments (not the ember fastboot one, I tested). This should make it good again.

#102 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants