Click to expand
This project demonstrates a modern microfrontend architecture for an e-commerce application. The system consists of multiple independent frontend applications (microfrontends) that work together seamlessly, powered by a Django REST API backend.
- ποΈ Modular Architecture: Independent microfrontends that can be developed and deployed separately
- π§ Technology Flexibility: Each microfrontend can use different technologies (currently all React)
- π Independent Deployment: Deploy components separately to different servers
- π Loose Coupling: Microfrontends communicate through events and shared APIs
- π³ Containerized: Full Docker support for easy development and deployment
- π± Responsive Design: Mobile-first responsive UI components
βββββββββββββββββββ ββββββββββββββββββββ
β Shell App β β Microfrontends β
β (Container) ββββββ€ (Components) β
β Port: 3000 β β Ports: 3001-4 β
βββββββββββββββββββ ββββββββββββββββββββ
β β
βββββββββ¬ββββββββ¬ββββββββ
β β
βββββββββΌββββββββΌββββββββ
β Django Backend β
β Port: 8000 β
βββββββββββββββββββββββββ
- Shell Application loads microfrontends dynamically using Webpack
- Microfrontends register themselves in the global window object
- Components communicate through Custom Events
- All frontends consume the same Django REST API
Click to view detailed structure
microfrontend-project/
βββ π apps/ # Frontend applications
β βββ π shell/ # Main container application
β β βββ π src/
β β β βββ App.jsx # Main shell component
β β β βββ App.css # Shell styles
β β β βββ index.js # Entry point
β β βββ package.json
β β βββ webpack.config.js
β β βββ Dockerfile
β β βββ nginx.conf
β β
β βββ π header/ # Header microfrontend
β β βββ π src/
β β β βββ Header.jsx # Header component
β β β βββ Header.css
β β β βββ index.js
β β βββ (similar structure...)
β β
β βββ π product-list/ # Product listing microfrontend
β βββ π user-profile/ # User profile microfrontend
β βββ π cart/ # Shopping cart microfrontend
β
βββ π backend/ # Django REST API
β βββ π ecommerce/
β β βββ π products/
β β β βββ models.py # Database models
β β β βββ views.py # API views
β β β βββ serializers.py # Data serializers
β β β βββ urls.py # API routes
β β βββ settings.py # Django settings
β β βββ urls.py # Main URLs
β βββ requirements.txt
β βββ Dockerfile
β βββ create_sample_data.py # Sample data generator
β
βββ π shared/ # Shared utilities and types
βββ docker-compose.yml # Multi-container setup
βββ package.json # Root package.json
βββ turbo.json # Build system configuration
Before you begin, ensure you have the following installed:
| Software | Version | Purpose |
|---|---|---|
| Node.js | 16.x or higher | JavaScript runtime |
| npm | 7.x or higher | Package manager |
| Python | 3.8 or higher | Backend runtime |
| Docker | 20.x or higher | Containerization |
| Docker Compose | 2.x or higher | Multi-container management |
# Check Node.js version
node --version
# Check npm version
npm --version
# Check Python version
python --version
# Check Docker installation
docker --version
docker-compose --versiongit clone https://github.com/qiyascc/microfrontend-example/
cd microfrontend-example
# Initialize root package.json
cd Shared/
npm init -yClick for detailed backend setup
cd backend
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On Mac/Linux:
source venv/bin/activate
# Install dependencies
pip install -r requierements.txt
Complete backend setup continues...
- Port: 3000
- Role: Main container that loads and orchestrates all microfrontends
- Features: Dynamic script loading, error handling, layout management
- Port: 3001
- Role: Navigation and site header
- Features: Responsive menu, logo, navigation links
- Port: 3002
- Role: Display and manage product catalog
- Features: Product grid, search, add to cart functionality
- Port: 3003
- Role: User information and account management
- Features: Profile display, order history, settings
- Port: 3004
- Role: Shopping cart management
- Features: Add/remove items, quantity management, checkout
# Build and start all services
docker-compose up --build
# Run in background
docker-compose up -d --build
# View logs
docker-compose logs -f
# Stop services
docker-compose downBackend:
cd backend
python manage.py migrate
python create_sample_data.py
python manage.py runserverFrontends (run in separate terminals):
# Shell app
cd apps/shell && npm run dev
# Header microfrontend
cd apps/header && npm run dev
# Product list microfrontend
cd apps/product-list && npm run dev
# User profile microfrontend
cd apps/user-profile && npm run dev
# Cart microfrontend
cd apps/cart && npm run dev| Service | URL | Port |
|---|---|---|
| Shell Application | http://localhost:3000 | 3000 |
| Header MF | http://localhost:3001 | 3001 |
| Product List MF | http://localhost:3002 | 3002 |
| User Profile MF | http://localhost:3003 | 3003 |
| Cart MF | http://localhost:3004 | 3004 |
| Django API | http://localhost:8000 | 8000 |
| Django Admin | http://localhost:8000/admin | 8000 |
docker-compose.yml
version: '3.8'
services:
shell:
build: ./apps/shell
ports: ["3000:80"]
environment:
- REACT_APP_HEADER_URL=http://localhost:3001/remoteEntry.js
- REACT_APP_PRODUCT_LIST_URL=http://localhost:3002/remoteEntry.js
- REACT_APP_USER_PROFILE_URL=http://localhost:3003/remoteEntry.js
- REACT_APP_CART_URL=http://localhost:3004/remoteEntry.js
depends_on:
- header
- product-list
- user-profile
- cart
header:
build: ./apps/header
ports: ["3001:80"]
product-list:
build: ./apps/product-list
ports: ["3002:80"]
environment:
- REACT_APP_API_URL=http://backend:8000
user-profile:
build: ./apps/user-profile
ports: ["3003:80"]
environment:
- REACT_APP_API_URL=http://backend:8000
cart:
build: ./apps/cart
ports: ["3004:80"]
backend:
build: ./backend
ports: ["8000:8000"]
volumes:
- ./backend:/app
command: >
sh -c "python manage.py migrate &&
python create_sample_data.py &&
python manage.py runserver 0.0.0.0:8000"
networks:
microfrontend-network:
driver: bridge# Build images
docker-compose build
# Start services
docker-compose up -d
# Stop services
docker-compose down
# Rebuild specific service
docker-compose build shell
# View service logs
docker-compose logs shellEach microfrontend can be deployed to separate servers:
# Build each microfrontend
cd apps/shell && npm run build
cd apps/header && npm run build
cd apps/product-list && npm run build
cd apps/user-profile && npm run build
cd apps/cart && npm run build
# Deploy to different servers
scp -r apps/shell/dist/* user@shell-server:/var/www/html/
scp -r apps/header/dist/* user@header-server:/var/www/html/
scp -r apps/product-list/dist/* user@products-server:/var/www/html/
scp -r apps/user-profile/dist/* user@profile-server:/var/www/html/
scp -r apps/cart/dist/* user@cart-server:/var/www/html/Shell Environment:
REACT_APP_HEADER_URL=https://header.yourdomain.com/remoteEntry.js
REACT_APP_PRODUCT_LIST_URL=https://products.yourdomain.com/remoteEntry.js
REACT_APP_USER_PROFILE_URL=https://profile.yourdomain.com/remoteEntry.js
REACT_APP_CART_URL=https://cart.yourdomain.com/remoteEntry.jsMicrofrontend Environment:
REACT_APP_API_URL=https://api.yourdomain.comGET /api/products/- List all productsGET /api/products/{id}/- Get product detailsPOST /api/products/- Create new productPUT /api/products/{id}/- Update productDELETE /api/products/{id}/- Delete product
GET /api/users/- List usersGET /api/users/{id}/- Get user detailsPOST /api/users/- Create user
GET /api/categories/- List categoriesGET /api/categories/{id}/- Get category details
Microfrontends not loading
Problem: Components show "Loading..." but never load Solution:
- Check if microfrontend servers are running
- Verify CORS headers are set correctly
- Check browser console for script loading errors
- Ensure components are exported to window object
// In each microfrontend's main component
if (typeof window !== 'undefined') {
window.ComponentName = ComponentName;
}CORS errors
Problem: Browser shows CORS policy errors Solution:
- Ensure Django CORS headers are configured
- Check nginx CORS settings in each microfrontend
- Verify all services are using same-origin or proper CORS headers
# In Django settings.py
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000",
"http://localhost:3001",
# ... all other ports
]Docker container issues
Problem: Containers failing to start Solution:
- Check Dockerfile paths and context
- Verify port mappings aren't conflicting
- Check container logs:
docker-compose logs service_name - Ensure all required environment variables are set
- Check Service Status:
docker-compose ps- View Logs:
docker-compose logs -f shell- Test API Endpoints:
curl http://localhost:8000/api/products/- Browser Developer Tools:
- Check Network tab for failed requests
- Look for JavaScript errors in Console
- Verify components are registered in window object
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Test all microfrontends individually
- Submit a pull request
- Use consistent naming conventions
- Follow React best practices
- Include proper error handling
- Write meaningful commit messages
- Update documentation for new features
This project is licensed under the MIT License - see the LICENSE file for details.
