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

WebpackError: ReferenceError: document is not defined - IE check #14868

Closed
jaysella opened this issue Jun 18, 2019 · 1 comment
Closed

WebpackError: ReferenceError: document is not defined - IE check #14868

jaysella opened this issue Jun 18, 2019 · 1 comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@jaysella
Copy link
Contributor

Description

I recently added const isIE = /*@cc_on!@*/ false || !!document.documentMode as a check to see if a user is using Internet Explorer and conditionally add a message asking them to use a modern browser (Chrome/Firefox). While this works as expected using gatsby develop, it fails using gatsby build stating:

error Building static HTML failed
WebpackError: ReferenceError: document is not defined

Steps to reproduce

Running gatsby build within project folder after declaring const isIE = /*@cc_on!@*/ false || !!document.documentMode and running a check on the const {isIE && ()} in one of the js files. Gatsby develop works fine as expected.

Expected result

Site builds without error and conditionally displays message if user is using IE

Actual result

Site unable to build with gatsby build but fine with gatsby develop.

WebpackError: ReferenceError: document is not defined
  - header.js:20 Module../src/components/header.js
    lib/src/components/header.js:20:27
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - sync-requires.js:8 Object../.cache/sync-requires.js
    lib/.cache/sync-requires.js:8:58
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - static-entry.js:9 Module../.cache/static-entry.js
    lib/.cache/static-entry.js:9:22
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - bootstrap:83 
    lib/webpack/bootstrap:83:1
  - universalModuleDefinition:3 webpackUniversalModuleDefinition
    lib/webpack/universalModuleDefinition:3:1
  - universalModuleDefinition:10 Object.<anonymous>
    lib/webpack/universalModuleDefinition:10:2
  - static-entry.js:84 Module._compile
    lib/.cache/static-entry.js:84:3
  - static-entry.js:101 Object.Module._extensions..js
    lib/.cache/static-entry.js:101:9

Environment

  System:
    OS: macOS 10.14.5
    CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 10.15.3 - /usr/local/bin/node
    Yarn: 1.15.2 - /usr/local/bin/yarn
    npm: 6.9.0 - /usr/local/bin/npm
  Languages:
    Python: 2.7.10 - /usr/bin/python
  Browsers:
    Chrome: 75.0.3770.90
    Safari: 12.1.1
  npmPackages:
    gatsby: ^2.9.0 => 2.9.0 
    gatsby-image: ^2.1.3 => 2.1.3 
    gatsby-plugin-google-analytics: ^2.0.21 => 2.0.21 
    gatsby-plugin-hotjar: ^1.0.1 => 1.0.1 
    gatsby-plugin-manifest: ^2.1.1 => 2.1.1 
    gatsby-plugin-offline: ^2.1.1 => 2.1.1 
    gatsby-plugin-react-helmet: ^3.0.12 => 3.0.12 
    gatsby-plugin-sass: ^2.0.11 => 2.0.11 
    gatsby-plugin-sharp: ^2.1.3 => 2.1.3 
    gatsby-plugin-sitemap: ^2.1.0 => 2.1.0 
    gatsby-remark-external-links: 0.0.4 => 0.0.4 
    gatsby-source-contentful: ^2.0.69 => 2.0.69 
    gatsby-source-filesystem: ^2.0.39 => 2.0.39 
    gatsby-transformer-remark: ^2.3.12 => 2.3.12 
    gatsby-transformer-sharp: ^2.1.21 => 2.1.21 
  npmGlobalPackages:
    gatsby-cli: 2.6.7
@gatsbot gatsbot bot added the type: documentation An issue or pull request for improving or updating Gatsby's documentation label Jun 18, 2019
@jaysella jaysella added type: bug An issue or pull request relating to a bug in Gatsby and removed type: documentation An issue or pull request for improving or updating Gatsby's documentation labels Jun 18, 2019
@DSchau
Copy link
Contributor

DSchau commented Jun 18, 2019

@j-651 you'll want to do this check "in the browser." As a proxy for this, you can use lifecycle events (componentDidMount) or hooks (useEffect).

The code could look something like this:

import React, { useState, useEffect } from 'react'

function Something() {
  const [isIE, setIsIE] = useState(false)
  useEffect(() => {
    setIsIE(/*@cc_on!@*/ false || !!document.documentMode)
  }, [])

  return (
    <p>{isIE ? 'Internet Explorer' : 'Not IE'}</p>
  )
}

export default Something

Going to close this as answered--thanks for the question!

@DSchau DSchau closed this as completed Jun 18, 2019
@jaysella jaysella added type: question or discussion Issue discussing or asking a question about Gatsby and removed type: bug An issue or pull request relating to a bug in Gatsby labels Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants