You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[feat] Allow server-only load functions to return more than JSON (#6318)
closes#6008fixes#6357
This allows load functions to return things like Date objects, regexes, Map and Set, BigInt and so on. It also allows repeated and cyclical references, for the times when they're useful.
During client-side navigation, SvelteKit will load this data using `fetch`, which means that the returned value must be serializable as JSON.
109
+
During client-side navigation, SvelteKit will load this data from the server, which means that the returned value must be serializable using [devalue](https://github.com/rich-harris/devalue).
Copy file name to clipboardExpand all lines: documentation/docs/05-load.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Loading data
4
4
5
5
A [`+page.svelte`](/docs/routing#page-page-svelte) or [`+layout.svelte`](/docs/routing#layout-layout-svelte) gets its `data` from a `load` function.
6
6
7
-
If the `load` function is defined in `+page.js` or `+layout.js` it will run both on the server and in the browser. If it's instead defined in `+page.server.js` or `+layout.server.js` it will only run on the server, in which case it can (for example) make database calls and access private [environment variables](/docs/modules#$env-static-private), but can only return data that can be serialized as JSON. In both cases, the return value (if there is one) must be an object.
7
+
If the `load` function is defined in `+page.js` or `+layout.js` it will run both on the server and in the browser. If it's instead defined in `+page.server.js` or `+layout.server.js` it will only run on the server, in which case it can (for example) make database calls and access private [environment variables](/docs/modules#$env-static-private), but can only return data that can be serialized with [devalue](https://github.com/rich-harris/devalue). In both cases, the return value (if there is one) must be an object.
The returned `data`, if any, must be an object of values. For a server-only `load` function, these values must be JSON-serializable. Top-level promises will be awaited, which makes it easy to return multiple promises without creating a waterfall:
259
+
The returned `data`, if any, must be an object of values. For a server-only `load` function, these values must be serializable with [devalue](https://github.com/rich-harris/devalue). Top-level promises will be awaited, which makes it easy to return multiple promises without creating a waterfall:
constsrc=`${sliced_pattern}(?:/__data.json)?$`;// TODO adding /__data.json is a temporary workaround — those endpoints should be treated as distinct routes
227
+
constsrc=`${sliced_pattern}(?:/__data.js)?$`;// TODO adding /__data.js is a temporary workaround — those endpoints should be treated as distinct routes
0 commit comments