Alveron Middleware Persistence is a specialized middleware designed to integrate a state persistence layer into Alveron applications. Alveron is a lightweight, Elm-inspired state management library for React (approx. 1.2kb) that leverages React's `useState` hook and handles asynchronous side effects. This middleware enables Alveron stores to automatically persist their state to browser storage mechanisms like `localStorage` or `sessionStorage`, ensuring that application data endures across page reloads or browser sessions. The current stable version, 8.0.3, is designed to work seamlessly with Alveron versions greater than 8.0.0. While a specific release cadence for this middleware is not publicly documented, its versioning typically aligns with major releases of the core `alveron` library. Its primary differentiator is its direct compatibility and integration within the Alveron ecosystem, providing a structured approach to state hydration and rehydration tailored for Alveron's model, actions, and effects architecture.
npm install alveron-middleware-persistenceVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to integrate `alveron-middleware-persistence` with an Alveron store to persist and rehydrate a simple counter's state using `localStorage`. It shows the basic setup of an Alveron store with actions and how to apply the configured persistence middleware.
Review the official Alveron and alveron-middleware-persistence documentation for migration guides and updated API usage when upgrading major versions.
Implement a versioning strategy within your persistence configuration (if supported by the library) and provide migration functions. For development, manually clear browser storage (`localStorage.clear()`) after schema changes. For production, consider user-data migration strategies or default fallbacks for undefined properties.
Ensure the middleware is only executed in browser contexts, or provide global polyfills for `localStorage` and `sessionStorage` (e.g., using `node-localstorage` or a mock) for SSR or Node.js testing environments.
Wrap your Alveron store initialization with a check for `typeof window !== 'undefined'` or provide `localStorage` / `sessionStorage` polyfills for Node.js environments.
For ESM, use `import persistenceMiddleware from 'alveron-middleware-persistence';`. For CommonJS (if supported), ensure correct interop or use a bundler that handles ESM imports correctly.
Install or upgrade Alveron: `npm install alveron@'>8.0.0'` or `yarn add alveron@'>8.0.0'`.