Server Utilities

getRequestEvent

Edit this page

Solid uses Async Local Storage as a way of injecting the request context anywhere on the server. This is also the event that shows up in middleware.

It can be retrieved by getRequestEvent from "solid-js/web".

import { getRequestEvent } from "solid-js/web";
const event = getRequestEvent();

Request

.request is the he most important property of the RequestEvent. This is a Web Request object that represents the current request to the server. You can access properties off of it such as url and headers. body, however, does not typically need to be handled directly for things such as server functions or rendering, which already handle mapping.

import { getRequestEvent } from "solid-js/web";
const event = getRequestEvent();
if (event) {
const auth = event.request.headers.get("Authorization");
}

Response

The getRequestEvent can also be used to stub out the Response - this extends the options that can pass to the Response constructor. This is kept up to date so it can be used to read and write headers and status for the current response.

import { getRequestEvent } from "solid-js/web";
const event = getRequestEvent();
if (event) {
event.response.headers.append("Set-Cookie", "foo=hello");
event.response.status = 201;
}

Change event.response or create a new Response

The getRequestEvent event is considered global and lasts the life of the request. Therefore, 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.

The returned response will only impact the response when it is an RPC call. This is important because some headers previously set may not be needed to be set for the whole page, but only for a specific request.

Note: This is important to keep in mind when choosing where to set headers and responses.

Report an issue with this page