Overview:
LiveSvelte is a library that combines the power of Phoenix LiveView and Svelte to enable easy client-side state management and seamless end-to-end reactivity in server-rendered HTML. By leveraging Phoenix LiveView’s websocket communication, LiveSvelte allows for real-time updates to the DOM without the need to write client-side JavaScript code. This library also provides support for server-side rendering (SSR) using Node.js and offers features such as Sigil as an alternative LiveView DSL, Svelte preprocessing support, Tailwind CSS integration, and more.
Features:
- End-To-End Reactivity with LiveView: LiveSvelte enables seamless reactivity in server-rendered HTML by leveraging Phoenix LiveView’s websocket communication.
- Server-Side Rendered (SSR) Svelte: LiveSvelte supports server-side rendering of Svelte components, providing enhanced performance and SEO benefits.
- Sigil as an Alternative LiveView DSL: LiveSvelte introduces Sigil as an alternative DSL for LiveView, offering a more concise and expressive syntax for building reactive web applications.
- Svelte Preprocessing Support with svelte-preprocess: With LiveSvelte, you can take advantage of Svelte’s preprocessing capabilities using svelte-preprocess, allowing for features like TypeScript integration, CSS preprocessing, and more.
- Tailwind Support: LiveSvelte seamlessly integrates with the popular Tailwind CSS framework, making it easy to style and design your LiveView components using Tailwind’s utility classes.
- Dead View Support: LiveSvelte provides support for Dead Views, allowing you to remove unnecessary server-side rendering for specific components or pages.
- live_json Support: LiveSvelte includes support for the live_json function, which allows for convenient manipulation and update of JSON data directly in the LiveView component.
- Slot Interoperability (Experimental): LiveSvelte offers experimental support for Slot Interoperability, allowing you to use Svelte components within LiveView’s slots for enhanced modularity and reusability.
Installation:
To install LiveSvelte, follow these steps:
- Add
live_svelteto the list of dependencies in yourmix.exsfile:
defp deps do
[
...
{:live_svelte, "~> 0.5"},
...
]
end
- Adjust the
setupandassets.deployaliases in yourmix.exsfile:
defp setup do
[
{:commands, ["app.setup", "assets.link", "ecto.setup", "live_svelte.setup"]},
{:atomic, true}
]
end
defp assets.deploy do
[
{:copy_static_ass"
4. Add the `import LiveSvelte` statement in the `html_helpers/0` function inside `/lib/<app_name>_web.ex`:
```elixir
def html_helpers do
quote do
...
import LiveSvelte
...
end
end
(Optional) For Tailwind support, add
"./svelte/**/*.svelte"to thecontentarray in thetailwind.config.jsfile.Remove the esbuild configuration from the
config/config.exsfile and remove theesbuilddependency from thedepsfunction in yourmix.exsfile.
Summary:
LiveSvelte is a powerful library that combines the benefits of Phoenix LiveView and Svelte to create reactive and dynamic user interfaces in server-rendered HTML. With seamless end-to-end reactivity, server-side rendering support, and a range of other features such as Sigil DSL, Svelte preprocessing, and Tailwind integration, LiveSvelte offers a comprehensive solution for building interactive web applications. The easy installation process and compatibility with Phoenix LiveView make LiveSvelte a convenient choice for developers looking to enhance their LiveView projects with client-side state management and advanced frontend capabilities.