Experiences and Caveats of Svelte 5 Migration #14131
kvetoslavnovak
started this conversation in
General
Replies: 2 comments 6 replies
-
By the way: - let notext = $derived.by(() => {
- if (data.completeDoc == 'NoLangVersion') {
- return true;
- }
- if (data.completeDoc !== 'NoLangVersion') {
- return false;
- }
- });
+ let notext = $derived(data.completeDoc == 'NoLangVersion');
|
Beta Was this translation helpful? Give feedback.
1 reply
-
@brunnerh do you have any advice what is wrong with arrow function in derived rune?
Or to have example from real world what would be in your opinion the most clear way to migrate this code to Svelte 5: <script>
export let form;
let results = [];
let previousSearch = '';
let more;
let searchDoc;
let skip;
$: if (!!form) {
more = form.nextLength;
searchDoc = form.searchDoc;
skip = Number(form?.skip) + 20;
}
$: if (!!form?.searchResultFromAction) {
if (previousSearch == form.searchDoc && form.loadmore) {
results = [...results, ...form.searchResultFromAction];
} else {
results = [...form.searchResultFromAction];
previousSearch = form.searchDoc;
}
}
</script> |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have recently updated a rather complex web application. The application has features like auth, Stripe, i18n, dark/light mode, PWA, etc. Overall, it has around 30 pages and components, with almost no third-party npm packages.
I would like to point out what I found quite challenging when migrating the app to Svelte 5.
Auto-Migration Script Hammer
The auto-migration script provided by Svelte can do the job for you with this "one-liner" command in the terminal
npx sv migrate svelte-5
(after you do all the necessary updates and installs: "@sveltejs/vite-plugin-svelte": "^4.0.0" and "svelte": "^5"). But I do not recommend this "hammer" approach.Go file by file, component by component with Ctrl + Shift + P (Windows/Linux) / Shift + Command + P (Mac) and use the
Migrate Component to Svelte 5 Syntax
command in the VS Code command palette instead. You will have more control that way.Deprecated run() Surprise
The script cannot perform miracles. Upgrading reactive variable declarations to
$state()
is usually fine. However, the script may struggle to detect whether$:
should be converted to$derived()/$derived.by(() => {})
or$effect(() => {})
.So, guess what? With the auto-migration script, you might end up with lots of
run(() => {})
.For example, imagine as a simplified example using something like this:
The auto-migration script will give you this:
with a nice little warning that the run function is deprecated.
The better Svelte 5 code would be this I guess:
The reason is that the script cannot transform code to
$derived.by(() => {})
easily, so it would use a more dirty approach with$effect()
. But$effect()
runs only client-side, so the script uses the deprecatedrun
function instead.Avoid $effect If You Can
Now we are getting to the most important takeaway. Which is
$effect()
running only client-side. So no$effect()
on the server, for prerendering pages and SSR.$effect()
DOES NOT RUN ON THE SERVER!This should be really emphasized in the Svelte 5 documentation.
Look at this two examples:
They are not the same. This causes a lot of challenges. The client will need to reevaluate the c variable when mounting the page. The page will look different when sent from the server and when finally DOM-rendered on the client (SSR, SEO, flicker issues, etc.).
So always try to use $derived or
$derived.by(() => {})
over$effect()
. It will save you lots of trouble.It's quite the same story as when we were discouraged from using stores in SvelteKit and SSR.
$effect vs onMount() in SvelteKit
You might be tempted to replace your
onMount()
with$effect()
thanks to the examples that were given during the arrival of Svelte 5. For the reasons already mentioned, I would discourage this for the time being. onMount is still a a core Svelte lifecycle hook.$bindable $props Surprise
The other nice surprise is that Svelte 5 takes care to have consistent variable values. If you pass a variable as a prop to a component and change this variable in the component later on, the script will try to solve this inconsistency using
$bindable $prop
. The parent should be notified, so your app state is consistent.Look at this example:
The auto-migration script will want you to use a component with binded value to ensure the parent may get the updated value back:
We are mutating the
name
varaible in this child component. So we are notifing the parent so. The parent will use this mutated value as well.If you do not need the parent to reflect the mutated
name
value we can use quite simpler way as well, you guessed it, with$derived()
:But in this later case we are not mutating name variable in the component.
:global { } Block
A very nice feature that I found during migration was that we can use CSS
:global
with block now. Styling with:global
is quite necessary if you want to style the HTML elements in@html
, for example.So instead of this:
you can use this:
Style as a Prop in Components
In Svelte 4, if you wanted to provide a CSS class as a prop to a component, you would use
{$$props.class}
:In Svelte 5 you may use
class={className}
:Possible Lighthouse Perfomance Drop
When I used the auto-merging script, I was shocked at how my app's performance dropped. With Svelte 4, I had nearly all 100%s. It was only after I manually migrated and carefully considered how (mainly how to avoid
$effect()
if possible) that my Lighthouse scores were back in the green again.Final Words
It took longer to migrate to Svelte 5 than I had expected. I still have not pushed this new version to production, though. The updates to Svelte 5 are still coming in with quite high frequency.
I hope my experience may be useful to others.
You can read this post also at https://dev.to/kvetoslavnovak/experiences-and-caveats-of-svelte-5-migration-27cp
Beta Was this translation helpful? Give feedback.
All reactions