Skip to content

Commit cc94101

Browse files
authored
Fixed react SSR
Prevented the code from running on the server by verifying that `document` is defined.
1 parent 15eb2ba commit cc94101

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

src/helpers/find.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
var basicSelectors = {
2-
'body': document.body,
3-
'window': window,
4-
'document': document
5-
};
1+
var basicSelectors;
2+
if (typeof document !== 'undefined') {
3+
baseSelectors.body = document.body;
4+
baseSelectors.window = window;
5+
baseSelectors.document = document;
6+
}
67

78
var matchesMethodName = (() => {
8-
const body = document.body;
9-
return typeof(body.matches) === 'function' ? 'matches' :
10-
typeof(body.webkitMatchesSelector) === 'function' ? 'webkitMatchesSelector': //webkit
11-
typeof(body.mozMatchesSelector) === 'function' ? 'mozMatchesSelector': //mozilla
12-
typeof(body.msMatchesSelector) === 'function' ? 'msMatchesSelector': //ie
13-
typeof(body.oMatchesSelector) === 'function' ? 'oMatchesSelector': //old opera
14-
null
9+
if (typeof document !== 'undefined') {
10+
const body = document.body;
11+
return typeof(body.matches) === 'function' ? 'matches' :
12+
typeof(body.webkitMatchesSelector) === 'function' ? 'webkitMatchesSelector': //webkit
13+
typeof(body.mozMatchesSelector) === 'function' ? 'mozMatchesSelector': //mozilla
14+
typeof(body.msMatchesSelector) === 'function' ? 'msMatchesSelector': //ie
15+
typeof(body.oMatchesSelector) === 'function' ? 'oMatchesSelector': //old opera
16+
null
17+
}
1518
})();
1619

1720
export default function find(selector, el) {

0 commit comments

Comments
 (0)