Skip to content

Commit b41f7c4

Browse files
committed
add server-only handleServerLoad
1 parent 07a6624 commit b41f7c4

File tree

8 files changed

+29
-9
lines changed

8 files changed

+29
-9
lines changed

packages/kit/src/runtime/server/ambient.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ declare module '__SERVER__/internal.js' {
55
handleError?: import('types').HandleServerError;
66
handleFetch?: import('types').HandleFetch;
77
handleLoad?: import('types').HandleLoad;
8+
handleServerLoad?: import('types').HandleServerLoad;
89
}>;
910
}

packages/kit/src/runtime/server/data/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ export async function render_data(
7878
}
7979
}
8080
return data;
81-
}
81+
},
82+
handleServerLoad: options.hooks.handleServerLoad
8283
});
8384
} catch (e) {
8485
aborted = true;

packages/kit/src/runtime/server/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export class Server {
4242
// @ts-expect-error
4343
handleError: module.handleError || (({ error }) => console.error(error?.stack)),
4444
handleFetch: module.handleFetch || (({ request, fetch }) => fetch(request)),
45-
handleLoad: module.handleLoad || (({ event, resolve }) => resolve(event))
45+
handleLoad: module.handleLoad || (({ event, resolve }) => resolve(event)),
46+
handleServerLoad: module.handleServerLoad || (({ event, resolve }) => resolve(event))
4647
};
4748
}
4849
}

packages/kit/src/runtime/server/page/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ export async function render_page(event, page, options, manifest, state, resolve
150150
if (parent) Object.assign(data, await parent.data);
151151
}
152152
return data;
153-
}
153+
},
154+
handleServerLoad: options.hooks.handleServerLoad
154155
});
155156
} catch (e) {
156157
load_error = /** @type {Error} */ (e);

packages/kit/src/runtime/server/page/load_data.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import { validate_depends } from '../../shared.js';
1010
* state: import('types').SSRState;
1111
* node: import('types').SSRNode | undefined;
1212
* parent: () => Promise<Record<string, any>>;
13+
* handleServerLoad: import('types').HandleServerLoad;
1314
* }} opts
1415
* @returns {Promise<import('types').ServerDataNode | null>}
1516
*/
16-
export async function load_server_data({ event, state, node, parent }) {
17+
export async function load_server_data({ event, state, node, parent, handleServerLoad }) {
1718
if (!node?.server) return null;
1819

1920
let done = false;
@@ -40,9 +41,9 @@ export async function load_server_data({ event, state, node, parent }) {
4041
disable_search(url);
4142
}
4243

43-
const result = await node.server.load?.call(null, {
44+
const load_event = {
4445
...event,
45-
fetch: (info, init) => {
46+
fetch: (/** @type {URL | RequestInfo} */ info, /** @type {RequestInit | undefined} */ init) => {
4647
const url = new URL(info instanceof Request ? info.url : info, event.url);
4748

4849
if (DEV && done && !uses.dependencies.has(url.href)) {
@@ -112,7 +113,11 @@ export async function load_server_data({ event, state, node, parent }) {
112113
}
113114
}),
114115
url
115-
});
116+
};
117+
118+
const result = node.server.load
119+
? await handleServerLoad({ event: load_event, resolve: node.server.load })
120+
: null;
116121

117122
const data = result ? await unwrap_promises(result) : null;
118123
if (__SVELTEKIT_DEV__) {

packages/kit/src/runtime/server/page/respond_with_error.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export async function respond_with_error({
4444
event,
4545
state,
4646
node: default_layout,
47-
parent: async () => ({})
47+
parent: async () => ({}),
48+
handleServerLoad: options.hooks.handleServerLoad
4849
});
4950

5051
const server_data = await server_data_promise;

packages/kit/types/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@ export interface HandleLoad {
642642
(input: { event: LoadEvent; resolve: Load }): ReturnType<Load>;
643643
}
644644

645+
/**
646+
* The `handleServerLoad` hook runs every time a server-only `load` function (for example from +page.server.js) is called on the server.
647+
* This hook provides the server load `event` and a `resolve` function to call the actual hook with the event.
648+
*/
649+
export interface HandleServerLoad {
650+
(input: { event: ServerLoadEvent; resolve: ServerLoad }): ReturnType<ServerLoad>;
651+
}
652+
645653
/**
646654
* The [`handleFetch`](https://kit.svelte.dev/docs/hooks#server-hooks-handlefetch) hook allows you to modify (or replace) a `fetch` request that happens inside a `load` function that runs on the server (or during pre-rendering)
647655
*/

packages/kit/types/internal.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
HandleFetch,
1515
Actions,
1616
HandleClientError,
17-
HandleLoad
17+
HandleLoad,
18+
HandleServerLoad
1819
} from './index.js';
1920
import {
2021
HttpMethod,
@@ -96,6 +97,7 @@ export interface ServerHooks {
9697
handle: Handle;
9798
handleError: HandleServerError;
9899
handleLoad: HandleLoad;
100+
handleServerLoad: HandleServerLoad;
99101
}
100102

101103
export interface ClientHooks {

0 commit comments

Comments
 (0)