{"id":6089,"date":"2019-06-07T02:30:15","date_gmt":"2019-06-07T02:30:15","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=6089"},"modified":"2019-06-07T02:30:15","modified_gmt":"2019-06-07T02:30:15","slug":"gatsby-define-global-document-variable","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/gatsby-define-global-document-variable\/","title":{"rendered":"Gatsby define global document variable"},"content":{"rendered":"<p>If you reference &#8220;document&#8221; in a Gatsby site, it will fail on build.<\/p>\n<p>E.g.:<\/p>\n<pre lang=\"javascript\">\n       <VideoRecorder\n                    height={document.documentElement.clientHeight}\n                    width={document.documentElement.clientWidth}\n                 \/>\n<\/pre>\n<p>Error:<\/p>\n<pre>\n  WebpackError: TypeError: Cannot read property 'documentElement' of undefined\n<\/pre>\n<p>To fix this, you need to return quickly, so that the code doesn&#8217;t get to the unused reference in the mind of the compiler.<\/p>\n<p>E.g.:<\/p>\n<pre lang=\"javascript\">\n        if (!document) { return <div><\/div>; }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Fixing a compilation error in webpack<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[246,302,455,592],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/6089"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=6089"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/6089\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=6089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=6089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=6089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}