Skip to content

Commit 370beb4

Browse files
better
1 parent 08b502b commit 370beb4

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

packages/svelte/src/internal/server/hydratable.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,28 @@ function encode(key, value, unresolved) {
5757

5858
entry.serialized = devalue.uneval(entry.value, (value, uneval) => {
5959
if (is_promise(value)) {
60+
// we serialize promises as `"${i}"`, because it's impossible for that string
61+
// to occur 'naturally' (since the quote marks would have to be escaped)
62+
// this placeholder is returned synchronously from `uneval`, which includes it in the
63+
// serialized string. Later (at least one microtask from now), when `p.then` runs, it'll
64+
// be replaced.
65+
const placeholder = `"${uid++}"`;
6066
const p = value
61-
.then((v) => `r(${uneval(v)})`)
67+
.then((v) => {
68+
entry.serialized = entry.serialized.replace(placeholder, `r(${uneval(v)})`);
69+
})
6270
.catch((devalue_error) =>
6371
e.hydratable_serialization_failed(
6472
key,
6573
serialization_stack(entry.stack, devalue_error?.stack)
6674
)
6775
);
6876

69-
// prevent unhandled rejections from crashing the server
70-
p.catch(() => {});
71-
72-
// track which promises are still resolving when render is complete
7377
unresolved?.set(p, key);
74-
p.finally(() => unresolved?.delete(p));
75-
76-
// we serialize promises as `"${i}"`, because it's impossible for that string
77-
// to occur 'naturally' (since the quote marks would have to be escaped)
78-
const placeholder = `"${uid++}"`;
79-
80-
(entry.promises ??= []).push(
81-
p
82-
.then((s) => {
83-
entry.serialized = entry.serialized.replace(placeholder, s);
84-
})
85-
.catch(() => {})
86-
);
78+
// prevent unhandled rejections from crashing the server, track which promises are still resolving when render is complete
79+
p.catch(() => {}).finally(() => unresolved?.delete(p));
8780

81+
(entry.promises ??= []).push(p);
8882
return placeholder;
8983
}
9084
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
mode: ['async'],
5+
error: 'hydratable_serialization_failed'
6+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
import { hydratable } from 'svelte';
3+
4+
hydratable('key', () => new Promise(() => { throw new Error('nope') }));
5+
</script>

0 commit comments

Comments
 (0)