Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement getServerFunctionMeta #1203

Closed
wants to merge 3 commits into from
Closed
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
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.tsx
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 @@ -63,6 +63,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
Loading