DEV Community

Matthias Andrasch
Matthias Andrasch

Posted on

To API or not to API... Svelte and InertiaJS (Laravel / PHP)

In the last months I experimented with SvelteKit and GraphQL. For example I connected a SvelteKit frontend with WordPress (WPGraphQL plugin), ran a quick test run with Laravel Lighthouse, used Supabase, Strapi and Directus for small demos. While all these approaches have their individual strengths, I always felt like "Couldn't this be simpler for smaller projects?". Especially for solo developers or sideprojects setting up and managing the API-backend can be complex (or pricy).

Therefore I was really amazed to stumble about Inertia recently, a glue between Laravel (PHP) and Modern JS Frontend Frameworks (React, Vue, Svelte). One of the creators, Jonathan Reinink, explains here why he created it (24:55):

So InertiaJS offers a solution for the painpoint of having to manage a backend api project and a frontend project separately. And it supports Svelte - awesome!

I was first scared off by the tagline "The modern monolith", because I thought it was meant to be for bigger teams or in enterprise environments. But this was a misconception:

Inertia was designed for development teams who typically build server-side rendered applications using frameworks like Laravel, Ruby on Rails or Django. They create controllers, get data from the database (via an ORM), and render views.

But what happens when these developers want to replace their server-side rendered views with a modern JavaScript-based single-page app front-end? The answer is always "you need to build an API". Because that's how modern SPAs are built.

This means building a REST or GraphQL API. It means figuring out auth for that API. It means client-side state management. It means setting up a new Git repo. It means setting up another hosting account for the API. And this list goes on. It's a complete paradigm shift.

Inertia allows you to build a fully JavaScript-based single-page app without all this added complexity.

Source: https://inertiajs.com/who-is-it-for

So currently I set the goal for myself to learn Inertia via https://laracasts.com/series/build-modern-laravel-apps-using-inertia-js and also maybe connect it to twill cms.

Another cool aspect is that Inertia added the Server Side Rendering feature lately (https://inertiajs.com/server-side-rendering), although support for Svelte is still in development.

(Btw: If you're not into modern javascript frameworks, Laravel Livewire could be interesting for you.)

Let's see how my journey with Inertia works out.

Are you already using Svelte and Inertia in your projects? Let me know in the comments, would love to hear about your experiences!

Resources:

Top comments (0)