Skip to content

Commit 46bf8c3

Browse files
committed
feat: simplify rpc functions and remove filename from url
TanStack Server Functions already encodes the name into the function id: https://github.com/TanStack/router/blob/40b7576ff89d549af1218a1925c7cfaf9e85d724/packages/server-functions-plugin/src/index.ts#L111 Refs: #1859, #1872
1 parent c1efa15 commit 46bf8c3

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

packages/start/src/config/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
188188
fileURLToPath(new URL("../server/server-runtime", import.meta.url)),
189189
)}"`,
190190
replacer: (opts) =>
191-
`createServerReference(${() => {}}, '${opts.functionId}', '${opts.extractedFilename}')`,
191+
`createServerReference('${opts.functionId}')`,
192192
},
193193
{
194194
envConsumer: "server",
@@ -200,7 +200,7 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
200200
),
201201
)}'`,
202202
replacer: (opts) =>
203-
`createServerReference(${opts.fn}, '${opts.functionId}', '${opts.extractedFilename}')`,
203+
`createServerReference(${opts.fn}, '${opts.functionId}')`,
204204
}
205205
],
206206
provider: {
@@ -212,7 +212,7 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
212212
),
213213
)}'`,
214214
replacer: (opts) =>
215-
`createServerReference(${opts.fn}, '${opts.functionId}', '${opts.extractedFilename}')`,
215+
`createServerReference(${opts.fn}, '${opts.functionId}')`,
216216
},
217217
}),
218218
{

packages/start/src/server/server-fns-runtime.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { getRequestEvent } from "solid-js/web";
22
import { provideRequestEvent } from "solid-js/web/storage";
33

4-
export function createServerReference(fn: Function, id: string, name: string) {
4+
export function createServerReference(fn: Function, id: string) {
55
if (typeof fn !== "function")
66
throw new Error("Export from a 'use server' module must be a function");
77
const baseURL = import.meta.env.SERVER_BASE_URL ?? "";
88
return new Proxy(fn, {
99
get(target, prop, receiver) {
1010
if (prop === "url") {
11-
return `${baseURL}/_server?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
11+
return `${baseURL}/_server?id=${encodeURIComponent(id)}`;
1212
}
1313
if (prop === "GET") return receiver;
1414
return (target as any)[prop];
@@ -18,7 +18,7 @@ export function createServerReference(fn: Function, id: string, name: string) {
1818
if (!ogEvt) throw new Error("Cannot call server function outside of a request");
1919
const evt = { ...ogEvt };
2020
evt.locals.serverFunctionMeta = {
21-
id: id + "#" + name
21+
id
2222
};
2323
evt.serverOnly = true;
2424
return provideRequestEvent(evt, () => {

packages/start/src/server/server-functions-handler.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,14 @@ export async function handleServerFunction(h3Event: H3Event) {
8383
const instance = request.headers.get("X-Server-Instance");
8484
const singleFlight = request.headers.has("X-Single-Flight");
8585
const url = new URL(request.url);
86-
let functionId: string | undefined | null, name: string | undefined | null;
86+
let functionId: string | undefined | null;
8787
if (serverReference) {
8888
// invariant(typeof serverReference === "string", "Invalid server function");
89-
[functionId, name] = serverReference.split("#");
89+
[functionId] = serverReference.split("#");
9090
} else {
9191
functionId = url.searchParams.get("id");
92-
name = url.searchParams.get("name");
9392

94-
if (!functionId || !name) {
93+
if (!functionId) {
9594
return process.env.NODE_ENV === "development"
9695
? new Response("Server function not found", { status: 404 })
9796
: new Response(null, { status: 404 });
@@ -158,7 +157,7 @@ export async function handleServerFunction(h3Event: H3Event) {
158157
/* @ts-expect-error */
159158
sharedConfig.context = { event };
160159
event.locals.serverFunctionMeta = {
161-
id: functionId + "#" + name,
160+
id: functionId
162161
};
163162
return serverFunction(...parsed);
164163
});

packages/start/src/server/server-runtime.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,20 @@ async function fetchServerFunction(
192192
return result;
193193
}
194194

195-
export function createServerReference(fn: Function, id: string, name: string) {
195+
export function createServerReference(id: string) {
196196
const baseURL = import.meta.env.SERVER_BASE_URL ?? "";
197+
const fn = (...args: any[]) => fetchServerFunction(`${baseURL}/_server`, id, {}, args);
198+
197199
return new Proxy(fn, {
198200
get(target, prop, receiver) {
199201
if (prop === "url") {
200-
return `${baseURL}/_server?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
202+
return `${baseURL}/_server?id=${encodeURIComponent(id)}`;
201203
}
202204
if (prop === "GET") {
203205
return receiver.withOptions({ method: "GET" });
204206
}
205207
if (prop === "withOptions") {
206-
const url = `${baseURL}/_server?id=${encodeURIComponent(id)}&name=${encodeURIComponent(
207-
name
208-
)}`;
208+
const url = `${baseURL}/_server?id=${encodeURIComponent(id)}`;
209209
return (options: RequestInit) => {
210210
const fn = async (...args: any[]) => {
211211
const encodeArgs = options.method && options.method.toUpperCase() === "GET";
@@ -218,7 +218,7 @@ export function createServerReference(fn: Function, id: string, name: string) {
218218
)}`
219219
: "")
220220
: `${baseURL}/_server`,
221-
`${id}#${name}`,
221+
id,
222222
options,
223223
encodeArgs ? [] : args
224224
);
@@ -229,12 +229,9 @@ export function createServerReference(fn: Function, id: string, name: string) {
229229
}
230230
return (target as any)[prop];
231231
},
232-
apply(target, thisArg, args) {
233-
return fetchServerFunction(`${baseURL}/_server`, `${id}#${name}`, {}, args);
234-
}
235232
});
236233
}
237234

238-
export function createClientReference(Component: Component<any>, id: string, name: string) {
235+
export function createClientReference(Component: Component<any>, id: string) {
239236
return Component;
240237
}

0 commit comments

Comments
 (0)