Data Sharing
AppShell's data sharing feature allows you to share your main application's data with micro-frontend applications.
Overview
It provides a flexible API to share data effectively, ensuring that each component within your ecosystem can access application data easily.
How It Works
Data sharing is facilitated through a combination of React context and the framebus library for cross-origin communication. This setup ensures that micro-frontends running in isolated iframes can receive shared data seamlessly.
Flow
Initialization -
AppShellProviderinitializes with the shared dataClient Connection - When a client iframe loads, it sends a
SET_CONFIGeventData Distribution - Shell responds with
SET_DATAcontaining the shared dataUpdates - When shared data changes, shell broadcasts updates to all connected clients
Usage
Pass the data prop to AppShellProvider:
import { AppShellProvider, type ApplicationData } from '@akinon/app-shell';
const App = () => {
const data: ApplicationData = {
user: {
id: 1,
name: 'Jane Doe',
email: '[email protected]'
},
theme: 'dark',
locale: 'en'
};
return (
<AppShellProvider
apps={registeredApps}
data={data}
navigation={navigationConfig}
actions={shellActions}
>
{/* Your shell application */}
</AppShellProvider>
);
};ApplicationData Type
The ApplicationData type is flexible, allowing you to share any serializable data. The optional searchParams field is commonly used for URL query parameters.
Example Data Structure
Dynamic Data Updates
When shared data changes, update the data prop and the changes will be propagated to all connected clients:
Accessing Shared Data (Client Side)
Clients access shared data via the useAppClient hook:
Best Practices
1. Share Only Necessary Data
Avoid sharing the entire application state. Share only the data that micro-applications need for their functionality.
2. Avoid Data Duplication
Clients should not copy shared data into their own state. Always reference the shared data from useAppClient.
3. Use Serializable Data Only
Only serializable data can be shared via postMessage. Functions, class instances, and circular references cannot be shared.
See MDN Structured Clone Algorithm for the full list of supported types.
4. Keep Data Flat When Possible
Deeply nested data structures can be harder to work with and may impact performance during serialization.
Next Steps
Actions - Define custom actions for clients
Navigation - Configure navigation helpers
Last updated
Was this helpful?

