-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: document @html
and <img src>
hydration change
#12373
Conversation
Also add a test for it closes #12333
|
packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js
Outdated
Show resolved
Hide resolved
sites/svelte-5-preview/src/routes/docs/content/03-appendix/02-breaking-changes.md
Outdated
Show resolved
Hide resolved
onMount(async () => { | ||
// force update | ||
raw_html = ''; | ||
src = ''; | ||
await tick(); | ||
// set client values | ||
raw_html = '<h1>Client</h1>'; | ||
src = '/client.jpg'; | ||
}); | ||
</script> | ||
|
||
{@html raw_html} | ||
<img {src} /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another way we could tackle this, which is arguably a better practice since it scales to an arbitrary number of variables:
onMount(async () => { | |
// force update | |
raw_html = ''; | |
src = ''; | |
await tick(); | |
// set client values | |
raw_html = '<h1>Client</h1>'; | |
src = '/client.jpg'; | |
}); | |
</script> | |
{@html raw_html} | |
<img {src} /> | |
let mounted = $state(false); | |
$effect(() => { | |
// force `markup` and `src` to be re-evaluated | |
mounted = true; | |
}); | |
</script> | |
{@html (mounted, markup)} | |
<img src={(mounted, src)} /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work for @html
, because it checks against the previous value and does nothing if it's the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I realised it doesn't really make any sense anyway — if you're dealing with local state, you should just do this:
<script>
import { onMount, tick } from 'svelte';
let markup = $state('<h1>Server</h1>');
let src = $state('/server.jpg');
onMount(() => {
markup = '<h1>Client</h1>';
src = '/client.jpg';
});
</script>
So this only makes sense if you're dealing with props, in which case you don't know what values to set to. Something like this would make more sense:
<script>
let { markup, src } = $props();
if (typeof window !== 'undefined') {
// stash the values...
const initial = { markup, src };
// unset them...
markup = src = undefined;
$effect(() => {
// ...and reset after we've mounted
markup = initial.markup;
src = initial.src;
});
}
</script>
{@html markup}
<img {src} />
…breaking-changes.md Co-authored-by: Rich Harris <[email protected]>
this is kind of blocked on #12362, so I may rustle up a fix real quick |
actually I can't right now, will come back to it later if @paoloricciuti doesn't beat me to it |
Ugh didn't realized this was blocked...I'll try to do something now but please don't worry about me if you can squeeze the fix. |
all good — we could have just merged without mentioning the warning, but it seems better to consolidate |
Btw I shoot my shot with #12396 I hope I did everything correctly |
I saw, thanks — I gave you bad advice when I said 'On the server it means |
Uh gotcha, but also I don't think we can force |
IIUC it's the opposite — |
Also add a test for it
closes #12333
Dev-time check for
@html
will be part of #12362Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.Tests and linting
pnpm test
and lint the project withpnpm lint