diff --git a/docs/routes/api/server.md b/docs/routes/api/server.md index 341674e06..d8cbfafee 100644 --- a/docs/routes/api/server.md +++ b/docs/routes/api/server.md @@ -44,3 +44,26 @@ In this example, regardless of whether we are rendering this on the server or in ### Serialization Server functions allow the serialization of many different data types in the response. The full list is available [here](https://github.com/lxsmnsyc/seroval/blob/main/docs/compatibility.md#supported-types). + +### Meta information + +Depending on your hosting, the server function might be running in parallel on multiple cpu cores or workers. You can use `getServerFunctionMeta` to retrieve a function-specific `id`, which is stable across all instances. + +Keep in mind: this `id` can and will change between builds! + +```tsx twoslash +import { getServerFunctionMeta } from "@solidjs/start/server"; + +// or some in-memory db +const appCache: any = globalThis; + +const counter = async () => { + "use server"; + const { id } = getServerFunctionMeta()!; + const key = `counter_${id}`; + appCache[key] = appCache[key] ?? 0; + appCache[key]++; + + return appCache[key] as number; +}; +``` diff --git a/packages/start/config/server-fns-runtime.jsx b/packages/start/config/server-fns-runtime.jsx index f7744f8ed..76882612a 100644 --- a/packages/start/config/server-fns-runtime.jsx +++ b/packages/start/config/server-fns-runtime.jsx @@ -13,6 +13,9 @@ export function createServerReference(fn, id, name) { const ogEvt = getRequestEvent(); if (!ogEvt) throw new Error("Cannot call server function outside of a request"); const evt = cloneEvent(ogEvt); + evt.context.serverFunctionMeta = { + id: id + "#" + name, + }; evt.serverOnly = true; return provideRequestEvent(evt, () => { return fn.apply(thisArg, args); diff --git a/packages/start/server/index.tsx b/packages/start/server/index.tsx index 19188357f..7c23b62c1 100644 --- a/packages/start/server/index.tsx +++ b/packages/start/server/index.tsx @@ -1,6 +1,7 @@ export * from "vinxi/server"; export { StartServer } from "./StartServer"; export * from "./middleware"; +export * from "./serverFunction"; export * from "./types"; import { createHandler as createBaseHandler } from "./handler"; import { createPageEvent } from "./page-event"; diff --git a/packages/start/server/serverFunction.tsx b/packages/start/server/serverFunction.tsx new file mode 100644 index 000000000..2b02d2a49 --- /dev/null +++ b/packages/start/server/serverFunction.tsx @@ -0,0 +1,6 @@ +import { getRequestEvent } from "solid-js/web"; +import type { ServerFunctionMeta } from "./types"; + +export function getServerFunctionMeta(): ServerFunctionMeta | undefined { + return getRequestEvent()?.context.serverFunctionMeta; +} diff --git a/packages/start/server/types.ts b/packages/start/server/types.ts index 554edef20..ff88d4e53 100644 --- a/packages/start/server/types.ts +++ b/packages/start/server/types.ts @@ -40,6 +40,10 @@ export interface APIHandler { (event: APIEvent): Promise; } +export interface ServerFunctionMeta { + id: string; +} + declare module "solid-js/web" { interface RequestEvent extends FetchEvent { serverOnly?: boolean;