Skip to content

Felipeness/micro-frontend-nextjs

Repository files navigation

πŸ•·οΈ Spider-Man Store - Micro Frontend Architecture

TypeScript Next.js Module Federation Test Coverage License

Complete micro frontend e-commerce application built with Next.js, Module Federation, and modern web technologies

🌟 Demo & Live Preview

Live Demo: πŸ•·οΈ Spider-Man Store (Micro Frontend in Action)

Individual Micro Frontends:

Quick Demo in 30 seconds:

  1. Browse Products: View Spider-Man themed items with pricing
  2. Add to Cart: Click any product to add it to your cart
  3. Real-time Updates: Cart updates instantly across all micro frontends
  4. Checkout: Complete the purchase flow with notifications

πŸ“‹ Table of Contents

🎯 Overview

This project demonstrates a production-ready micro frontend architecture using Next.js and Module Federation. It features a Spider-Man themed e-commerce store with three independent applications working together seamlessly.

The architecture showcases modern patterns for building scalable, maintainable, and independently deployable frontend applications.

πŸ—οΈ Architecture

graph TB
    subgraph "Host Application (Port 3000)"
        H[Host App]
        H --> HC[Cart Context]
        H --> HA[Analytics]
        H --> HL[Layout & Routing]
    end
    
    subgraph "Remote Products (Port 3001)"
        P[Products App]
        P --> PL[Product List]
        P --> PC[Product Cards]
    end
    
    subgraph "Remote Cart (Port 3002)"
        C[Cart App]
        C --> CC[Cart Component]
        C --> CB[Cart Buttons]
    end
    
    H -.->|Module Federation| P
    H -.->|Module Federation| C
    P -.->|Global Context| HC
    C -.->|Global Context| HC
Loading

Applications Overview

Application Port Responsibility Exposes
Host 3000 Main orchestrator, layout, state management CartProvider, Analytics
Products 3001 Product catalog and related components ProductList, ProductCard
Cart 3002 Shopping cart functionality Cart, CartButton

πŸ”„ Micro Frontend Communication

Current Approach: Global Context via Window Object

How it works:

// Host exposes cart context globally
window.__CART_CONTEXT__ = {
  addToCart: (product) => { /* ... */ },
  items: [],
  updateQuantity: (id, quantity) => { /* ... */ },
  removeItem: (id) => { /* ... */ }
}

// Remote apps access the global context
if (window.__CART_CONTEXT__) {
  window.__CART_CONTEXT__.addToCart(product);
}

Why this approach?

  • βœ… Simplicity: Easy to implement and understand
  • βœ… Framework Agnostic: Works with any JavaScript framework
  • βœ… Real-time Sync: Immediate state updates across micro frontends
  • βœ… No Additional Dependencies: No need for message buses or event systems
  • βœ… Type Safety: Can be typed with TypeScript declarations

Trade-offs:

  • ⚠️ Global State: Uses global namespace (mitigated with namespacing)
  • ⚠️ Polling: Uses setInterval for updates (could use observers)

Alternative Approaches

1. Custom Events + Event Bus

// Publish events
window.dispatchEvent(new CustomEvent('cart:add', { 
  detail: { product } 
}));

// Subscribe to events
window.addEventListener('cart:add', (event) => {
  // Handle cart addition
});

Pros: Decoupled, event-driven, no polling
Cons: More complex, harder to debug, no type safety

2. Shared State Library (Zustand/Redux)

// Shared store across micro frontends
import { useCartStore } from '@shared/cart-store';

const addToCart = useCartStore(state => state.addToCart);

Pros: Structured state management, devtools support
Cons: Shared dependency, version conflicts, bundle duplication

3. Props Drilling from Host

// Host passes handlers as props
<ProductList onAddToCart={handleAddToCart} />

Pros: Explicit data flow, React-like patterns
Cons: Props coupling, harder to maintain with deep nesting

4. PostMessage API

// Cross-frame communication
parent.postMessage({ type: 'ADD_TO_CART', product }, '*');

