I love Svelte, Kit not necessarily.
It's nice but a bit too magic for me. I wrote about this before but I find it weird to write a load function that gets transpiled into a server and a client version and having to use `browser` to check if running in a browser context. Well it's not necessarily weird but it is too magic for me, e.g. an if statement checking this would get compiled away iirc.
On the other hand, I've been trying out astro with svelte and it's been super nice, especially with the ability to have runes in other typescript files.
Vite does statically replace the `browser` variable with `true` or `false` based on whether you're on the client or server, but this shouldn't affect the correctness of your code. It does allow Vite to remove any unused code, however. E.g. `if (browser)` turns into `if (false)` on the server and any code within that block can be removed. This can avoid shipping unused code to the client.
Astro is also built in top of Vite and the same thing happens there. If you reference `import.meta.env.SSR` it is statically replaced during build and unused code is tree-shaken out: https://vite.dev/guide/env-and-mode#env-variables
Thank you for the explanation, that cleared it up a lot for me. In astro I didn't yet use SSR, I will try this out as well.
I do still believe that it may be good to point this out in the docs more thoroughly. In general though, couldn't there be some situations where using a universal load function like this may increase the chance for some security critical logic bugs?
You should only access credentials in `.server` files. We have a built-in feature that checks that you only access credentials in `.server` files to try to prevent newcomers from accidentally making any mistake in that regard.
I’ve been a backend and data engineer who got my start in full stack. I’m working on a project and selected Svelte for the front end after trying to learn React. I agree 100%. Svelte is great. Even 5 with its warts isn’t as bad as react.
Kit on the other hand has been a major pain. You won’t need a webserver oh wait you do! You have to make drastic tradeoffs when choosing between full SPA and SSR render only. Server side pre-rendering seems like middle ground nightmare that I just I don’t think I can tackle sufficiently right now. I need to get an MVP.
The (browser) issue is a symptom of this greater problem - it’s just not clear what runs on the server side and what doesn’t. The documentation is very poor on this and blogs are flat out wrong.
Files with `.server` in the name run only on the server. The remaining files run on the client and server. This is mentioned in the introduction on https://svelte.dev/docs/kit/routing
What's confusing about this and what could we do to help?
It's all kinda hidden. The documentation steers you towards SSR or assumes it. It appears as though you can't have client side routing without a roundtrip to the server. Take a looked at the "page" documentation immediately under Routing.
How do I serve svelte files using a python or golang backend and still have client side routing? These should have a fairly straightforward answer but I don't think they do.
By default, SvelteKit does SSR for the first page and client-side routing thereafter. This is fully configurable. Perhaps it's worth an additional mention on the routing page. I'll take a look later. Thanks for the suggestion.
On the other hand, I've been trying out astro with svelte and it's been super nice, especially with the ability to have runes in other typescript files.