←back to thread

Svelte 5 Released

(www.npmjs.com)
390 points begoon | 1 comments | | HN request time: 0.202s | source
Show context
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 #
delanyoyoko ◴[] No.41893139[source]
Why will I use !browser, when I simply move that code into .server endpoint (or file)?
replies(1): >>41894714 #
1. 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.