Document service worker usage.

This commit is contained in:
Chris McCormick 2025-07-02 11:19:38 +08:00
parent 604090d134
commit 019b981ba3
2 changed files with 30 additions and 0 deletions

View file

@ -29,6 +29,10 @@ babashka or Clojure JVM):
See [doc/nrepl](doc/nrepl).
### Service worker
See [doc/serviceworker.md](doc/serviceworker.md).
## Tasks
Run `bb tasks` to see all available tasks:

26
doc/serviceworker.md Normal file
View file

@ -0,0 +1,26 @@
# Scittle in a service worker
You can use Scittle to bootstrap a ClojureScript based service worker.
Put the following code into e.g. `scittle-sw.js` to create a JavaScript based service worker, load Scittle, then fetch your script and eval it.
```javascript
importScripts("scittle.min.js");
const request = await fetch("sw.cljs");
const text = await request.text();
const result = scittle.core.eval_string(text);
```
Then load `scittle-sw.js` in your HTML:
```html
<script>
if('serviceWorker' in navigator)
navigator.serviceWorker.register('scittle-sw.js');
</script>
```
This will load `sw.cljs` and eval it in the context of the service worker.
A ready-made example can be found at [chr15m/scittle-template-serviceworker](https://github.com/chr15m/scittle-template-serviceworker).