Skip to content

Commit 3ea5177

Browse files
committed
fix server-fns client runtime
1 parent 72da53e commit 3ea5177

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

packages/start/src/fns/client.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ async function fetchServerFunction(
193193
return result;
194194
}
195195

196-
export function createServerReference(id: string) {
197-
let baseURL = import.meta.env.BASE_URL ?? "/";
198-
if (!baseURL.endsWith("/")) baseURL += "/";
199-
200-
const fn = (...args: any[]) => fetchServerFunction(`${baseURL}_server`, id, {}, args);
201-
196+
function enhanceServerFunction(id: string, baseURL: string, fn: Function) {
202197
return new Proxy(fn, {
203198
get(target, prop, receiver) {
204199
if (prop === "url") {
@@ -235,6 +230,27 @@ export function createServerReference(id: string) {
235230
});
236231
}
237232

233+
export function createServerReference(id: string) {
234+
let baseURL = import.meta.env.BASE_URL ?? "/";
235+
if (!baseURL.endsWith("/")) baseURL += "/";
236+
237+
const fn = (...args: any[]) => fetchServerFunction(`${baseURL}_server`, id, {}, args);
238+
return enhanceServerFunction(id, baseURL, fn);
239+
}
240+
241+
export function createServerFunction<T extends any[], R>(
242+
id: string,
243+
fn: () => Promise<(...args: T) => Promise<R>>,
244+
) {
245+
let baseURL = import.meta.env.BASE_URL ?? "/";
246+
if (!baseURL.endsWith("/")) baseURL += "/";
247+
let instance: (...args: T) => Promise<R>;
248+
return enhanceServerFunction(id, baseURL, async (...args: T) => {
249+
instance = instance || (await fn());
250+
return instance(...args);
251+
});
252+
}
253+
238254
export function createClientReference(Component: Component<any>, id: string) {
239255
return Component;
240256
}

0 commit comments

Comments
 (0)