question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[@theme-ui/match-media] Breaks gatsby builds due to undefined window

See original GitHub issue

Using @theme-ui/match-media’s useResponsiveValue hook in Gatsby causes the build to fail, due to its use of window.matchMedia.

I’d normally add a typeof window === 'undefined' check, but am not allowed to conditionally call a hook. Is there an alternative userland solution for this that I’m not thinking of?

I could add the check to the hook - is returning breakpoints[0] a sensible default?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
joestrouth1commented, Nov 18, 2019

@dburles The docs say:

By adding a @jest-environment docblock at the top of the file, you can specify another environment to be used for all tests in that file

I could not find a way to set the test environment for that test only.

Moved the test to its own file ssr.js and set the environment but @testing-library/react’s render function relies on document. None of the options it accepts helped. Tried to test the functions themselves outside of a rendered component, but the Rules of Hooks didn’t like that.

Surely someone else has needed to simulate the absence of window so I’ll keep looking for a solution.

0reactions
dburlescommented, Nov 15, 2019

@joestrouth1 PR Looks good. Try set the test environment to node for that test https://jestjs.io/docs/en/configuration.html#testenvironment-string

Read more comments on GitHub >

github_iconTop Results From Across the Web

'window is not defined' error from build files #25298 - GitHub
During server-side rendering, the window is undefined and is probably why you're running across this error on build. Issue #309 references this ...
Read more >
Gatsby build fails because window is undefined in a React hook
According to Gatsby docs window is not available during the build process. I've tried to if (typeof window !== 'undefined') condition to the ......
Read more >
Gatsby - the window is not defined error, what and how to fix it?
gatsby build : It is the environment to build the application to produce deployable artifacts. You can even run the app locally from...
Read more >
Troubleshooting Common Errors - Gatsby
Error: ReferenceError: window is not defined when running gatsby build; Build problems from Field 'browser' doesn't contain a valid alias configuration ...
Read more >
Window is not defined in gatsby build : r/gatsbyjs - Reddit
EDIT: Problem resolved. It was occurring due to an npm package named "react-video-modal" as I have to use some different version. This is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found