Pros: True isolation, works with iframes
Cons: Complex serialization, performance overhead

Why Global Context Was Chosen

  1. Rapid Prototyping: Fastest to implement and iterate
  2. Type Safety: Easy to add TypeScript definitions
  3. React Compatibility: Works well with React's mental model
  4. Performance: Direct function calls, no serialization
  5. Debugging: Easy to inspect state in browser devtools

For production applications, consider migrating to Custom Events or a Shared State Library for better architecture.

✨ Features

πŸ›’ E-commerce Functionality

  • Product catalog with Spider-Man themed items
  • Shopping cart with real-time updates
  • Add/remove items with quantity management
  • Checkout flow with notifications
  • Pricing in Brazilian Reais (R$)

πŸ—οΈ Technical Features

  • Module Federation: Runtime code sharing between applications
  • HTTP Client: Robust client with exponential backoff retry, jitter, and idempotency
  • OpenTelemetry: Distributed tracing and observability with RED/USE metrics
  • Analytics: Page view and interaction tracking
  • Responsive Design: Mobile-first approach with modern CSS
  • Error Handling: Graceful degradation and error boundaries

πŸ§ͺ Quality Assurance

  • Unit Tests: Jest + Testing Library for component testing
  • E2E Tests: Playwright for integration testing
  • TypeScript: Full type safety across all applications
  • Linting: ESLint with consistent code standards

πŸš€ Performance

Bundle Analysis

  • Host App: ~180KB (gzipped)
  • Products Remote: ~95KB (gzipped)
  • Cart Remote: ~85KB (gzipped)
  • Shared Packages: ~45KB (gzipped)
  • Total Bundle: ~405KB (competitive for micro frontend architecture)

Runtime Metrics

  • First Contentful Paint: < 1.2s
  • Time to Interactive: < 2.5s
  • Lighthouse Score: 95+ (Performance)
  • Module Federation Load Time: < 300ms per remote

Optimization Features

  • Code Splitting: Automatic route-based splitting
  • Lazy Loading: Dynamic imports for remote components
  • Bundle Sharing: React/ReactDOM shared as singletons
  • HTTP Caching: Aggressive caching for static assets
  • Image Optimization: Next.js Image component with WebP support

πŸš€ Getting Started

Prerequisites

  • Node.js 18+ (LTS recommended)
  • PNPM 8+ (for efficient package management)

Installation

  1. Clone the repository:
git clone https://github.com/Felipeness/micro-frontend-nextjs.git
cd micro-frontend-nextjs
  1. Install dependencies:
pnpm install
  1. Start all applications:
pnpm run dev
  1. Open your browser:

Development Workflow

# Install dependencies
pnpm install

# Start all apps in development mode
pnpm run dev

# Run tests
pnpm test

# Run E2E tests
pnpm test:e2e

# Build all applications
pnpm run build

# Type checking
pnpm run type-check

# Linting
pnpm run lint

πŸ“ Project Structure

