path:
/README.md
5.12 KB | plain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# liveside
A tiny live-reload server written in POSIX-first portable C99. It watches a directory tree
for changes and pushes the browser a Server-Sent Events (SSE) `reload`
message, which reloads the page.
Runs on macOS, Linux, the BSDs, and Windows. The main server code stays
POSIX-first (`poll`, `dirent`, `unistd`, BSD sockets, `stat`) and uses
[TinyCThread](https://github.com/tinycthread/tinycthread)
for portable threads/mutexes. On Win32 toolchains that do not provide POSIX
headers directly, use packages from [win32ports](https://github.com/win32ports).
Project-owned code under `ports/` is reserved for reusable pieces that are core
to liveside and may be extracted later, such as socket smoothing,
ignore-file matching, and the native Windows watcher.
It is deliberately **not** a static file server — it only ever answers
two requests, `/livereload` (the SSE stream) and `/liveside.js` (the
client snippet). Your actual site keeps being served by whatever you
already use — `darkhttpd`, `lighttpd`, `python3 -m http.server`, anything
— on its own port. liveside just sits alongside it (hence the name).
It fills the same role as the `autorefresh` target in the
saikrupagas.com `Makefile` (`ls | entr make refresh`, which rebuilds and
then simulates a keypress in Arc/Firefox), but works in any browser
without AppleScript/xdotool or driving a specific app.
## Build
Fetch dependencies with mor, then build:
```bash
make deps
make
```
`make` also runs `make deps` automatically. Dependencies are recorded in
[requirements.ini](requirements.ini) and downloaded into `thirdparty/`, which
is ignored by git. TinyCThread is fetched this way and compiled alongside
`liveside.c`.
On Windows, `make deps` fetches TinyCThread plus win32ports headers for POSIX
interfaces such as `dirent.h`, `getopt.h`, `sys/time.h`, and `unistd.h`. The
Makefile detects `$(OS)=Windows_NT`, links Winsock, includes the native
`ReadDirectoryChangesW` watcher port, and produces `liveside.exe`.
## Style
liveside adopts the [suckless coding style](https://suckless.org/coding_style/)
for C code. Use `make format` / `make check-format` with `.astylerc`; see
[STYLE.md](STYLE.md).
The served `/liveside.js` body is generated from `client/liveside.js` into
`client/liveside.js.inc` at build time and included by the C preprocessor, so
edit the JavaScript file directly rather than maintaining a C string copy.
## Run
```bash
./liveside -d /path/to/site -p 8081
```
Options:
- `-d watch-dir` — directory to watch recursively (default `.`)
- `-p port` — HTTP/SSE port (default `8081`)
- `-c build-cmd` — shell command to run (e.g. `make all`) before notifying
clients; if it exits non-zero, clients are not told to reload
- `-i poll-ms` — polling interval in milliseconds, **only used as a
fallback** on platforms without kqueue/inotify (default `300`)
Ctrl-C stops it.
## License
BSD 3-Clause. See [LICENSE](LICENSE).
## Client
liveside serves its own client script, so pairing it with an existing
static server (e.g. `darkhttpd` on 8000 for the site, this on 8081 for
reload) needs nothing copied between them — just point an absolute
`<script>` tag at liveside's port:
```html
<script src="http://localhost:8081/liveside.js"></script>
```
That's a cross-origin request, which script tags don't need CORS for. It
auto-detects the right origin to connect back to from its own `src`. If
you instead copy `client/liveside.js` into the site itself and load it
same-origin, set `window.__RELOAD_PORT__` before the script tag so it
knows where to find the SSE stream.
## How change detection works
- **macOS / FreeBSD / NetBSD / OpenBSD / DragonFly BSD**: opens a `kqueue`
watch (`EVFILT_VNODE`) on every file and directory under the watched tree,
so edits, adds, deletes, and renames
are reported by the kernel the moment they happen — no polling delay.
Because new files need their own watch, any event triggers a cheap
rebuild of the watch set (a full re-walk), which also lets it notice
freshly created files. A short debounce window coalesces the burst of
events a single save often produces (write + truncate + attrib change)
into one reload.
- **Linux**: same idea via `inotify`, but watches are per-directory
(inotify reports changes to files *inside* a watched directory, unlike
kqueue), so it needs far fewer watch descriptors for the same tree.
- **Windows**: uses a native `ReadDirectoryChangesW` watcher over the
recursive watch root, then filters events through the ignore rules before
broadcasting reloads.
- **Everywhere else**: falls back to polling — recursively hashing each file's
name, size, and mtime every `poll-ms` and comparing to the previous hash.
Polling works fine, just with up to `poll-ms` of latency instead of being
instant.
Ignore rules are loaded from `.gitignore` and `.livesideignore` files at the
watch root and in nested directories. Parent rules apply before child rules;
within one directory, `.gitignore` is read before `.livesideignore` so
liveside-specific rules can override with `!`. Ignored paths are excluded from
polling snapshots and watcher scans, and native watcher events for ignored
paths are filtered before reloads are broadcast.