PhoenixAssets.Components (PhoenixAssets v0.1.0)

View Source

Phoenix function components for emitting Vite assets and mounting Svelte pages.

vite_assets/1 renders the correct <script>/<link> tags for an entry: in production it reads the manifest and emits the hashed entry script, its stylesheet links, and modulepreload hints for its transitive imports (with Subresource Integrity when the manifest carries it); in development it points at the Vite dev server (and its HMR client). svelte_page/1 renders the mount point a SvelteKit/Svelte client hydrates, serialising props as a data attribute.

Available only when Phoenix.Component is present (it is an optional dependency); apps that exclude phoenix_live_view do not get this module.

Why

Apps that let Phoenix render the HTML shell need a drop-in way to reference build output without hard-coding hashes or duplicating dev/prod logic per template.

Summary

Functions

Emits a Speculation Rules prefetch block for the app's page routes.

Renders the mount point for a Svelte page, serialising props for hydration.

Emits the script and stylesheet tags for a Vite entry.

Functions

speculation_rules(assigns)

@spec speculation_rules(map()) :: Phoenix.LiveView.Rendered.t()

Emits a Speculation Rules prefetch block for the app's page routes.

Reads the page routes from the asset graph (or takes them via :routes, e.g. from a PhoenixAssets.Graph.Compiled module, to avoid a graph read per render) and emits a <script type="speculationrules"> document-rules block. Progressive enhancement: browsers without the Speculation Rules API ignore the tag. Renders nothing when no routes are known.

Attributes

  • routes (:list) - Defaults to nil.
  • eagerness (:string) - Defaults to "moderate". Must be one of "conservative", "moderate", or "eager".
  • nonce (:string) - Defaults to nil.

svelte_page(assigns)

@spec svelte_page(map()) :: Phoenix.LiveView.Rendered.t()

Renders the mount point for a Svelte page, serialising props for hydration.

Props are encoded with the stdlib JSON module: plain maps, lists, and scalars work as-is; structs must derive or implement the JSON.Encoder protocol.

Attributes

  • name (:string) (required)
  • props (:map) - Defaults to %{}.
  • id (:string) - Defaults to nil.

vite_assets(assigns)

@spec vite_assets(map()) :: Phoenix.LiveView.Rendered.t()

Emits the script and stylesheet tags for a Vite entry.

In development emits the Vite HMR client and the entry as ES modules from the dev server; in production emits the hashed entry script, its stylesheet links, and modulepreload hints for its transitive imports from the manifest.

Attributes

  • entry (:string) (required)
  • nonce (:string) - Defaults to nil.