microfront-reactive-query-usecase Svelte Themes

Microfront Reactive Query Usecase

This small microfrontend shows the usecase of reactive query library with microfrontend which is in react and svelte

Microfrontend Project with Module Federation

A microfrontend architecture project demonstrating module federation with React (host) and Svelte (remote) applications, sharing state via RxJS.

Architecture

  • Host App (React): Displays a list of users fetched from JSONPlaceholder API
  • Remote App (Svelte): Shows user details in a sliding sidebar
  • Shared State: RxJS-based reactive store for user data synchronization
  • URL Sync: Selected user ID is synced with the browser URL (?user=[id])

Features

  • šŸ”„ Module Federation: Webpack 5 module federation for microfrontend architecture
  • āš›ļø React Host: Main application with user list
  • šŸ”„ Svelte Remote: Sidebar component for user details
  • šŸ“” Reactive State: RxJS BehaviorSubject for real-time state sharing
  • šŸ”— URL Synchronization: Browser URL reflects selected user
  • šŸŽØ Modern UI: Beautiful, responsive design with smooth animations
  • šŸ“± Responsive: Works on desktop and mobile devices

Project Structure

microfront-reactive-query/
ā”œā”€ā”€ host/                 # React host application
│   ā”œā”€ā”€ src/
│   │   ā”œā”€ā”€ App.jsx      # Main React component
│   │   ā”œā”€ā”€ App.css      # Styles
│   │   ā”œā”€ā”€ userStore.js # Shared RxJS store
│   │   └── index.js     # Entry point
│   ā”œā”€ā”€ public/
│   │   └── index.html
│   ā”œā”€ā”€ webpack.config.js
│   └── package.json
ā”œā”€ā”€ remote-svelte/       # Svelte remote application
│   ā”œā”€ā”€ src/
│   │   ā”œā”€ā”€ UserDetailSidebar.svelte  # Sidebar component
│   │   ā”œā”€ā”€ UserDetailSidebar.js      # React wrapper
│   │   ā”œā”€ā”€ userStore.js              # Shared RxJS store
│   │   ā”œā”€ā”€ App.svelte                # Standalone app
│   │   └── index.js
│   ā”œā”€ā”€ public/
│   │   └── index.html
│   ā”œā”€ā”€ webpack.config.js
│   └── package.json
└── package.json         # Root package with scripts

Installation

  1. Install dependencies for all projects:
npm install
cd host && npm install
cd ../remote-svelte && npm install
cd ..

Or use the convenience script:

npm run install:all

Running the Project

Development Mode

Start both applications simultaneously:

npm run dev

Or start them individually:

# Terminal 1 - Start the host app (port 3002)
npm run dev:host

# Terminal 2 - Start the remote app (port 3001)
npm run dev:remote

Then open your browser:

How It Works

  1. Host Application (React on port 3002):

    • Fetches user list from https://jsonplaceholder.typicode.com/users
    • Stores users in RxJS BehaviorSubject
    • Displays users in a grid layout
    • On user click, updates selected user ID and URL parameter
    • Lazy loads the Svelte sidebar component via Module Federation
  2. Remote Application (Svelte on port 3001):

    • Exposes UserDetailSidebar component via Module Federation
    • Subscribes to the shared RxJS store for selected user changes
    • Displays detailed user information in a sliding sidebar
    • Sidebar can be closed by clicking the close button or backdrop
  3. Shared State:

    • Both apps use identical userStore.js with RxJS BehaviorSubjects
    • Module Federation ensures singleton pattern for shared dependencies
    • Real-time synchronization across microfrontends
  4. URL Synchronization:

    • When a user is selected, URL updates to ?user=[id]
    • On page load, checks URL for initial user selection
    • Enables deep linking and browser history navigation

Technologies Used

  • React 18: Host application framework
  • Svelte 4: Remote sidebar component
  • Webpack 5: Module Federation and bundling
  • RxJS 7: Reactive state management
  • JSONPlaceholder: Fake REST API for user data

Module Federation Configuration

Host (React):

remotes: {
  remoteSvelte: 'remoteSvelte@http://localhost:3001/remoteEntry.js'
}

Remote (Svelte):

exposes: {
  './UserDetailSidebar': './src/UserDetailSidebar.js'
}

Customization

  • Change API endpoint: Modify the fetch URL in host/src/App.jsx
  • Styling: Update CSS files in respective apps
  • Port numbers: Change ports in webpack configs
  • Sidebar width: Adjust width in UserDetailSidebar.svelte styles

Production Build

cd host && npm run build
cd ../remote-svelte && npm run build

License

MIT

Top categories

Loading Svelte Themes