PhoenixKitBoards.Web.RuntimeHooks (PhoenixKitBoards v0.4.2)

Copy Markdown View Source

Delivers this module's LiveView hooks without the host wiring anything.

A hook used to have to be in the host's LiveSocket at construction time, which is why js_sources/0 and core's :phoenix_kit_js_sources compiler exist. Since LiveView 1.1 that is no longer so: getHookDefinition/1 resolves a name lazily, and its last resort is a <script data-phx-runtime-hook="Name"> anywhere in the document, whose window.phx_hook_Name() returns the callbacks. LiveView re-creates such a script when it arrives in a patch, so this works on a board rendered over the socket rather than in the first HTML.

The host's own registration still wins — getHookDefinition checks liveSocket.hooks before it looks for a runtime hook — so a host that does run the compiler is unaffected, and there is never a second copy.

Why a shim rather than the bundle inline

Inlining would be simpler, but it puts 40 KB of uncacheable script in every board page. Instead each hook resolves to a small object that forwards to the real one once AssetController has served the bundle. The forwarding is what the shape of LiveView's API forces: getHookDefinition is synchronous, so something has to be returned immediately, before the bundle can possibly have loaded.

The load is shared by both hooks and by every element using them, so the bundle is requested once per page however many hooks mount.

Summary

Functions

Where the bundle is served from, digest included so it can be cached forever.

The runtime-hook scripts. Render before any element that uses them.

Functions

bundle_url()

Where the bundle is served from, digest included so it can be cached forever.

scripts(assigns)

The runtime-hook scripts. Render before any element that uses them.

Attributes

  • nonce (:string) - CSP nonce for the emitted scripts. Required only by hosts that enforce a Content-Security-Policy — without one those hosts would block the script and lose collaboration with nothing but a console warning to say why.

    Defaults to nil.