如何在生产环境中排除/禁用Redux devtools或断开其连接?

问题描述 投票:0回答:1

顾名思义,devtools应该仅在开发过程中才可见或可访问,而不是在生产中。我不希望我的最终用户与州和调度员打交道,也不希望看到幕后发生的事情。

是否可以在生产版本中隐藏Redux Devtools或断开其连接?

javascript security web redux
1个回答
1
投票

为了从devtools中隐藏Redux,请注意以下代码:

import { createStore, applyMiddleware, compose } from 'redux';
import createSagaMiddleware from 'redux-saga';
import reducer from '~/redux/reducers';
import mySaga from '~/redux/sagas';
import { nodeEnv } from '~/utils/config';

const composeEnhancers =
  (nodeEnv !== 'production' &&
    typeof window !== 'undefined' &&
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
  compose;

const sagaMiddleware = createSagaMiddleware();

export default createStore(
  reducer,
  composeEnhancers(applyMiddleware(sagaMiddleware))
);

sagaMiddleware.run(mySaga);

这是Redux和Redux-Saga之间的集成,不重要的是:

const composeEnhancers =
  (nodeEnv !== 'production' &&
    typeof window !== 'undefined' &&
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
  compose;

composeEnhancers只需在客户端和完全开发模式下使用__REDUX_DEVTOOLS_EXTENSION_COMPOSE__进行调整,否则代码仅使用compose,这意味着它将被浏览器devtools隐藏。

© www.soinside.com 2019 - 2024. All rights reserved.