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 error with null document.body #29

Open
AndreyMiloserdov opened this issue Aug 23, 2017 · 4 comments
Open

fix error with null document.body #29

AndreyMiloserdov opened this issue Aug 23, 2017 · 4 comments

Comments

@AndreyMiloserdov
Copy link

In some libraries used dom-helpers appear error:

matches.js:23 Uncaught TypeError: Cannot read property 'matches' of null
    at matches.js:23
    at Object.exports.__esModule (matches.js:28)
    at __webpack_require__

just replace code in dom-helpers/query/matches.js from

  if (_inDOM2.default) {
    (function () {
      var body = document.body;
      var nativeMatch = body.matches || body.matchesSelector || body.webkitMatchesSelector || body.mozMatchesSelector || body.msMatchesSelector;

      matches = nativeMatch ? function (node, selector) {
        return nativeMatch.call(node, selector);
      } : ie8MatchesSelector;
    })();
  }

to

document.addEventListener('DOMContentLoaded', function() {
  if (_inDOM2.default) {
    (function () {
      var body = document.body;
      var nativeMatch = body.matches || body.matchesSelector || body.webkitMatchesSelector || body.mozMatchesSelector || body.msMatchesSelector;

      matches = nativeMatch ? function (node, selector) {
        return nativeMatch.call(node, selector);
      } : ie8MatchesSelector;
    })();
  }
});

That fix problems for null document.body

@vdh
Copy link
Contributor

vdh commented Oct 18, 2017

I tried applying that fix but it failed the Karma tests.

This whole matches functionality is written synchronously around the idea that DOMContentLoaded has already fired. I can't think of a good way to fix it without rewriting the whole thing to properly wait asynchronously for DOMContentLoaded.

@jquense
Copy link
Member

jquense commented Oct 18, 2017

Yes you should either load scripts in the body per web best practices or do the content loaded handler at the root of your app

@vdh
Copy link
Contributor

vdh commented Oct 19, 2017

@jquense I know about best practices, but they shouldn't then get turned into silent hard dependencies.

I already have a DOMContentLoaded listener before the root of my app, but because this async dependency is happening at require time, this matches code is executing immediately, long before any React component code that actually needs to use it is fired… :(

@jquense
Copy link
Member

jquense commented Oct 19, 2017

Yes, there is basically a dependency that scripts not execute before the body because we need an element to do tests against. You don't need to wait for content loaded, moving the script tag to below the body will work as well. I'm happy to take a PR switching all checks to something more lazy and keeps the API the same. It may be easier tho to not load the script in the HEAD

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

No branches or pull requests

3 participants