|
| 1 | +--- |
| 2 | +title: Mounts |
| 3 | +description: Browser-owned client apps — when to use a mount instead of a page route. |
| 4 | +order: 11.8 |
| 5 | +--- |
| 6 | + |
| 7 | +# Mounts |
| 8 | + |
| 9 | +A mount is a browser-owned client app mounted at a server URL. Use it when the browser owns the entire surface from boot — a search overlay, an admin console, a rich editor — rather than a server-rendered page that hydrates. |
| 10 | + |
| 11 | +## Page route vs mount |
| 12 | + |
| 13 | +| | `page(...)` | `mount(...)` | |
| 14 | +|---|---|---| |
| 15 | +| First paint | Server-rendered HTML | Browser renders from boot | |
| 16 | +| SSR | Yes | No | |
| 17 | +| Interactive | After hydration | From boot | |
| 18 | +| Use for | Most routes | Full client apps | |
| 19 | + |
| 20 | +If you need a fast first paint or SSR for SEO, use a `page(...)` route. Use a mount when the browser owns everything from the start. |
| 21 | + |
| 22 | +## Scaffold |
| 23 | + |
| 24 | +```bash |
| 25 | +sprag add mount search |
| 26 | +``` |
| 27 | + |
| 28 | +This creates: |
| 29 | + |
| 30 | +``` |
| 31 | +app/mounts/search/ |
| 32 | +├── mount.py # Manifest |
| 33 | +├── web.py # Root Component |
| 34 | +├── modules.py # Root Module (optional) |
| 35 | +└── server.py # Boot controller (optional) |
| 36 | +``` |
| 37 | + |
| 38 | +## Manifest |
| 39 | + |
| 40 | +```python |
| 41 | +# app/mounts/search/mount.py |
| 42 | +from sprag import mount |
| 43 | +from .web import SearchApp |
| 44 | +from .modules import SearchModule |
| 45 | +from .server import SearchBoot |
| 46 | + |
| 47 | +search = mount( |
| 48 | + path="/search", |
| 49 | + component=SearchApp, |
| 50 | + module=SearchModule, |
| 51 | + boot=SearchBoot, |
| 52 | + metadata={"title": "Search"}, |
| 53 | +) |
| 54 | +``` |
| 55 | + |
| 56 | +### Parameters |
| 57 | + |
| 58 | +| Parameter | Required | Description | |
| 59 | +|---|---|---| |
| 60 | +| `path` | Yes | URL path for this mount | |
| 61 | +| `component` | Yes | Root Component class | |
| 62 | +| `module` | No | Root Module class for lifecycle logic | |
| 63 | +| `boot` | No | Controller providing initial data via `load()` | |
| 64 | +| `metadata` | No | Dict of metadata (title, description, etc.) | |
| 65 | +| `shell` | No | Override the app-level shell | |
| 66 | +| `css` | No | Mount-specific CSS files | |
| 67 | +| `js` | No | Extra scripts to include | |
| 68 | +| `modules` | No | JS import aliases: `{"alias": "path/to/module.js"}` | |
| 69 | +| `providers` | No | Browser provider Modules | |
| 70 | + |
| 71 | +## Boot controller |
| 72 | + |
| 73 | +The `boot` controller's `load()` runs on the server and seeds initial state into the mount, just like a page controller. Use it to supply data the mount needs on first load. |
| 74 | + |
| 75 | +```python |
| 76 | +# app/mounts/search/server.py |
| 77 | +from sprag import Controller |
| 78 | + |
| 79 | +class SearchBoot(Controller): |
| 80 | + route = "/search" |
| 81 | + |
| 82 | + def load(self): |
| 83 | + return {"title": "Search the docs"} |
| 84 | +``` |
| 85 | + |
| 86 | +The return dict becomes the Module's initial `self.state`. If you don't need server data, omit `boot` — the mount still works with an empty initial state. |
| 87 | + |
| 88 | +## Root Component and Module |
| 89 | + |
| 90 | +The root Component renders the mount's DOM. The root Module owns lifecycle: event delegation, server calls, state. |
| 91 | + |
| 92 | +```python |
| 93 | +# app/mounts/search/web.py |
| 94 | +from sprag import Component, ui |
| 95 | + |
| 96 | +class SearchApp(Component): |
| 97 | + def render(self, props=None): |
| 98 | + return ui.div( |
| 99 | + ui.input( |
| 100 | + type="text", |
| 101 | + placeholder="Search…", |
| 102 | + data_role="search-input", |
| 103 | + class_="search-input", |
| 104 | + ), |
| 105 | + ui.div(data_role="search-status", class_="search-status"), |
| 106 | + ui.ul(data_role="search-results", class_="search-results"), |
| 107 | + class_="search-app", |
| 108 | + ) |
| 109 | +``` |
| 110 | + |
| 111 | +```python |
| 112 | +# app/mounts/search/modules.py |
| 113 | +from sprag import Module, debounce |
| 114 | + |
| 115 | +class SearchModule(Module): |
| 116 | + def on_start(self): |
| 117 | + self.delegate(self.element, "input", "[data-role='search-input']", self.on_input) |
| 118 | + |
| 119 | + @debounce(0.15) |
| 120 | + def on_input(self, event, target): |
| 121 | + self.call_action("search", {"query": target.value}).then(self._on_results) |
| 122 | + |
| 123 | + def _on_results(self, result): |
| 124 | + self.set_state(result.value or {}) |
| 125 | +``` |
| 126 | + |
| 127 | +## Providers |
| 128 | + |
| 129 | +Pass browser-side provider Modules the same way as with `page(...)`: |
| 130 | + |
| 131 | +```python |
| 132 | +search = mount( |
| 133 | + path="/search", |
| 134 | + component=SearchApp, |
| 135 | + module=SearchModule, |
| 136 | + providers={"toast": ToastProvider}, |
| 137 | +) |
| 138 | +``` |
| 139 | + |
| 140 | +Resolve them inside any Module with `self.provider("toast")`. |
0 commit comments