LoginSignup
0
1

More than 3 years have passed since last update.

React Developer Toolsのインストールを推されるコンソールメッセージ消してみた。

Posted at

行いたいこと

ブラウザのコンソールに出てる以下の文章を本番環境で消したかった。
Download the React DevTools for a better development experience: https://fb.me/react-devtools
image.png

やったこと

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
  <head>
<!-- 略 -->
  </head>
  <body>
    <script>
      // 本番環境上でコンソール上のdevtoolに関するメッセージを本番環境では表示しないようにする。
      @production
        window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
          supportsFiber: true,
          inject: function() {},
          onCommitFiberRoot: function() {},
          onCommitFiberUnmount: function() {},
        };
      @endproduction
    </script>
    <div id="app"></div>
  </body>
</html>

解説

グローバル変数に以下の内容を代入すれば、devtoolに関するメッセージは消える
※ただしバンドルされるコード内に記載しても意味がない。

  window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
          supportsFiber: true,
          inject: function() {},
          onCommitFiberRoot: function() {},
          onCommitFiberUnmount: function() {},
        };

補足(Laravel)

投稿主の環境はPHP(Laravel) + React.jsの構成になっている。
ちょうどいいからLaravelのBlade(テンプレートエンジン)上で、本番環境では、出ないようにするために、
@production@endproductionで挟んでいる。

@productionの解説
https://readouble.com/laravel/7.x/ja/blade.html#if-statements

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1