Architecture
An overview of superreload's internal architecture.
Components
┌─────────────────────────────────────────────────────────────┐
│ Browser │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ JavaScript Client (injected by middleware) │ │
│ │ - WebSocket connection │ │
│ │ - CSS hot swap │ │
│ │ - Error overlay │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
│ WebSocket (port 9877)
▼
┌─────────────────────────────────────────────────────────────┐
│ WebSocketServer │
│ - Manages client connections │
│ - Broadcasts reload/error messages │
└─────────────────────────────────────────────────────────────┘
│
│
▼
┌─────────────────────────────────────────────────────────────┐
│ DjangoReloadServer │
│ - Orchestrates file watching and reloading │
│ - Determines file type and appropriate action │
│ - Handles cooldown to prevent duplicate reloads │
└─────────────────────────────────────────────────────────────┘
│
┌───────────────┼───────────────┐
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ FileWatcher │ │ Reloader │ │ DjangoFramework │
│ │ │ │ │ │
│ - watchfiles │ │ - importlib │ │ - URL cache │
│ - debouncing │ │ - reload() │ │ - Template cache│
│ - filtering │ │ - error capture │ │ - App discovery │
└─────────────────┘ └─────────────────┘ └─────────────────┘
Core Modules
superreload.core.watcher
File system watching using the watchfiles library (Rust-based, fast).
FileWatcher: Async generator that yields file changesFileWatcherConfig: Configuration for paths, patterns, ignoresFileChange: Dataclass representing a single file change
superreload.core.reloader
Python module reloading.
Reloader: Coordinates module reloadingReloadResult: Result of a reload attempt (success/failure, errors)- Uses
importlib.reload()under the hood
superreload.core.websocket
WebSocket server for browser communication.
WebSocketServer: Async WebSocket server usingwebsocketsWebSocketMessage: Message format for client communication- Supports multiple concurrent browser connections
superreload.core.errors
Error formatting for the overlay.
format_exception(): Extracts error details including local variablesget_source_context(): Gets source code around error lineReloadError: Dataclass with all error information
superreload.core.framework
Framework abstraction layer.
Framework: Base class for framework adaptersFrameworkRegistry: Registry for discovering/loading frameworksReloadContext: Context passed to framework hooks
Django Integration
superreload.frameworks.django.framework
Django-specific framework adapter.
- Clears URL resolvers after reload
- Clears template caches
- Identifies non-reloadable files (migrations, settings)
- Discovers Django app paths to watch
superreload.frameworks.django.middleware
WSGI middleware that injects the JavaScript client.
- Only active when
DEBUG = True - Injects script before
</body> - Handles error responses too (for recovery)
superreload.frameworks.django.reload_server
Orchestrates everything for Django.
- Creates FileWatcher, Reloader, WebSocketServer
- Routes file changes to appropriate handlers
- Manages reload cooldown
Message Types
WebSocket messages between server and browser:
| Type | Direction | Description |
|---|---|---|
connected | Server → Browser | Connection acknowledged |
reload | Server → Browser | Trigger page refresh |
css_reload | Server → Browser | Hot swap CSS files |
js_reload | Server → Browser | Reload JavaScript (full refresh) |
error | Server → Browser | Show error overlay |
Data Flow
- User saves a file
watchfilesdetects the changeFileWatcheryields the change after debouncingDjangoReloadServer._handle_file_changes()processes it:- Python: Reload module via
Reloader, notify browser - CSS: Send
css_reloadmessage - JS/HTML: Send
reloadmessage
- Python: Reload module via
- Browser receives message and acts accordingly