{ "id": "3f266ab7800a82b69961fa46", "source": "solid:signals", "type": "github-document", "title": "data-fetching", "content": "---\ntitle: \"Data fetching\"\n---\n\nFetching data from a remote API or database is a core task for most applications.\n[Solid](/) and [Solid Router](/solid-router) provide foundational tools like the [`createResource` primitive](/guides/fetching-data) and [queries](/solid-router/data-fetching/queries) to manage asynchronous data.\n\nSolidStart builds on these capabilities, extending them to provide a comprehensive solution for data fetching in a full-stack environment.\n\nThis page assumes you are familiar with the fundamental concepts of Solid and Solid Router.\nIf you are a beginner, we highly recommend starting with the [queries documentation](/solid-router/data-fetching/queries).\nYou can also find many practical examples in the [data fetching how-to guide](/solid-start/guides/data-fetching).\n\n## Server functions and queries\n\nServer functions provide a way to write functions that run exclusively on the server.\nThis makes it safe to fetch data directly from a database without relying on a separate API endpoint.\n\nServer functions integrate seamlessly with queries, as they can be used as the fetcher for a query.\n\n```tsx\nimport { query, redirect } from \"@solidjs/router\";\nimport { useSession } from \"vinxi/http\";\nimport { db } from \"./db\";\n\nconst getCurrentUserQuery = query(async (id: string) =\u003e {\n\t\"use server\";\n\tconst session = await useSession({\n\t\tpassword: process.env.SESSION_SECRET as string,\n\t\tname: \"session\",\n\t});\n\n\tif (session.data.userId) {\n\t\treturn await db.users.get({ id: session.data.userId });\n\t} else {\n\t\tthrow redirect(\"/login\");\n\t}\n}, \"currentUser\");\n```\n\nIn this example, the `getCurrentUserQuery` retrieves the session data, and if an authenticated user exists, it gets their information from the database and returns it.\nOtherwise, it redirects the user to the login page.\nAll of these operations are performed completely on the server regardless of how the query is called.\n\n:::caution[Modifying headers after streaming]\nOnce streaming begins, response headers (including status and cookies) are sent and cannot be changed.\nAny header-modifying logic within a server function, such as redirects or APIs like `useSession` that set cookies, must run before streaming starts;\notherwise, this error will occur:\n`Cannot set headers after they are sent to the client.`\n\nTo avoid this, disable streaming for queries that may modify headers by enabling the [`deferStream`](/solid-router/reference/data-apis/create-async#deferstream) option.\n\n```tsx\nconst user = createAsync(() =\u003e getCurrentUserQuery(), { deferStream: true });\n```\n\n:::", "url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/solid-start/building-your-application/data-fetching.mdx", "metadata": { "path": "src/routes/solid-start/building-your-application/data-fetching.mdx", "repo": "solidjs/solid-docs", "repo_url": "https://github.com/solidjs/solid-docs.git", "size": 2559, "source_type": "github" }, "hash": "6fe011fbf03a34cbfc987c9ef345ab55d40802888663b338ccc8872cd3989035", "timestamp": "2026-02-23T11:43:00.193821114+01:00" }