Modern Frontend Architecture Patterns
Modern Frontend Architecture Patterns
As applications grow in complexity, choosing the right frontend architecture becomes crucial. Let's explore modern patterns.
Micro-frontends
Break down monolithic frontends into smaller, manageable pieces:
// Module Federation with Webpack
const ModuleFederationPlugin = require('@module-federation/webpack');
module.exports = {
plugins: [
new ModuleFederationPlugin({
name: 'shell',
remotes: {
userModule: 'user@http://localhost:3001/remoteEntry.js',
productModule: 'product@http://localhost:3002/remoteEntry.js',
},
}),
],
};
Component-Driven Development
Build applications from the component level up:
// Atomic design principles
// Atoms
const Button = ({ children, variant, onClick }) => (
<button className={`btn btn-${variant}`} onClick={onClick}>
{children}
</button>
);
// Molecules
const SearchBox = () => (
<div className="search-box">
<Input placeholder="Search..." />
<Button variant="primary">Search</Button>
</div>
);
// Organisms
const Header = () => (
<header>
<Logo />
<Navigation />
<SearchBox />
</header>
);
State Management Evolution
Modern state management approaches:
// Zustand - Simple state management
import { create } from 'zustand';
const useStore = create((set) => ({
user: null,
setUser: (user) => set({ user }),
logout: () => set({ user: null }),
}));
// React Query - Server state
import { useQuery } from '@tanstack/react-query';
function UserProfile({ userId }) {
const { data: user, isLoading } = useQuery({
queryKey: ['user', userId],
queryFn: () => fetchUser(userId),
});
if (isLoading) return <Spinner />;
return <div>{user.name}</div>;
}
Build Tool Evolution
Modern build tools for better DX:
// Vite configuration
export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
output: {
manualChunks: {
vendor: ['react', 'react-dom'],
ui: ['@mui/material'],
},
},
},
},
});
Performance Patterns
Optimize for Core Web Vitals:
// Code splitting with React.lazy
const LazyComponent = lazy(() => import('./HeavyComponent'));
// Image optimization
import Image from 'next/image';
function OptimizedImage() {
return (
<Image
src="/hero.jpg"
alt="Hero"
width={800}
height={600}
priority
placeholder="blur"
/>
);
}
// Virtual scrolling for large lists
import { FixedSizeList as List } from 'react-window';
function VirtualizedList({ items }) {
return (
<List
height={600}
itemCount={items.length}
itemSize={50}
>
{({ index, style }) => (
<div style={style}>{items[index].name}</div>
)}
</List>
);
}
Testing Strategy
Comprehensive testing approach:
// Unit tests with Testing Library
import { render, screen, fireEvent } from '@testing-library/react';
test('button handles click', () => {
const handleClick = jest.fn();
render(<Button onClick={handleClick}>Click me</Button>);
fireEvent.click(screen.getByText('Click me'));
expect(handleClick).toHaveBeenCalledTimes(1);
});
// E2E tests with Playwright
import { test, expect } from '@playwright/test';
test('user can login', async ({ page }) => {
await page.goto('/login');
await page.fill('[data-testid=email]', 'user@example.com');
await page.fill('[data-testid=password]', 'password');
await page.click('[data-testid=login-button]');
await expect(page).toHaveURL('/dashboard');
});
Conclusion
Modern frontend architecture emphasizes modularity, performance, and developer experience. Choose patterns that align with your team size and project complexity.
