Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Microfrontend E-Commerce Project

Microfrontend Architecture

A Modern Microfrontend Architecture Implementation with Django Backend

React Django Webpack Docker

πŸ“‹ Table of Contents

Click to expand

πŸš€ Overview

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.

✨ Key Features

  • πŸ—οΈ 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

πŸ—οΈ Architecture

System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Shell App     β”‚    β”‚   Microfrontends β”‚
β”‚   (Container)   │◄────   (Components)   β”‚
β”‚   Port: 3000    β”‚    β”‚   Ports: 3001-4  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚       β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
         β”‚   Django Backend      β”‚
         β”‚   Port: 8000          β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Communication Flow

  1. Shell Application loads microfrontends dynamically using Webpack
  2. Microfrontends register themselves in the global window object
  3. Components communicate through Custom Events
  4. All frontends consume the same Django REST API

πŸ“ Project Structure

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

βš™οΈ Prerequisites

Before you begin, ensure you have the following installed:

Required Software

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

Verification Commands

# Check Node.js version
node --version

# Check npm version
npm --version

# Check Python version
python --version

# Check Docker installation
docker --version
docker-compose --version

πŸ› οΈ Installation & Setup

Step 1: Clone and Setup Project Structure

git clone https://github.com/qiyascc/microfrontend-example/
cd microfrontend-example


# Initialize root package.json
cd Shared/
npm init -y

Step 2: Backend Setup (Django)

Click 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...

🎯 Microfrontend Details

1. Shell Application (Container)

  • Port: 3000
  • Role: Main container that loads and orchestrates all microfrontends
  • Features: Dynamic script loading, error handling, layout management

2. Header Microfrontend

  • Port: 3001
  • Role: Navigation and site header
  • Features: Responsive menu, logo, navigation links

3. Product List Microfrontend

  • Port: 3002
  • Role: Display and manage product catalog
  • Features: Product grid, search, add to cart functionality

4. User Profile Microfrontend

  • Port: 3003
  • Role: User information and account management
  • Features: Profile display, order history, settings

5. Cart Microfrontend

  • Port: 3004
  • Role: Shopping cart management
  • Features: Add/remove items, quantity management, checkout

πŸ”§ Development

Running in Development Mode

Option 1: Run All Services with Docker (Recommended)

# 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 down

Option 2: Run Services Individually

Backend:

cd backend
python manage.py migrate
python create_sample_data.py
python manage.py runserver

Frontends (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

Access Points

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 Deployment

Docker Compose Configuration

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

Docker Commands

# 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 shell

πŸš€ Production Deployment

Individual Service Deployment

Each 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/

Environment Variables for Production

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.js

Microfrontend Environment:

REACT_APP_API_URL=https://api.yourdomain.com

πŸ”„ API Endpoints

Products API

  • GET /api/products/ - List all products
  • GET /api/products/{id}/ - Get product details
  • POST /api/products/ - Create new product
  • PUT /api/products/{id}/ - Update product
  • DELETE /api/products/{id}/ - Delete product

Users API

  • GET /api/users/ - List users
  • GET /api/users/{id}/ - Get user details
  • POST /api/users/ - Create user

Categories API

  • GET /api/categories/ - List categories
  • GET /api/categories/{id}/ - Get category details

πŸ” Troubleshooting

Common Issues and Solutions

Microfrontends not loading

Problem: Components show "Loading..." but never load Solution:

  1. Check if microfrontend servers are running
  2. Verify CORS headers are set correctly
  3. Check browser console for script loading errors
  4. 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:

  1. Ensure Django CORS headers are configured
  2. Check nginx CORS settings in each microfrontend
  3. 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:

  1. Check Dockerfile paths and context
  2. Verify port mappings aren't conflicting
  3. Check container logs: docker-compose logs service_name
  4. Ensure all required environment variables are set

Debugging Tips

  1. Check Service Status:
   docker-compose ps
  1. View Logs:
   docker-compose logs -f shell
  1. Test API Endpoints:
   curl http://localhost:8000/api/products/
  1. Browser Developer Tools:
    • Check Network tab for failed requests
    • Look for JavaScript errors in Console
    • Verify components are registered in window object

🀝 Contributing

Development Workflow

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes
  4. Test all microfrontends individually
  5. Submit a pull request

Code Standards

  • Use consistent naming conventions
  • Follow React best practices
  • Include proper error handling
  • Write meaningful commit messages
  • Update documentation for new features

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


About

This is example repository for microfrontend structure

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages