useServerContext is a hook that gives you access to the PageEvent received by the server.
Accessing the request on the server
One common use case of this is to get the cookies. Cookies can be used to save user-preferences as session data. These preferences can be used to personalize the page on the server and the client identically.
tsx
import { parseCookie
Parse a cookie header.
Parse the given cookie header string into an object
The object has the various cookies as keys(names) => values
(alias) function parseCookie(str: string, options?: CookieParseOptions): Record<string, string>
import parseCookie
Parse a cookie header.
Parse the given cookie header string into an object
The object has the various cookies as keys(names) => values
(alias) parseCookie(str: string, options?: CookieParseOptions | undefined): Record<string, string>
import parseCookie
(
isServer
(alias) const isServer: false
import isServer
?event
const event: PageEvent
.request
(property) FetchEvent.request: Request
.headers
Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header.
(property) Request.headers: Headers
.get
(method) Headers.get(name: string): string | null
("cookie") ??""
:document
var document: Document
.cookie
Returns the HTTP cookies that apply to the Document. If there are no cookies or cookies can't be applied to this resource, the empty string will be returned.
Can be set, to add a new cookie to the element's set of HTTP cookies.
If the contents are sandboxed into a unique origin (e.g. in an iframe with the sandbox attribute), a "SecurityError" DOMException will be thrown on getting and setting.
(property) Document.cookie: string
Parse a cookie header.
Parse the given cookie header string into an object
The object has the various cookies as keys(names) => values
(alias) function parseCookie(str: string, options?: CookieParseOptions): Record<string, string>
import parseCookie
Parse a cookie header.
Parse the given cookie header string into an object
The object has the various cookies as keys(names) => values
(alias) parseCookie(str: string, options?: CookieParseOptions | undefined): Record<string, string>
import parseCookie
(
isServer
(alias) const isServer: false
import isServer
?event
const event: PageEvent
.request
(property) FetchEvent.request: Request
.headers
Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header.
(property) Request.headers: Headers
.get
(method) Headers.get(name: string): string | null
("cookie") ??""
:document
var document: Document
.cookie
Returns the HTTP cookies that apply to the Document. If there are no cookies or cookies can't be applied to this resource, the empty string will be returned.
Can be set, to add a new cookie to the element's set of HTTP cookies.
If the contents are sandboxed into a unique origin (e.g. in an iframe with the sandbox attribute), a "SecurityError" DOMException will be thrown on getting and setting.
(property) Document.cookie: string