mirror of
https://github.com/Dvorinka/Devour.git
synced 2026-06-04 12:33:04 +00:00
17 lines
3.4 KiB
JSON
17 lines
3.4 KiB
JSON
{
|
|
"id": "dbc23ba19ebd2dbef7e2d6ec",
|
|
"source": "solid:signals",
|
|
"type": "github-document",
|
|
"title": "get-request-event",
|
|
"content": "---\ntitle: getRequestEvent\nuse_cases: \u003e-\n server-side rendering, authentication, headers manipulation, cookies, server\n functions, request context access\ntags:\n - server\n - ssr\n - request\n - headers\n - authentication\n - context\nversion: '1.0'\ndescription: \u003e-\n Access server request context with getRequestEvent. Read headers, set cookies,\n and manage response status in SSR and server functions.\n---\n\nSolid uses Async Local Storage as a way of injecting the request context anywhere on the server.\nThe server provides a utility function to access this context\n(called a `RequestEvent`).\n\n```js\nimport { getRequestEvent } from \"solid-js/web\"\nimport type { RequestEvent } from \"solid-js/web\"\n\nfunction getRequestEvent(): RequestEvent | undefined\n```\n\nYou can retrieve the request event by calling `getRequestEvent`:\n\n```js\nimport { getRequestEvent } from \"solid-js/web\"\n\nconst event = getRequestEvent()\n```\n\n## Request\n\n`.request` is the most important property of the `RequestEvent`.\nThis is a Web [Request object](https://developer.mozilla.org/en-US/docs/Web/API/Request) that represents the current request to the server.\nYou can access properties off of it such as `url` and `headers`.\n `body`, however, does not typically need to be handled directly for things such as server functions or rendering, which already handle mapping.\n\n```js\nimport { getRequestEvent } from \"solid-js/web\"\n\nconst event = getRequestEvent();\nif (event) {\n\tconst auth = event.request.headers.get(\"Authorization\");\n}\n```\n\n## Response\n\nThe `getRequestEvent` can also be used to stub out the Response - this extends the [options that can be passed to the `Response constructor`](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#options).\nThis is kept up to date so it can be used to read and write headers and status for the current response.\n\n```js\nimport { getRequestEvent } from \"solid-js/web\"\n\nconst event = getRequestEvent();\nif (event) {\n\tevent.response.headers.append(\"Set-Cookie\", \"foo=hello\");\n\tevent.response.status = 201;\n}\n```\n\n### Change event.response or create a new Response\n\nThe `getRequestEvent` event is considered global and lasts the life of the request.\nTherefore, if you are calling a server function on the server during SSR or an RPC call, setting values on `event.response` will reflect on that request.\n\nThe returned response will only impact the response when it is an RPC call.\nThis is important because some headers previously set may not be needed to be set for the whole page, but only for a specific request.\n\n**Note:** This is important to keep in mind when choosing where to set headers and responses.\n\n:::note[Usage with SolidStart]\n\tSee this guide on [Request\n\tEvents](/solid-start/advanced/request-events).\n:::",
|
|
"url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/reference/server-utilities/get-request-event.mdx",
|
|
"metadata": {
|
|
"path": "src/routes/reference/server-utilities/get-request-event.mdx",
|
|
"repo": "solidjs/solid-docs",
|
|
"repo_url": "https://github.com/solidjs/solid-docs.git",
|
|
"size": 2752,
|
|
"source_type": "github"
|
|
},
|
|
"hash": "2f7290b6a87cdba1244eeb51f680cd586309ac28e9cb5ea06a3779d86a55bf71",
|
|
"timestamp": "2026-02-23T11:43:00.190739703+01:00"
|
|
} |