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.

circle-info

Data shared with micro-applications is read-only. Clients cannot modify the shared data directly.

How It Works

Data sharing is facilitated through a combination of React context and the framebusarrow-up-right library for cross-origin communication. This setup ensures that micro-frontends running in isolated iframes can receive shared data seamlessly.

Flow

  1. Initialization - AppShellProvider initializes with the shared data

  2. Client Connection - When a client iframe loads, it sends a SET_CONFIG event

  3. Data Distribution - Shell responds with SET_DATA containing the shared data

  4. Updates - 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:

circle-info

For detailed information about accessing data on the client side, see the Client Application documentation.

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.

circle-exclamation

4. Keep Data Flat When Possible

Deeply nested data structures can be harder to work with and may impact performance during serialization.

Next Steps

Last updated

Was this helpful?