fix: track SvelteDate snapshots in reactions (#18700)

Read the Date value through `getTime()` before cloning it to get reactive updates from `SvelteDate`.

Closes #18698

---------

Co-authored-by: svelte-triage-bot <team@svelte.com>
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
pull/17308/merge
svelte-triage-bot[bot] 11 hours ago committed by GitHub
parent 4b61851ec8
commit 721244ad3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: track SvelteDate snapshots in reactions

@ -105,6 +105,8 @@ function clone(value, cloned, path, paths, original = null, no_tojson = false) {
}
if (value instanceof Date) {
// Ensure SvelteDate snapshots are tracked
value.getTime();
return /** @type {Snapshot<T>} */ (structuredClone(value));
}

@ -0,0 +1,13 @@
import { test } from '../../test';
import { tick } from 'svelte';
export default test({
async test({ assert, target }) {
assert.htmlEqual(target.innerHTML, '<button>update</button><p>1970-01-01T00:00:00.000Z</p>');
target.querySelector('button')?.click();
await tick();
assert.htmlEqual(target.innerHTML, '<button>update</button><p>1970-01-02T00:00:00.000Z</p>');
}
});

@ -0,0 +1,9 @@
<script>
import { SvelteDate } from 'svelte/reactivity';
const date = new SvelteDate(0);
const snapshot = $derived($state.snapshot(date));
</script>
<button onclick={() => date.setTime(86400000)}>update</button>
<p>{snapshot.toISOString()}</p>
Loading…
Cancel
Save