←back to thread

Svelte 5 Released

(www.npmjs.com)
390 points begoon | 5 comments | | HN request time: 0.409s | source
1. sureglymop ◴[] No.41893027[source]
When I recently looked at SvelteKit, what confused me the most was the universal load function.

If you use "if !browser" with sveltes browser object, the things inside that branch will only run on the server.

But because svelte is compiled, these functions get compiled to two different versions, one for the server and one for the client, in which that if statement has been completely compiled out. That in itself isn't bad but it's weird that normal code and control flow influences compilation. In other languages this would be a macro or something that clearly expresses that it's metaprogramming.

Then there are some other weird things like props being merged and potentially overwriting each other in a deep level. But that is by design and can be reasoned about.

What also felt weird to me is that there is no recommended way to do initialization logic such as reading in a config, reading from the environment and preparing something for the rest of the lifetime of the application.

Has anyone had similar experiences/confusions when learning SvelteKit?

replies(2): >>41893139 #>>41893882 #
2. delanyoyoko ◴[] No.41893139[source]
Why will I use !browser, when I simply move that code into .server endpoint (or file)?
replies(1): >>41894714 #
3. Cloudef ◴[] No.41893882[source]
> but it's weird that normal code and control flow influences compilation. In other languages this would be a macro or something that clearly expresses that it's metaprogramming.

Similar kind of thing happens in zig during comptime execution.

replies(1): >>41894731 #
4. sureglymop ◴[] No.41894714[source]
Because then you are doing a different thing than I did there.

The universal load function specifically not only injects the data on the server before serving the page but then also runs on the client once the page is loaded.

In my specific case the client side version hit a different api, hosted in a different region. There are different use cases for this though.

5. sureglymop ◴[] No.41894731[source]
It's all acceptable if it at least is documented explicitly and correctly. But I do think sveltekits documentation is a bit sparse and doesn't go into detail about such things.

This is what I would call "magic" or implicit behavior. It seems very nice to have that at first but in reality after significant use of something there will come a point at which one wants to fully understand how it works. The magic then needs to be demystified.