Experience With Tauri + SvelteKit #6423
Replies: 10 comments 20 replies
-
A few thoughts from looking through the steps we have in the guides: https://tauri.app/v1/guides/getting-started/setup/sveltekit SvelteKit and Tauri can work well together, but getting them setup can involve a few manual steps:
export const prerender = true
export const ssr = false These aren't necessarily difficult steps, but knowing the difference between SPA, SSG, SSR and what you can/can't do between each can make it tough to get started as a beginner |
Beta Was this translation helpful? Give feedback.
-
I've made one static website with SvelteKit and figured the knowledge would transfer over to Tauri. For the most part, that was true, but there are some bumps I've encountered. One thing that confused me at the beginning was Content Security Policy configuration. SvelteKit lets you do this via On to the config itself: // svelte.config.js
import adapter from '@sveltejs/adapter-static';
import preprocess from 'svelte-preprocess';
/** @type {import('@sveltejs/kit').Config} */
const config = {
preprocess: preprocess(),
kit: {
adapter: adapter(),
csp: {
mode: 'hash',
directives: {
'default-src': ['none'],
'img-src': ['self'],
'script-src': ['self'],
'style-src': ['self', 'unsafe-inline'],
}
}
}
};
export default config; I use a restrictive-by-default CSP. ( 'style-src': ['self', 'unsafe-inline'],
+ // Vite uses WebSockets for HMR, so WebSocket connections to localhost:5173 are whitelisted during development
+ 'connect-src': process.env.TAURI_DEBUG === 'true' ? ['ws://localhost:5173'] : undefined I didn't realize that browsers and Tauri handled WebSocket connections differently. This last point is not necessarily specific to SvelteKit and is more of a Vite thing, but I think it's worth mentioning. I encountered a bug in my app where certain images would not show in release builds, but were visible in debug builds. I think the cause goes like this: by default, Vite inlines images smaller than 4KiB as base64 during release builds (Vite docs). Then, Tauri does something with the CSP that blocks these assets.
I resolved this issue by disabling asset inlining in the Vite config file ( build: {
// The Windows webview (WebView2) is supposed to update itself automatically, so it's safe to target the latest 3 versions (i.e. Blink 108).
// The macOS webview (WebKit) only updates with new OS versions, so the build target is more conservative; the earliest Safari version for macOS Big Sur is 14 (i.e. WebKit 610.2.11).
// The Linux webview (WebKitGTK) is based on WebKit, so the target is the same as macOS.
// Reference: https://discord.com/channels/616186924390023171/1082055425907753063
target: process.env.TAURI_PLATFORM == 'windows' ? 'edge108' : ['es2021', 'safari14'],
+ // By default, Vite inlines assets smaller than 4 KiB as base64 (see https://vitejs.dev/config/build-options.html#build-assetsinlinelimit).
+ // These assets are blocked by the CSP in production builds, so inlining is explicitly disabled.
+ assetsInlineLimit: 0
}, I would say that my experience with Tauri and SvelteKit has been positive. For the most part, it *just works*. There are just some small things to be aware of. |
Beta Was this translation helpful? Give feedback.
-
If you have a single route (not single page) application, I would suggest that the most straightforward option would be to use Svelte by itself. If you need routing, SvelteKit seems like the obvious choice. There are 3rd party routers available (and have been for a long time) but SPA mode is supported in SvelteKit with very few configuration changes. As it is supported by Svelte itself, it makes sense to me to go with SvelteKit in this case. That is what I have done for my current Tauri project. |
Beta Was this translation helpful? Give feedback.
-
Thanks for all of the insights! @FabianLars @probablykasper and I have been doing some research into this. Putting our notes so far below: Recommended Setup (SPA)Starting out with SPA mode by using Advanced Setup (SSG with prerendering)Once you know your way around SvelteKit you can start enabling prerendering to improve First Contentful/Meaningful Paint metrics. This can be applied per route or globally if set in the root Considerations
|
Beta Was this translation helpful? Give feedback.
-
I've used Sveltekit for ~10 projects and used it with Tauri for 2 or 3, I think it works pretty well! The pair is currently my favorite way to make any desktop application and I can't wait for the rest of mobile support to be ready so I can stop making PWAs for my mobile needs! Sveltekit has first class support for ssg as others have mentioned, however it is not quite as great an experience as the other modes, especially when it comes to form submission and the like. Most benefits of SSR over SSG really do go away significantly when in an application context like this, the bundle is served off disk and so the loading time is negligable, plus the application itself routinely takes 10 or more times longer to start up than the page takes to load, so this is not the end of the world. However, the UX would still be a good 20% better if we had SSR in Tauri! I believe the next best step forward would be a Sveltekit adapter for Tauri - one that gives Tauri first class support running the SSR code in the core process or something and serving the rendered files in such a way that developers can keep the full Sveltekit feature suite with no compromises. Sveltekit and Tauri already share a number of goals with regard to performance and bundle size, and I'd love it if they worked together even better! |
Beta Was this translation helpful? Give feedback.
-
Chiming in here about something that doesn't work. Using SvelteKit and have a store that is using Tauri's If I use a load function that doesn't use the tauri Happy to expand or provide code, but it's one limitation of SvelteKit + Tauri I ran into today and wanted to share in case it's helpful. Thank you! |
Beta Was this translation helpful? Give feedback.
-
My experience in two words: Thank You! Deep and heartfelt. This scratches an itch I've had for decades. Mind if I share my success story to date? I was a developer when I was young, and the world was new. I stuck to web development because GUI was a PITA in Perl or other languages, and I've never been one for systems languages. There have been a few things I wanted to GUI (side projects), but Electron was bloated, and with rusty developer skills learning React or Vue was a non-starter. I did not have time to learn Rust in depth. But I knew Tauri + SvelteKit (T+SK) had promise. I tried with T+SK a while back, but it's hard to pour knowledge into a rock. I write / publish SciFi (side gig), and I enjoy journaling my progress (word counts, mood). My editor keeps the daily wordcount history for me, so I've had scripts that updated my merged journal and created a contribution map (like GHs). Other authors have asked me for my app, but I couldn't get it into their hands. Enter T+SK, attempt two (electric boogaloo). I used GPT 4 to bridge my knowledge gap. It taught me how to properly configure the Menu, message between the two layers, and debug functions I wrote in Rust. The Dev version ran fine. I ran into problems with the Prod mode (404 error). I went back to the S+TK Getting Started. Well documented, even for a guy like me who hates RTFM. I found my problem (I had I could have benefited from some better code examples. GPT4 was current enough to get me close enough, so I could get the rest of the way there:
|
Beta Was this translation helpful? Give feedback.
-
Trying to follow the quickstart guide here and I've completed all the steps (perhaps incorrectly?) but
I'm guessing it's because
I'm sure this is a simple glitch with an easy fix, but this is my first experience with Tauri and it's disappointing that the steps in the docs don't appear to produce a working Hello World app in my case. |
Beta Was this translation helpful? Give feedback.
-
I made a reusable template back when I was looking into this as well https://github.com/auros-one/tauri-sveltekit Got to the same conclusions as @lorenzolewis' #6423 (comment) except I didn't come in contact with CSP as I use it for a 100% local / offline app. Happy to collab on a PR if there's anything missing from the tauri-sveltekit template for your usecase |
Beta Was this translation helpful? Give feedback.
-
Hi there! We're building a PWA with SvelteKit and have been looking into Tauri as possible future step to be able to improve our distribution. I had a couple of questions that maybe someone in the community can help us with. But first, some context, our app is meant to run offline and heavily depends on service workers and indexeddb (via Dexie.js). The service worker will always start from the app root, which is located in In this sense, the questions we have are:
Any help or comment is greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Recently there's been a lot of conversation around using SvelteKit and Tauri together. We want to know:
Let us know your feedback!
Beta Was this translation helpful? Give feedback.
All reactions