Skip to content

Commit 9c88438

Browse files
docs
1 parent 9af6013 commit 9c88438

File tree

2 files changed

+13
-56
lines changed

2 files changed

+13
-56
lines changed

documentation/docs/06-runtime/05-hydratable.md

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Hydratable data
33
---
44

5-
In Svelte, when you want to render asynchonous content data on the server, you can simply `await` it. This is great! However, it comes with a major pitall: when hydrating that content on the client, Svelte has to redo the asynchronous work, which blocks hydration for however long it takes:
5+
In Svelte, when you want to render asynchonous content data on the server, you can simply `await` it. This is great! However, it comes with a pitall: when hydrating that content on the client, Svelte has to redo the asynchronous work, which blocks hydration for however long it takes:
66

77
```svelte
88
<script>
@@ -17,7 +17,7 @@ In Svelte, when you want to render asynchonous content data on the server, you c
1717
<h1>{user.name}</h1>
1818
```
1919

20-
That's silly, though. If we've already done the hard work of getting the data on the server, we don't want to get it again during hydration on the client. `hydratable` is a low-level API build to solve this problem. You probably won't need this very often -- it will probably be used behind the scenes by whatever datafetching library you use. For example, it powers [remote functions in SvelteKit](/docs/kit/remote-functions).
20+
That's silly, though. If we've already done the hard work of getting the data on the server, we don't want to get it again during hydration on the client. `hydratable` is a low-level API built to solve this problem. You probably won't need this very often -- it will be used behind the scenes by whatever datafetching library you use. For example, it powers [remote functions in SvelteKit](/docs/kit/remote-functions).
2121

2222
To fix the example above:
2323

@@ -30,7 +30,7 @@ To fix the example above:
3030
// it with the provided key and baking it into the `head` content. During hydration, it will
3131
// look for the serialized version, returning it instead of running `getUser`. After hydration
3232
// is done, if it's called again, it'll simply invoke `getUser`.
33-
const user = await hydratable('user', getUser());
33+
const user = await hydratable('user', getUser);
3434
</script>
3535
3636
<h1>{user.name}</h1>
@@ -45,63 +45,21 @@ const rand = hydratable('random', () => Math.random());
4545

4646
If you're a library author, be sure to prefix the keys of your `hydratable` values with the name of your library so that your keys don't conflict with other libraries.
4747

48-
## Imperative API
48+
## Serialization
4949

50-
If you're writing a library with separate server and client exports, it may be more convenient to use the imperative API:
51-
52-
```ts
53-
import { hydratable } from 'svelte';
54-
55-
const value = hydratable.get('foo'); // only works on the client
56-
const hasValue = hydratable.has('foo');
57-
hydratable.set('foo', 'whatever value you want'); // only works on the server
58-
```
59-
60-
## Custom serialization
61-
62-
By default, Svelte uses [`devalue`](https://npmjs.com/package/devalue) to serialize your data on the server so that decoding it on the client requires no dependencies. If you need to serialize additional things not covered by `devalue`, you can provide your own transport mechanisms by writing custom `encode` and `decode` methods.
63-
64-
### `encode`
65-
66-
Encode receives a value and outputs _the JavaScript code necessary to create that value on the client_. For example, Svelte's built-in encoder looks like this:
67-
68-
```js
69-
import * as devalue from 'devalue';
70-
71-
/**
72-
* @param {any} value
73-
*/
74-
function encode (value) {
75-
return devalue.uneval(value);
76-
}
77-
78-
encode(['hello', 'world']); // outputs `['hello', 'world']`
79-
```
80-
81-
### `decode`
82-
83-
`decode` accepts whatever the JavaScript that `encode` outputs resolves to, and returns whatever the final value from `hydratable` should be.
84-
85-
### Usage
86-
87-
When using the isomorphic API, you must provide either `encode` or `decode`, depending on the environment. This enables your bundler to treeshake the unneeded code during your build:
50+
All data returned from a `hydratable` function must be serializable. Not to fear, though -- this doesn't mean you're limited to JSON! Svelte uses [`devalue`](https://npmjs.com/package/devalue) for serialization, which means it can serialize all sorts of things, including `Map`, `Set`, `URL`, and `BigInt`. Check the documentation page for a full list. In addition to these, thanks to some Svelte magic, you can also fearlessly use promises:
8851

8952
```svelte
9053
<script>
9154
import { hydratable } from 'svelte';
92-
import { BROWSER } from 'esm-env';
93-
import { encode, decode } from '$lib/encoders';
94-
95-
const random = hydratable('random', () => Math.random(), { transport: BROWSER ? { decode } : { encode }});
55+
const promises = hydratable('random', () => {
56+
return {
57+
one: Promise.resolve(1),
58+
two: Promise.resolve(2)
59+
}
60+
});
9661
</script>
97-
```
98-
99-
For the imperative API, you just provide `encode` or `decode` depending on which method you're using:
100-
101-
```ts
102-
import { hydratable } from 'svelte';
103-
import { encode, decode } from '$lib/encoders';
10462
105-
const random = hydratable.get('random', { decode });
106-
hydratable.set('random', Math.random(), { encode });
63+
{await promises.one}
64+
{await promises.two}
10765
```

packages/svelte/tests/runtime-legacy/shared.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,6 @@ async function run_test_variant(
566566
}
567567
} catch (err) {
568568
if (config.runtime_error) {
569-
console.log(err);
570569
assert.include((err as Error).message, config.runtime_error);
571570
} else if (config.error && !unintended_error) {
572571
assert.include((err as Error).message, config.error);

0 commit comments

Comments
 (0)