keen_phoenix_svelte
π Live demo & docs: keen-phoenix-svelte.keenmate.dev Β· Hex Β· HexDocs
Auto-mount compiled Svelte apps into Phoenix β on both LiveView and plain controller-rendered pages β as self-contained islands.
Write this in a template:
<.svelte name="like" id={"like-#{@id}"} props={%{id: @id, liked: @liked}} />
β¦and the compiled Svelte app in assets/apps/like/ is mounted into that element,
kept in sync with server state, given a context/api/channel/live bridge to
the server (plus a bus for island-to-island messaging), and torn down on
navigation β no manual <script>/<link> wiring. A configurable placeholder
shows while the bundle loads (server-wide default, per-app <:placeholder>
override), so there's no flash of empty container.
Svelte is the first-class, tooled path, but the mount boundary is
framework-neutral β an island's entry just default-exports
(target, opts) => { setProps, destroy }. Because every island mounts through
that one contract, there's a single component, <.app> (an optional
framework="β¦" attribute just tags data-framework for debugging). The demo
mounts Svelte, Lit, React and vanilla-JS islands through it. <.svelte> remains
as a back-compat alias.
New here, or comparing this to
live_svelte? Start with Philosophy & comparison β the autonomous-island model, what this deliberately doesn't do, and how it differs fromlive_svelte.
What's New in v1.0.0-rc.3
- Proxy cache with revalidation β
:proxymode now works for unversioned upstreams β Registered proxied bundles are cached in ETS and revalidated rather than pinned forever: a stale entry triggers a single-flight conditionalGET(If-None-Match/If-Modified-Since), so a304keeps the bytes and a200swaps them, with concurrent requests collapsed into one upstream fetch. Freshness follows the upstream'sCache-Control/ETag, falling back to a:ttl(default 5 min) β so a barecdn/app.jswith no version in its URL is re-checked on a cadence instead of cached for a year. - End-to-end conditional-request chain β The proxy forwards an
ETag(synthesizing a weak one when the origin ships none) and answers the browser'sIf-None-Matchwith a304, so browser β Phoenix β origin all revalidate cheaply. This replaces the old fixed 1-yearimmutableheader; a per-appimmutable: trueopts truly-versioned URLs back into it. - Bounded proxy memory β Cache entries upsert by URL (refreshes never grow the table) and a periodic sweep evicts URLs no longer in the registry, so a rotating DB-driven app registry stays bounded to its working set.
- Renamed to speak "app", not "island" β
KeenPhoenixSvelte.IslandProxyβKeenPhoenixSvelte.Apps.Proxy, config:island_providerβ:app_provider(which now also accepts a 2-arity conditional-fetch form), and the proxy-path default/keen-islandsβ/apps(it shares thebase_pathprefix). Update your routerforward. - Package metadata / links β corrected the
:source_urlcasing, addedhomepage_url, and aWebsitelink to the live demo.
What's New in v1.0.0-rc.2
- Framework-neutral
<.app>β one component mounts any island β Every island mounts through the same(target, opts) => { setProps, destroy }contract, so there is now a singleapp/1component regardless of whether the bundle is Svelte, Lit, React, or hand-written vanilla JS. An optionalframeworkattribute just emits an informationaldata-frameworktag.<.svelte>remains as a back-compat alias for the rc.1 name. - Placeholder / loader β no flash of empty container β
<.app>renders a placeholder that the client clears the instant the bundle mounts (after the fetch, so it stays visible the whole time). Resolution is a per-call<:placeholder>slot βΊ the server-wideconfig :keen_phoenix_svelte, :placeholderβΊ a built-in, dependency-free skeleton. The server default accepts an HTML string, a function,{mod, fun}, orfalseto disable. - Event bus β island-to-island messaging without the server β A
busjoins the app boundary: a page-wide, client-side pub/sub (emit/on/once) over a DOMEventTarget, wired into both the LiveView hook andmountStatic(), so independent islands coordinate without a round-trip and without importing each other. - External apps β a registry with
:direct/:proxydelivery βKeenPhoenixSvelte.Appsregisters islands whose bundle lives elsewhere (a CDN, another deploy);<.runtime>emits aname β urlmanifest andAppsManager.resolve/1loads them. Pick:direct(browser imports the CDN URL) or:proxy(Phoenix fetches and re-serves same-origin viaKeenPhoenixSvelte.Apps.Proxyβ no CORS, CSP'self'). - Example reworked into "KeenSpace" β the demo is now a Teams-style workspace that doubles as reference code: chat over channel + Presence, a video catalogue over the REST helper, a calendar over
context.tokens, a bus-driven activity toast, simulated i18n, a plain (non-LiveView) route, and Lit/React/vanilla islands alongside the Svelte ones.
How it works
Two cooperating halves:
- Elixir β
<.app>renders a hook-bound<div>(phx-update="ignore",data-app, JSONdata-props);<KeenPhoenixSvelte.runtime>emits the page context. - JS β the
KeenSveltehook mounts the island inside a LiveView andAppsManagerlazilyimport()s/apps/<name>/main.mjs(or a registered URL);mountStatic()mounts islands on plain pages. Only the bundles on a page are fetched β and any framework's bundle mounts through the same(target, opts) => { setProps, destroy }contract.
phx-update="ignore" keeps LiveView out of the Svelte-owned subtree; the hook
drives mount / prop-update / teardown across live navigation.
Unlike live_svelte, this is the island model β no ~V sigil, no
server-rendered slots, no SSR Node runtime.
Install
{:keen_phoenix_svelte, "~> 1.0"}
Plus the npm package @keenmate/phoenix_svelte and a Svelte/Vite toolchain. Full
steps in Installation & setup.
Documentation
- Installation & setup β wire the library into your app
- Authoring apps β folder layout, the mount contract, Svelte 5 & 4
- Server communication β runtime context,
live,api,channel,bus - External apps (CDN, direct vs proxy) β load islands from elsewhere, and the
:direct/:proxydelivery modes
A complete, runnable demo (the like app on a LiveView route and a plain route,
plus a channel) lives in the
example/ app.
Versioning
keen_phoenix_svelte is a dual package: the Hex library keen_phoenix_svelte
and the npm package @keenmate/phoenix_svelte are released in lockstep at the
same version β install matching versions of both. The Elixir side renders the
component + hook wiring; the npm side supplies the client runtime (the KeenSvelte
hook, AppsManager, the api/channel helpers, and the Vite build helper).
License
MIT.