Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-pugs-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: add server property when load executes on server
2 changes: 2 additions & 0 deletions documentation/docs/20-core-concepts/20-load.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ Server `load` functions are called with a `ServerLoadEvent`, which inherits `cli

Universal `load` functions are called with a `LoadEvent`, which has a `data` property. If you have `load` functions in both `+page.js` and `+page.server.js` (or `+layout.js` and `+layout.server.js`), the return value of the server `load` function is the `data` property of the universal `load` function's argument.

> [!NOTE] Since 2.??, when universal `load` functions are called on server, `LoadEvent` includes `server` object, which consists of `clientAddress`, `cookies`, `locals`, `platform` and `request`. `server` becomes `undefined` if `load` functions are called on client.

### Output

A universal `load` function can return an object containing any values, including things like custom classes and component constructors.
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,14 @@ export interface LoadEvent<
/** The span associated with the current `load` function. */
current: Span;
};

/**
* Server only properties when `LoadEvent` executed on server.
*/
server?: Omit<
ServerLoadEvent<Params, ParentData, RouteId>,
keyof LoadEvent<Params, Data, ParentData, RouteId>
>;
}

export interface NavigationEvent<
Expand Down
12 changes: 11 additions & 1 deletion packages/kit/src/runtime/server/page/load_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,17 @@ export async function load_data({
depends: () => {},
parent,
untrack: (fn) => fn(),
tracing: traced_event.tracing
tracing: traced_event.tracing,
server: {
cookies: event.cookies,
getClientAddress: event.getClientAddress,
locals: event.locals,
platform: event.platform,
request: event.request,
isDataRequest: event.isDataRequest,
isSubRequest: event.isSubRequest,
isRemoteRequest: event.isRemoteRequest
}
})
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { browser } from '$app/environment';

/** @type {import('@sveltejs/kit').Load} */
export async function load({ server }) {
return {
correct: browser === !server && (!server || ((await server.request.arrayBuffer()) && true))
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/** @type {import('./$types').PageData} */
export let data;
</script>

<h1>Is it correct? {data.correct}</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { browser } from '$app/environment';

/** @type {import('@sveltejs/kit').Load} */
export async function load({ server }) {
return {
correct: browser === !server && (!server || ((await server.request.arrayBuffer()) && true))
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/** @type {import('./$types').PageData} */
export let data;
</script>

<h1>Is it correct? {data.correct}</h1>
8 changes: 8 additions & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,14 @@ declare module '@sveltejs/kit' {
/** The span associated with the current `load` function. */
current: Span;
};

/**
* Server only properties when `LoadEvent` executed on server.
*/
server?: Omit<
ServerLoadEvent<Params, ParentData, RouteId>,
keyof LoadEvent<Params, Data, ParentData, RouteId>
>;
}

export interface NavigationEvent<
Expand Down
Loading