micro-frontend-nextjs/
β”œβ”€β”€ πŸ“± apps/
β”‚   β”œβ”€β”€ host/                     # Main application (Port 3000)
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ components/       # Shared UI components
β”‚   β”‚   β”‚   β”œβ”€β”€ context/          # React Context providers
β”‚   β”‚   β”‚   β”œβ”€β”€ lib/              # Utility functions
β”‚   β”‚   β”‚   └── types/            # TypeScript definitions
β”‚   β”‚   β”œβ”€β”€ pages/                # Next.js pages
β”‚   β”‚   β”œβ”€β”€ styles/               # Global styles
β”‚   β”‚   └── next.config.js        # Module Federation config
β”‚   β”‚
β”‚   β”œβ”€β”€ remote-products/          # Products micro frontend (Port 3001)
β”‚   β”‚   β”œβ”€β”€ src/components/       # Product-related components
β”‚   β”‚   β”œβ”€β”€ pages/                # Product pages
β”‚   β”‚   └── next.config.js        # MF config for products
β”‚   β”‚
β”‚   └── remote-cart/              # Cart micro frontend (Port 3002)
β”‚       β”œβ”€β”€ src/components/       # Cart-related components
β”‚       β”œβ”€β”€ pages/                # Cart pages
β”‚       └── next.config.js        # MF config for cart
β”‚
β”œβ”€β”€ πŸ“¦ packages/
β”‚   β”œβ”€β”€ http-client/              # Shared HTTP client with retry logic
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ http-client.ts    # Main HTTP client
β”‚   β”‚   β”‚   β”œβ”€β”€ retry.ts          # Retry mechanisms
β”‚   β”‚   β”‚   └── types.ts          # TypeScript definitions
β”‚   β”‚   └── __tests__/            # Unit tests
β”‚   β”‚
β”‚   └── telemetry/                # OpenTelemetry configuration
β”‚       β”œβ”€β”€ src/
β”‚       β”‚   β”œβ”€β”€ telemetry.ts      # Tracing setup
β”‚       β”‚   └── types.ts          # Telemetry types
β”‚       └── __tests__/            # Tests
β”‚
β”œβ”€β”€ πŸ”§ types/                     # Global TypeScript definitions
β”œβ”€β”€ πŸ“‹ package.json               # Root package configuration
β”œβ”€β”€ πŸ”’ pnpm-workspace.yaml       # PNPM workspace config
└── πŸ“– README.md                  # This file

πŸ› οΈ Technologies

Core Framework

Micro Frontend Architecture

State Management & Communication

  • React Context - Local state management
  • Global Context Pattern - Cross-micro frontend communication
  • Custom Analytics - Event tracking and user interaction monitoring

HTTP & Observability

  • Custom HTTP Client - With exponential backoff and jitter
  • OpenTelemetry - Distributed tracing and metrics
  • RED/USE Metrics - Request rate, error rate, duration monitoring

Testing & Quality

Development Tools

πŸ§ͺ Testing

Test Coverage Report

Package/App Statements Branches Functions Lines Status
host 96.36% 87.5% 84.61% 98.03% βœ… Excellent
http-client 93.28% 96.96% 80% 96.09% βœ… Excellent
remote-products 78.57% 85.71% 84.61% 78.57% βœ… Good
remote-cart 53.44% 44.82% 57.89% 51.78% ⚠️ Needs Improvement

Components with 100% Coverage:

  • βœ… Analytics Library - Complete tracking and session management
  • βœ… Metrics Library - RED metrics and performance monitoring
  • βœ… Retry Utilities - Exponential backoff and jitter implementation
  • βœ… TelemetryProvider - OpenTelemetry integration
  • βœ… CartContext - Shopping cart state management
  • βœ… ProductCard - Product display component
  • βœ… ProductList - Product catalog component

Unit Tests

# Run all unit tests
pnpm test

# Run tests in watch mode
pnpm test:watch

# Run tests with coverage
pnpm test:coverage

# Test specific package
pnpm --filter http-client test

E2E Tests (Cypress)

# Run Cypress tests headlessly
pnpm cypress run

# Open Cypress Test Runner
pnpm cypress open

# Run specific test file
pnpm cypress run --spec "cypress/e2e/spider-man-store.cy.ts"

Integration Tests

# Run Playwright E2E tests
pnpm test:e2e

# Run E2E tests in UI mode
pnpm test:e2e:ui

# Run E2E tests for specific browser
pnpm test:e2e --project=chromium

Test Structure

  • Unit Tests: Located in __tests__ folders within each package
  • Integration Tests: Located in e2e folders within applications
  • Cypress Tests: Located in cypress/e2e folder
  • Test Utilities: Shared test helpers and mocks

Test Statistics

  • Total Tests: 144+ unit tests
  • E2E Tests: 15 Cypress scenarios
  • Coverage Goal: 85% minimum for critical paths
  • Test Execution Time: ~3 seconds for all unit tests

πŸ“‹ Development Guidelines

Code Standards

  • Conventional Commits: Use semantic commit messages
  • TypeScript Strict Mode: All code must pass strict type checking
  • ESLint Rules: Follow established linting rules
  • Component Structure: Keep components small and focused

