The big new release of Inertia comes with server-side rendering support for Svelte, among other improvements. However, Svelte is a second-class citizen in the documentation. There are first-party versions of Laravel Breeze written in Vue and React that make it easy to get up and running with Inertia. Svelte users are not so lucky.
Installing Inertia
Start by installing Inertia’s Laravel adapter if you haven’t already.
Create your app’s base template at resources/views/app.blade.php
.
Publish Inertia’s middleware.
Add the middleware as the last item in the the web
middleware group in app/Http/Kernel.php
.
Installing Svelte
Install a few packages, including Svelte itself (this isn’t mentioned at all in the documentation).
Create the resources/js/app.js
file required by Inertia.
Make the following changes to vite.config.js
:
- Change
laravel(
tolaravel.default(
- Add
svelte()
to the array, after Laravel
Add "type": "module"
to your package.json
file
If you don’t make this change, you might encounter this error:
Failed to resolve entry for package “@sveltejs/vite-plugin-svelte”. The package may have incorrect main/module/exports specified in its package.json: No known conditions for ”.” entry in “@sveltejs/vite-plugin-svelte” package
Bonus: Adding Tailwind
Add @vite('resources/css/app.css')
to app.blade.php
.
Install Tailwind and its dependencies.
Edit tailwind.config.cjs
.
Add the three Tailwind directives to resources/css/app.css
.
Bonus: Adding Typescript
Install a few packages.
Edit vite.config.js
and add this import.
Add the following object as the first parameter to svelte()
.
Create tsconfig.json
with the following contents.
Now, you can add lang="ts"
to any Svelte <script>
tag.