tonydang

tonydang

Setting Up Phoenix with Inertia.js and Svelte :)

I recently got inertia-phoenix (an Inertia.js adapter for Phoenix) working with Svelte. Setting up the server-side rendering (SSR) with Svelte and esbuild was a little tricky so I made a repo with a detailed guide and example project so that I would remember all the steps for future projects.

https://github.com/tonydangblog/phoenix-inertia-svelte

Thought I’d share it here in case anyone else is trying to get the same setup going. :wink:

In addition to setting up Phoenix with Inertia and Svelte, the guide also includes a section for setting up several other JS tooling you may want when using Svelte, such as TypeScript, Prettier, ESLint, etc.

I’m definitely not an expert with esbuild setups, so please lmk if you see any mistakes or possible improvements! :folded_hands:

Most Liked

tonydang

tonydang

I think the answer depends on a lot of things. :sweat_smile:

First, do you have a complex frontend where using a framework like React, Vue, or Svelte would be helpful? If so, you’ll need to decide whether to keep your backend and frontend separate (e.g., Phoenix + Next.js/Nuxt/SvelteKit/Astro) or use a monolithic Phoenix setup.

If you’re sticking with a Phoenix monolith and still want to use a frontend framework, the current options that support SSR (server-side rendering of JS components) that I know of are LiveSvelte, LiveVue, and Inertia.js.

Now, for choosing between LiveSvelte/Vue and Inertia.js: I haven’t used LiveVue, so I can only speak about LiveSvelte and Inertia.js. LiveSvelte is specific to Svelte, while Inertia.js is framework agnostic and can be used with React, Vue, or Svelte. LiveSvelte integrates closely with LiveView, which means it includes all of LiveView’s features, like automatic socket connections and rendering based on socket state changes.

In contrast, Inertia.js doesn’t integrate with LiveView. Instead, it hands off the entire frontend to your chosen frontend framework. It provides SPA-like routing similar to LiveView’s live navigation but without using a socket. For real-time features, you would need to set up your own socket and channels.

I find LiveSvelte offers tighter integration, making the developer experience simpler overall. However, there are some known SSR performance issues and specific challenges (related to making LiveView work in an offline app) that led me to try Inertia.js.

The Elixir Inertia.js adapter has experimental SSR support, and while it sometimes crashes in development, it falls back to non-SSR if it fails. I haven’t encountered SSR issues in non-dev environments. Not integrating with LiveView also avoids some of the offline-related problems I faced.

Both have their pros and cons. I really like LiveSvelte, but Inertia.js solves the specific problems I have better at the moment. That said, I don’t think the issues I have with LiveView and offline functionality are unsolvable—I just haven’t dug into them further since I wanted to try out Inertia.js. Inertia.js isn’t perfect either, but so far, everything seems to just work where it needs to in my current project.

Sorry for the long-winded response! I hope this helps answer your question!

11
Post #7
tonydang

tonydang

Hi @jam — I actually just hacked together a hackathon app this week using SvelteKit + Phoenix Channels, and I think I like it more than using LiveSvelte for PWAs that need to work offline. Specifically, I used SvelteKit in SPA mode. During development, I set up a Vite proxy to talk to Phoenix. In production, the SPA is compiled into Phoenix’s static folder, so I only need to run the Phoenix server — no separate SvelteKit server required.

There are some tradeoffs with this approach, though. Mainly, there’s no SSR unless you prerender pages. But for a PWA with a service worker caching files for offline use, that’s not a big deal — and having the SvelteKit router is great.

You can make LiveSvelte work as an offline PWA, but there are a couple of downsides:

  1. Lack of client-side routing: Liveview has live navigation, but that only works when you’re online. If you want offline routing without page reload, you’ll need to build your own or use a third-party library, neither of which will likely be as full-featured as SvelteKit’s.
  2. CSRF token issues: For offline apps, you’ll need to cache your HTML, which ends up caching the CSRF token used for the LiveView socket connection. This makes reconnecting after being offline for a while tricky with LiveSvelte. I had a very hacky workaround in a previous app, but I wouldn’t recommend it.

In summary, I wouldn’t recommend LiveSvelte for PWAs that need offline capabilities. It’s possible, but right now I don’t think it’s as clean as just using channels.

cblavier

cblavier

Naive question: why would I pick inertia over liveview?
what would be the rationale?

Where Next?

Popular in Guides/Tuts Top

ben-pr-p
https://github.com/ben-pr-p/elixir-phoenix-parcel-example Hey all! I put together a starter-pack / instructions to set up Phoenix with t...
New
georgeguimaraes
Another cool plugin for Neovim, GitHub - jmbuhr/otter.nvim: Just ask an otter! 🦦 · GitHub makes it possible to run linters for embedded c...
New
AstonJ
With a few questions relating to this recently, I wonder if it might help having a thread where we share how we’ve set up Elixir/Erlang/P...
New
hauks96
Hello everyone, I created a deployment tutorial for Phoenix applications with Kubernetes (microk8s) a few months back with the goal of s...
New
crockwave
To integrate dropdown menus in a Phoenix Liveview app, you can use a combination of js, Hooks, CSS and your .leex and .ex code. You can...
New
pinksynth
Whenever tests have to interact with an Ecto.Repo, sometimes it’s necessary to test a few different branches or paths that data can take....
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
Zurga
In a quest to optimize the amount of data sent between the server and client I recently decided to try to use MessagePack instead of JSON...
New
kevinlang
Hey all, With Phoenix 1.6 just around the corner, I figured I’d make a tutorial on how to add Bulma to a new Phoenix 1.6 project. By lev...
New
dogweather
I just finished a long process of configuring and debugging a Docker Compose-based dev environment. Several late-night hours! I think I’v...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement