Classes
LiveReloadMixin
Mixin class providing live reload functionality via SSE.
This class is designed to be mixed into a…
LiveReloadMixin
Mixin class providing live reload functionality via SSE.
This class is designed to be mixed into an HTTP request handler.
It provides two key methods:
- handle_sse(): Handles the SSE endpoint (/bengal_reload)
- serve_html_with_live_reload(): Injects the live reload script into HTML
The SSE connection remains open, sending keepalive comments roughly every 9 seconds and "reload" messages (or JSON payloads) when the site is rebuilt.
Attributes
| Name | Type | Description |
|---|---|---|
path |
str |
|
client_address |
tuple[str, int] |
|
wfile |
BufferedIOBase |
Methods 2
handle_sse
Handle Server-Sent Events endpoint for live reload.
Maintains a persistent HTT…
handle_sse
def handle_sse(self) -> None
Handle Server-Sent Events endpoint for live reload.
Maintains a persistent HTTP connection and sends SSE messages:
- Keepalive comments (: keepalive) every 30 seconds
- Reload events (data: reload) when site is rebuilt
The connection remains open until the client disconnects or an error occurs.
serve_html_with_live_reload
Serve HTML file with live reload script injected (with caching).
Uses file mod…
serve_html_with_live_reload
def serve_html_with_live_reload(self) -> bool
Serve HTML file with live reload script injected (with caching).
Uses file modification time caching to avoid re-reading/re-injecting unchanged files during rapid navigation.
Returns
True if HTML was served (with or without injection), False if not HTMLbool
—
Internal Methods 1
_inject_live_reload
Inject live reload script into an HTTP response.
This method is provided for t…
_inject_live_reload
def _inject_live_reload(self, response: bytes) -> bytes
Inject live reload script into an HTTP response.
This method is provided for test compatibility and wraps the module-level inject_live_reload_into_response function.
Parameters 1
response |
bytes |
Complete HTTP response (headers + body) |
Returns
Modified response with live reload script injectedbytes
—
Functions
notify_clients_reload
Notify all connected SSE clients to reload.
Sends a "reload" message to all connected clients via …
notify_clients_reload
def notify_clients_reload() -> None
Notify all connected SSE clients to reload.
Sends a "reload" message to all connected clients via their queues. Clients with full queues are skipped to avoid blocking.
This is called after a successful build to trigger browser refresh.
send_reload_payload
Send a structured JSON payload to connected SSE clients.
send_reload_payload
def send_reload_payload(action: str, reason: str, changed_paths: list[str]) -> None
Send a structured JSON payload to connected SSE clients.
Parameters 3
| Name | Type | Default | Description |
|---|---|---|---|
action |
str |
— | 'reload' | 'reload-css' | 'reload-page' |
reason |
str |
— | short machine-readable reason string |
changed_paths |
list[str] |
— | list of changed output paths (relative to output dir) |
set_reload_action
Set the next reload action for SSE clients.
Actions:
- 'reload' : full page reload
- …
set_reload_action
def set_reload_action(action: str) -> None
Set the next reload action for SSE clients.
Actions:
- **'reload' **: full page reload
- **'reload-css' **: CSS hot-reload (no page refresh)
- **'reload-page' **: explicit page reload (alias of 'reload')
Parameters 1
| Name | Type | Default | Description |
|---|---|---|---|
action |
str |
— |
inject_live_reload_into_response
Inject live reload script into an HTTP response.
inject_live_reload_into_response
def inject_live_reload_into_response(response: bytes) -> bytes
Inject live reload script into an HTTP response.
Parameters 1
| Name | Type | Default | Description |
|---|---|---|---|
response |
bytes |
— | Complete HTTP response (headers + body) |
Returns
Modified response with live reload script injectedbytes
—