Skip to content

Commit 8adb504

Browse files
committed
add server-only handleServerLoad
1 parent 02be6a7 commit 8adb504

File tree

8 files changed

+33
-9
lines changed

8 files changed

+33
-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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export async function render_data(
7979
}
8080
return data;
8181
},
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export async function render_page(event, page, options, manifest, state, resolve
151151
}
152152
return data;
153153
},
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: 17 additions & 7 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)) {
@@ -84,7 +85,7 @@ export async function load_server_data({ event, state, node, parent }) {
8485
}
8586

8687
uses.params.add(key);
87-
return target[/** @type {string} */ (key)];
88+
return target[ /** @type {string} */(key)];
8889
}
8990
}),
9091
parent: async () => {
@@ -108,11 +109,13 @@ export async function load_server_data({ event, state, node, parent }) {
108109
}
109110

110111
uses.route = true;
111-
return target[/** @type {'id'} */ (key)];
112+
return target[ /** @type {'id'} */(key)];
112113
}
113114
}),
114115
url
115-
});
116+
};
117+
118+
const result = node.server.load ? await handleServerLoad({event: load_event, resolve: node.server.load}) : null;
116119

117120
const data = result ? await unwrap_promises(result) : null;
118121
if (__SVELTEKIT_DEV__) {
@@ -171,8 +174,15 @@ export async function load_data({
171174
depends: () => { },
172175
parent
173176
};
174-
177+
178+
console.log('--->');
179+
const res = node.universal.load(load_event);
180+
console.log('parent', await parent());
181+
console.log('evt', load_event);
182+
console.log('super-evt', event);
183+
175184
const result = await handleLoad({event: load_event, resolve: node.universal.load});
185+
console.log('--->');
176186

177187
const data = result ? await unwrap_promises(result) : null;
178188
if (__SVELTEKIT_DEV__) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export async function respond_with_error({
4545
state,
4646
node: default_layout,
4747
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,13 @@ 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+
}
645652

646653
/**
647654
* 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)

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)