Disable chrome react DevTools for production

前端 未结 5 658
[愿得一人]
[愿得一人] 2020-12-09 10:06

I\'m trying to browserify my react app for production using gulp and envify to setup NODE_ENV. So I can remove react warning, error reporting in the console, and even my cod

相关标签:
5条回答
  • 2020-12-09 10:31

    If you are using redux-devtools-extension you can do this.

    const devTools =
      process.env.NODE_ENV === "production"
        ? applyMiddleware(...middlewares)
        : composeWithDevTools(applyMiddleware(...middlewares));
    
    const store = createStore(rootReducer, initialState, devTools);
    

    This will make sure your devtools extension only works in the development environment and not in the production environment

    0 讨论(0)
  • 2020-12-09 10:54

    Just improve @peteriod answer, to make sure Dev tool has installed or not

    if (typeof window.__REACT_DEVTOOLS_GLOBAL_HOOK__ === 'object') {
       __REACT_DEVTOOLS_GLOBAL_HOOK__.inject = function() {};
    }
    
    0 讨论(0)
  • 2020-12-09 10:54

    you can check the mode :


    add below code befor </body> in your index.html in the public folder

      <input type="hidden" value="%NODE_ENV%" id="node_env_mode" />
    <script>
      var mode = document.querySelector("#node_env_mode").value;
      if (
        mode === "production" &&
        typeof window.__REACT_DEVTOOLS_GLOBAL_HOOK__ === "object"
      ) {
        window.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject = function () {};
      }
    </script>
    
    0 讨论(0)
  • 2020-12-09 10:55

    According to an issue on Github, you can add run a single javascript line before react is loaded to prevent it.

    From #191 of react-devtools

    <script>
    window.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject = function () {}
    </script>
    

    Then, you may consider wrapping this with your environment condition, like that you could do sth like below in your server side rendering. Let's say Pug (formerly known as Jade):

    #{process.env.NODE_ENV == 'production' ? "window.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject = function(){}" : ""}
    

    However, it would be still a good practice to put the business logic and the sensitive data back to your server.

    0 讨论(0)
  • 2020-12-09 10:57

    You could also delete all source map after build by adding the command below on your package.json file.

    "scripts":{
         ...,
        "build": "react-scripts build",
        "postbuild": "rimraf build/**/*.map"
    }
    

    It makes it a bit difficult to navigate the components and hides them on devtools - source

    0 讨论(0)
提交回复
热议问题