Svelte on Rails

Combining two beautiful worlds: Rails backend power and Svelte frontend joy.

For most interfaces, Hotwire is a great choice. When you need richer interactivity, Svelte is the perfect lightweight layer on top of your Rails app. You keep a classic, backend-powered Rails app, with backend-driven tests, and add rich interactivity exactly where you need it.

There are two hurdles to understand:

  1. Data from Rails: translations and other Rails data must be passed to components as props. The to_svelte helper makes this easy.
  2. Server-side rendering: components must be SSR-compatible. Browser-only APIs such as window or document (also in directly imported modules) must not run during SSR. Code that runs later, e.g. after a button click, is fine. Simple test: reload the page. If you see no blinking error message, you are good to go.

Installation

# optional, if you start from scratch:
rails new svelte-on-rails-testapp --skip-javascript && cd svelte-on-rails-testapp

bundle add svelte-on-rails
rails g svelte_on_rails:install

# optional, a full-feature example:
rails g svelte_on_rails:showcase

The installer is careful: it checks everything in a sandbox first and asks before changing files. Just follow the prompts. Details: Installation. Then restart your app and open localhost:3000/svelte_on_rails_showcase.

On models where you want to use to_svelte, add exposes_to_svelte:

class User < ApplicationRecord
  exposes_to_svelte
end

Code Example

View:

<%= svelte 'components/User', user: @user.to_svelte(:login_count, :name) %>

Component app/frontend/javascript/components/User.svelte:

<script>
  let { user } = $props();
</script>

<h1>{ user._schema.self.name } { user.name } { user._schema.login_count.label }: { user.login_count }</h1>

Output:

<h1>Benutzer Josef Logins: 77</h1>

to_svelte serializes the data and adds translated model and attribute names (_schema).

Server log, on the «Completed» line:

17:35:52 web.1  |   [SOR] User.svelte returned from cache (1.4ms)
17:35:52 web.1  | Completed 200 OK in 43ms ( ... | Svelte: 2 cached, 1 rendered, 3.3ms)
# or
12:36:45 web.1  | Completed 200 OK in 831ms ( ... | SVELTE BUILD FAILED!)

On «SVELTE BUILD FAILED», the log shows a detailed error message with links to the files.

Before You Build Your First App

  • Performance:
    • Add the redis gem and set use_caching: true in config/svelte_on_rails.yml.
    • Use watch_changes: true in development only.
    • Read Caching and Props.
  • Testing:
    • We use this Playwright setup.
    • Important: after hydration, the gem adds the class hydrated to the wrapping element (class svelte-component). Use it in your selectors, so tests don’t click before the event listeners are attached:
      page.wait_for_selector(".svelte-component.hydrated .any-button").click
      

      This matches the .stimulus-connected class of @csedl/hotwire-svelte-helpers.

  • Dropdowns, tooltips, modals: see the Hotwire Svelte Helpers Example App. It includes form helpers that interact with the Rails backend, and solves z-index issues by rendering into a root-level container.
  • Components within app/views, just like partials: see setup.

Why?

Svelte’s reactivity is the missing piece for Rails + Hotwire.

Why was Svelte invented? Watch the Rethinking Reactivity video (especially 5:00–7:10).

Why this gem? Read the Motivation.

Features

  • Server-side rendering, with an SSR server by default (similar to react_on_rails pro)
  • Handy, fine-grained fragment caching, see Caching and Props
  • ActiveRecord integration: pass models directly with .to_svelte
  • Optional support for .svelte components directly in app/views
  • Plays nicely with Hotwire Turbo (SSR only on initial page load)
  • Fully integrated with rails assets:precompile
  • Built-in server → client communication via ActionCable
  • Fully configurable
  • Detailed logging for performance optimization and error detection

Enjoy it! 😉

For questions or issues, please open an issue on GitLab.

Best Regards,

Chris