mirror of
https://github.com/Dvorinka/Devour.git
synced 2026-06-04 12:33:04 +00:00
17 lines
4.0 KiB
JSON
17 lines
4.0 KiB
JSON
{
|
|
"id": "364743f9181593a9a0ea073f",
|
|
"source": "solid:signals",
|
|
"type": "github-document",
|
|
"title": "use-server",
|
|
"content": "---\ntitle: '\"use server\"'\nuse_cases: \u003e-\n server-only logic, api endpoints, database operations, secure operations, data\n fetching, server actions, rpc calls, authentication logic\ntags:\n - server\n - rpc\n - api\n - data-fetching\n - actions\n - security\n - database\nversion: '1.0'\ndescription: \u003e-\n Create server-only functions in SolidStart using 'use server' directive.\n Handle database operations, API calls, and secure logic on the server.\n---\n\n`\"use server\"` enables functions or files to be executed only on the server. Server functions allow client components to call code that is executed in the server context.\n\n```tsx\n// Function-level\nconst logHello = async (message: string) =\u003e {\n\t\"use server\";\n\tconsole.log(message);\n};\n```\nOr when using at the top of a file.\n```tsx\n// File-level\n\"use server\";\n\nconst logHello = async (message: string) =\u003e {\n\tconsole.log(message);\n};\n```\n\n**Note:** `\"use server\"` functions must be marked async or return a promise.\n\n## Basic usage\n\nWhen using `\"use server\"`, regardless of whether server rendering is enabled, the functions it apply to will only run on the server.\n\nTo do this, compilation is used to transform the `\"use server\"` function into an RPC call to the server.\n\nIf `\"use server\"` is inserted as the first line in a file, the entire file will become server-only.\n\n```tsx {1}\n\"use server\";\n\nconst logHello = async (message: string) =\u003e {\n\tconsole.log(message);\n};\n```\n\nHowever, if `\"use server\"` is inserted as the first line of a function, only that function will be server-only:\n\n```tsx {2}\nconst logHello = async (message: string) =\u003e {\n\t\"use server\";\n\tconsole.log(message);\n};\n\nlogHello(\"Hello\");\n```\n\nIn both examples, the `logHello` function will only show in the server console, regardless of whether rendering was on the server or in the browser.\n\n## Usage with Data APIs\n\nServer functions can be used for fetching data and performing actions on the server.\nThe following examples show how to use server functions alongside solid-router's data APIs.\n\n```tsx {3}\nconst getUser = query((id) =\u003e {\n \"use server\";\n return db.getUser(id);\n}, \"users\");\n\nconst updateUser = action(async (id, data) =\u003e {\n \"use server\"\n await db.setUser(id, data);\n throw redirect(\"/\", { revalidate: getUser.keyFor(id) });\n});\n\n```\n\nWhen `getUser` or `updateUser` are triggered on the client, an http request will be made to the server, which calls the corresponding server function.\n\n## Single-flight mutations\n\nIn the above example, when the `updateUser` action is called, a redirect is thrown on the server.\nSolid Start can handle this redirect on the server instead of propagating it to the client.\nThe data for the redirected page is fetched and streamed to the client in the same http request as the `updateUser` action, rather than the client requiring a separate http request for the redirected page.\n\n## Serialization\n\nServer functions allow the serialization of many different data types in the response, using the Seroval serializer.\nThe full list is available [in Seroval's source code](https://github.com/lxsmnsyc/seroval/blob/main/docs/compatibility.md#supported-types).\n\n## Meta information\n\nTo get a stable function-specific identifier, even for parallel processes or multiple cpu cores or workers, use the [`getServerFunctionMeta`](/solid-start/reference/server/get-server-function-meta)",
|
|
"url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/solid-start/reference/server/use-server.mdx",
|
|
"metadata": {
|
|
"path": "src/routes/solid-start/reference/server/use-server.mdx",
|
|
"repo": "solidjs/solid-docs",
|
|
"repo_url": "https://github.com/solidjs/solid-docs.git",
|
|
"size": 3350,
|
|
"source_type": "github"
|
|
},
|
|
"hash": "0e340ab25d0fc0bf0f2400a15e6c42fe77d09e57698f5946a5c504d22675165c",
|
|
"timestamp": "2026-02-23T11:43:00.195463491+01:00"
|
|
} |