Micro Frontend Best Practices

  1. Independent Deployments: Each app should be deployable independently
  2. Shared Dependencies: Minimize shared runtime dependencies
  3. Error Boundaries: Implement proper error handling
  4. Performance: Lazy load micro frontends when possible
  5. Testing: Test each micro frontend in isolation

Adding New Micro Frontends

  1. Create new app in apps/ directory
  2. Configure Module Federation in next.config.js
  3. Add to workspace in pnpm-workspace.yaml
  4. Update host application to consume new remote
  5. Add appropriate tests and documentation

πŸš€ Deployment

Vercel (Recommended)

The easiest way to deploy this micro frontend architecture:

# Install Vercel CLI
npm i -g vercel

# Deploy each application
cd apps/host && vercel --prod
cd apps/remote-products && vercel --prod  
cd apps/remote-cart && vercel --prod

Docker Deployment

# Build and run with Docker Compose
docker-compose up --build

# Or build individual containers
docker build -t spider-man-host ./apps/host
docker build -t spider-man-products ./apps/remote-products
docker build -t spider-man-cart ./apps/remote-cart

Environment Variables

Create .env.local files in each app with:

# Host App
NEXT_PUBLIC_PRODUCTS_URL=https://your-products-app.vercel.app
NEXT_PUBLIC_CART_URL=https://your-cart-app.vercel.app
NEXT_PRIVATE_LOCAL_WEBPACK=true

# Remote Apps  
NEXT_PUBLIC_HOST_URL=https://your-host-app.vercel.app

Production Considerations

  • CDN: Use a CDN for static assets and Module Federation remotes
  • CORS: Configure CORS policies for cross-origin module loading
  • Error Monitoring: Add Sentry or similar for production error tracking
  • Load Balancing: Consider load balancers for high-traffic deployments

🀝 Contributing

We welcome contributions! Here's how to get started:

Development Process

  1. Fork the repository
  2. Clone your fork: git clone https://github.com/your-username/micro-frontend-nextjs.git
  3. Create a feature branch: git checkout -b feature/amazing-feature
  4. Install dependencies: pnpm install
  5. Make your changes following our coding standards
  6. Test your changes: pnpm test && pnpm test:e2e
  7. Commit using conventional commits: git commit -m "feat: add amazing feature"
  8. Push to your branch: git push origin feature/amazing-feature
  9. Open a Pull Request

What We're Looking For

  • πŸ› Bug Fixes: Issues with existing functionality
  • ✨ New Features: Additional micro frontend capabilities
  • πŸ“š Documentation: Improvements to docs and examples
  • πŸ§ͺ Tests: Better test coverage and quality
  • πŸš€ Performance: Optimization improvements
  • 🎨 UI/UX: Better user experience and design

Code Review Process

  • All submissions require review by project maintainers
  • We use automated checks (tests, linting, type checking)
  • Feedback is usually provided within 48 hours
  • Breaking changes require documentation updates

Getting Help

  • πŸ’¬ Discussions: Use GitHub Discussions for questions
  • πŸ› Issues: Use GitHub Issues for bug reports
  • πŸ“§ Email: Contact maintainers for sensitive matters

πŸ“„ License

This project is open source and available under the MIT License.

πŸ“ž Support & Community

Getting Help

  • πŸ“– Documentation: Check this README and inline code comments
  • πŸ” Issues: Search existing issues before creating new ones
  • πŸ’‘ Discussions: Use GitHub Discussions for questions and ideas
  • πŸ“§ Direct Contact: Reach out to @Felipeness

Community

  • ⭐ Star this repo if you find it helpful
  • 🍴 Fork to create your own version
  • πŸ“’ Share with others who might benefit
  • 🀝 Contribute to make it even better

Made with ❀️ by Felipe Ness - Building the future of micro frontends

About

Micro frontend architecture with Next.js, Module Federation, OpenTelemetry, and comprehensive testing

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors