Skip to content

Commit

Permalink
feat: implement getServerFunctionMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
katywings authored and ryansolid committed Mar 6, 2024
1 parent 28357e6 commit de833eb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
23 changes: 23 additions & 0 deletions docs/routes/api/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
```
3 changes: 3 additions & 0 deletions packages/start/config/server-fns-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export function createServerReference(fn: Function, id: string, name: string) {
const ogEvt = getRequestEvent();
if (!ogEvt) throw new Error("Cannot call server function outside of a request");
const evt = cloneEvent(ogEvt);
evt.locals.serverFunctionMeta = {
id: id + "#" + name,
};
evt.serverOnly = true;
return provideRequestEvent(evt, () => {
return fn.apply(thisArg, args);
Expand Down
2 changes: 1 addition & 1 deletion packages/start/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { StartServer } from "./StartServer";
export { createHandler } from "./handler";
export { getServerFunctionMeta } from "./serverFunction";
export * from "./types";

6 changes: 6 additions & 0 deletions packages/start/server/serverFunction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getRequestEvent } from "solid-js/web";
import type { ServerFunctionMeta } from "./types";

export function getServerFunctionMeta(): ServerFunctionMeta | undefined {
return getRequestEvent()?.locals.serverFunctionMeta;
}
4 changes: 4 additions & 0 deletions packages/start/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export interface APIHandler {
(event: APIEvent): Promise<any>;
}

export interface ServerFunctionMeta {
id: string;
}

declare module "solid-js/web" {
interface RequestEvent extends FetchEvent {
serverOnly?: boolean;
Expand Down

0 comments on commit de833eb

Please sign in to comment.