# `LiveSlice.ServiceWorker`
[🔗](https://github.com/CountlinkX-Solutions/live_slice/blob/v0.2.1/lib/live_slice/service_worker.ex#L1)

Serves the service worker of `live_slice/sw` and tells it what to precache.

The worker is the app's own small file that imports `live_slice/sw`, bundled
by its own esbuild profile into the static directory (for example
`priv/static/assets/js/sw.js`). This plug serves it at the root of the site,
so its scope is the whole origin, and writes the cache version and the
precache list in front of it:

    self.LIVE_SLICE_SW = {"version":"…","precache":["/assets/js/app-….js", …]};
    …the bundled worker…

The worker passes them to `installServiceWorker(self, {...self.LIVE_SLICE_SW, ...})`.
Since the version is part of the script, a new asset build changes the
served bytes, which is how browsers notice a new worker version.

    # lib/my_app_web/endpoint.ex, before Plug.Static, which gets the same
    # :only and :only_matching
    plug LiveSlice.ServiceWorker,
      at: "/sw.js",
      from: :my_app,
      script: "assets/js/sw.js",
      only: ~w(assets fonts images favicon.ico),
      only_matching: ~w(favicon)

Options:

  * `:from` (required) - the static directory, as for `Plug.Static`: an OTP
    app (its `priv/static`), `{app, path}` or a directory path.
  * `:script` (required) - the bundled worker, relative to `:from`.
  * `:at` - the URL path of the worker, `"/sw.js"` by default. Served below
    the root, the response also allows the root scope with
    `Service-Worker-Allowed: /`.
  * `:only` and `:only_matching` - what to precache, as the options of the
    same names of `Plug.Static`: a URL is listed only when its first path
    segment is one of `:only` or starts with one of `:only_matching`.
    Everything by default. Give both plugs the same values: the worker
    fetches every listed URL when it installs, and one `Plug.Static` does
    not serve cannot be precached.

The worker is sent with `Cache-Control: no-cache`, so the browser checks it
on every update check. Only `GET` and `HEAD` requests for `:at` are
answered; every other request goes on through the pipeline. Before the
worker is built, `:at` answers 404.

After `mix phx.digest`, a file at the top of the static directory, such as
`favicon.ico`, also has a digested name, `favicon-<digest>.ico`, and pages
that use `~p"/favicon.ico"` ask for that one. `Plug.Static` serves it only
when `:only_matching` matches it (`only_matching: ~w(favicon)`): `:only`
compares the whole first segment. A name the filters leave out although the
other name of its file is listed is logged once per version with
`Logger.warning/1`, naming the file and the option that would serve it.

# `manifest`

```elixir
@spec manifest(Path.t(), keyword()) :: %{version: String.t(), precache: [String.t()]}
```

The cache version and the precache list of the static directory `static_dir`.

After `mix phx.digest`, it reads `cache_manifest.json`: the precache list
holds both names of every entry, the digested one (which pages ask for) and
the plain one (which code may ask for: a worker script, a lazily loaded
chunk; `mix phx.digest` keeps the originals and `Plug.Static` serves them),
and the version is derived from the digested names, so it changes with any
digest. Without a cache manifest (development), it lists the files of the
directory and derives the version from their paths, sizes and modification
times. Source maps and gzip copies are left out in both cases.

Options: `:only` and `:only_matching` (which URLs `Plug.Static` serves, with
the meaning they have there: each name is listed only when its first path
segment is in `:only` or starts with one of `:only_matching`), and
`:except` (paths to leave out, relative to `static_dir` and undigested, such
as the worker itself; both names of such a file are left out). For example,
`manifest("priv/static", except: ["assets/js/sw.js"])` may return
`%{version: "3f1c0b2a9d4e5f60", precache: ["/assets/css/app.css", "/assets/js/app.js"]}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
