Redux Toolkit serializableCheck middleware warning behavior
0 reputation · 02 Aug 2023, 01:14 UTC
0 reputation · 02 Aug 2023, 01:14 UTC
Redux Toolkit's configureStore includes a default serializableCheck middleware designed to ensure that actions and state remain serializable. This is critical for maintaining features like time-travel debugging and state persistence.
While the middleware identifies non-serializable values—such as Date objects, Map, Set, or class instances—it currently issues console warnings rather than blocking the state update or throwing errors. This creates a discrepancy between the intent of the check and the actual state of the store, as the non-serializable data is still successfully committed to the state.
There is uncertainty regarding the optimal configuration for production environments where console warnings are often suppressed, yet the performance overhead of the runtime check remains.
serializableCheck middleware provide a mechanism to escalate warnings to hard errors for specific environment builds?32275 reputation · 02 Aug 2023, 09:41 UTC
The serializableCheck middleware in Redux Toolkit (RTK) is designed as a developer-experience tool. It issues console.warn messages to alert developers when non-serializable values (like Date, Map, or Set) enter the state or actions, but it does not block the state update or throw errors. This ensures that the application remains functional even if the state violates serialization best practices.
There is no built-in configuration option within the serializableCheck middleware to automatically escalate warnings to hard errors based on the environment. To achieve this behavior, you must implement a custom wrapper or a separate middleware that validates the action/state and throws an exception in development or staging environments.
The detection of non-serializable payloads depends entirely on where serializableCheck sits in the middleware chain. Because middleware executes in the order it is provided to configureStore:
serializableCheck runs, the check will detect it and trigger a warning.serializableCheck has already processed the action, the check will not detect the change, and no warning will be issued.To manage performance overhead in production and reduce noise in development, configure the middleware using the middleware callback in configureStore. This allows you to disable the check in production or ignore specific known non-serializable paths.
import { configureStore } from '@reduxjs/toolkit';
const store = configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
// Ignore specific action types that are known to carry non-serializable data
ignoredActions: ['your/action/type'],
// Ignore specific paths in the state
ignoredPaths: ['some.state.path'],
},
}).filter((middleware) => {
// Example: Completely remove the check in production to save overhead
return process.env.NODE_ENV !== 'production' || middleware.name !== 'serializableCheck';
}),
});
new Date(); verify that a console warning appears in the browser.Map to the payload before the default middleware array to confirm the warning triggers. Move it after to confirm the warning disappears.Diagnostic Detail: Are you using a custom persistence layer (e.g., redux-persist) that requires these non-serializable values, or are they accidental inclusions?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.