Cleaned dead code and updated documents
This commit is contained in:
@@ -10,23 +10,31 @@ rendering engine.
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'mizan.ssr.MizanTemplates',
|
||||
...
|
||||
'DIRS': [BASE_DIR / 'frontend'],
|
||||
'OPTIONS': {
|
||||
'worker': 'path/to/mizan-ssr/src/worker.tsx',
|
||||
'timeout': 5,
|
||||
},
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
Then `render(request, 'ProfilePage', context)` calls the Bun
|
||||
Then `render(request, 'components/Hello.tsx', context)` calls the Bun
|
||||
subprocess bridge instead of rendering a Django/Jinja2 template.
|
||||
**The component name IS the template name.**
|
||||
**The template name IS a `.tsx`/`.jsx` file path**, resolved against
|
||||
`DIRS`; `get_template` returns a `MizanTemplate` wrapping the absolute
|
||||
file path. The context dict becomes the component's props (`request`
|
||||
and `csrf_token` stripped). Rendered output is wrapped in
|
||||
`<div id="mizan-root">…</div>` plus a
|
||||
`<script>window.__MIZAN_SSR_DATA__=…</script>` hydration payload.
|
||||
|
||||
## AFI boundary
|
||||
|
||||
| Side | Responsibility |
|
||||
|---|---|
|
||||
| Backend adapter | Implements `mizan.ssr()` — executes context functions, gathers data |
|
||||
| Frontend adapter | Implements `renderToHTML()` — takes component + props, produces HTML |
|
||||
| Bun subprocess | Hosts the frontend adapter |
|
||||
| stdin/stdout JSON-RPC | Transport between the two |
|
||||
| Backend adapter (`SSRBridge`) | Manages the Bun subprocess lifecycle; gathers props |
|
||||
| Bun worker (`worker.tsx`) | `import()`s the file path, `renderToString(createElement(Component, props))` |
|
||||
| stdin/stdout JSON-RPC | Newline-delimited; `{id, method:"render", params:{file, props}}` → `{id, html}` / `{id, error}`; `ping` → `{id, pong:true}` |
|
||||
|
||||
## Why template backend
|
||||
|
||||
@@ -35,16 +43,22 @@ subprocess bridge instead of rendering a Django/Jinja2 template.
|
||||
- Django developers already use `render(request, template, context)`
|
||||
— no new API to learn.
|
||||
- URL routing, views, middleware, auth — all unchanged.
|
||||
- The template tag `{% mizan_render %}` is a convenience for
|
||||
developers who *also* use Django templates (e.g., a base.html shell
|
||||
with Mizan components inside).
|
||||
|
||||
> A `templatetags/` package exists for a future `{% mizan_render %}`
|
||||
> convenience tag (base.html shell with Mizan components inside), but
|
||||
> it is currently empty — no tag is implemented yet.
|
||||
|
||||
## Implementation surface
|
||||
|
||||
The SSR bridge module implements Django's template backend interface:
|
||||
The SSR backend (`mizan/ssr/backend.py`) implements Django's template
|
||||
backend interface:
|
||||
|
||||
- `BaseEngine` subclass
|
||||
- `Template` class with `.render(context, request)`
|
||||
- `MizanTemplates(BaseEngine)` — requires `OPTIONS['worker']` (path to
|
||||
`worker.tsx`); `get_template(name)` resolves a file under `DIRS`
|
||||
- `MizanTemplate` with `.render(context, request)` → calls the bridge
|
||||
- `SSRBridge` (`bridge.py`) — spawns `bun run <worker>`, holds the
|
||||
persistent subprocess, correlates requests by message id, thread-safe,
|
||||
auto-restarts on crash, waits for the worker's ready signal
|
||||
|
||||
Everything Django expects from a template backend, but the actual
|
||||
rendering routes to Bun.
|
||||
|
||||
Reference in New Issue
Block a user