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
9 changes: 8 additions & 1 deletion packages/start/src/runtime/server-fns-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import { cloneEvent } from "../server/fetchEvent";
export function createServerReference(fn: Function, id: string, name: string) {
if (typeof fn !== "function") throw new Error("Export from a 'use server' module must be a function");
const baseURL = import.meta.env.SERVER_BASE_URL;
// @tanstack/server-functions-plugin contructs the id from the filename + function name eg src_lib_api_ts--getStory_query
// So we extract the name by splitting on --
// This feels flaky and we should rather try and get the function name directly from the plugin
// but this requires a change in the plugin
const functionName = id.split("--").pop() || id;
const functionPath = `${baseURL}/_server/${encodeURIComponent(functionName)}/`;

return new Proxy(fn, {
get(target, prop, receiver) {
if (prop === "url") {
return `${baseURL}/_server?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
return `${functionPath}/?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
}
if (prop === "GET") return receiver;
return (target as any)[prop];
Expand Down
17 changes: 11 additions & 6 deletions packages/start/src/runtime/server-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,23 @@ async function fetchServerFunction(

export function createServerReference(fn: Function, id: string, name: string) {
const baseURL = import.meta.env.SERVER_BASE_URL;
// @tanstack/server-functions-plugin contructs the id from the filename + function name eg src_lib_api_ts--getStory_query
// So we extract the name by splitting on --
// This feels flaky and we should rather try and get the function name directly from the plugin
// but this requires a change in the plugin
const functionName = id.split("--").pop() || id;
const functionPath = `${baseURL}/_server/${encodeURIComponent(functionName)}/`;

return new Proxy(fn, {
get(target, prop, receiver) {
if (prop === "url") {
return `${baseURL}/_server?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
return `${functionPath}?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
}
if (prop === "GET") {
return receiver.withOptions({ method: "GET" });
}
if (prop === "withOptions") {
const url = `${baseURL}/_server/?id=${encodeURIComponent(id)}&name=${encodeURIComponent(
name
)}`;
const url = `${functionPath}?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
return (options: RequestInit) => {
const fn = async (...args: any[]) => {
const encodeArgs = options.method && options.method.toUpperCase() === "GET";
Expand All @@ -217,7 +222,7 @@ export function createServerReference(fn: Function, id: string, name: string) {
JSON.stringify(await Promise.resolve(toJSONAsync(args, { plugins })))
)}`
: "")
: `${baseURL}/_server`,
: functionPath,
`${id}#${name}`,
options,
encodeArgs ? [] : args
Expand All @@ -230,7 +235,7 @@ export function createServerReference(fn: Function, id: string, name: string) {
return (target as any)[prop];
},
apply(target, thisArg, args) {
return fetchServerFunction(`${baseURL}/_server`, `${id}#${name}`, {}, args);
return fetchServerFunction(functionPath, `${id}#${name}`, {}, args);
}
});
}
Expand Down
Loading