pub struct DioxusServerContext { /* private fields */ }
server
only.Expand description
A shared context for server functions that contains information about the request and middleware state.
You should not construct this directly inside components or server functions. Instead use server_context()
to get the server context from the current request.
§Example
#[server]
async fn read_headers() -> Result<(), ServerFnError> {
let server_context = server_context();
let headers: http::HeaderMap = server_context.extract().await?;
println!("{:?}", headers);
Ok(())
}
Implementations§
Source§impl DioxusServerContext
impl DioxusServerContext
Sourcepub fn get<T: Any + Send + Sync + Clone + 'static>(&self) -> Option<T>
pub fn get<T: Any + Send + Sync + Clone + 'static>(&self) -> Option<T>
Clone a value from the shared server context. If you are using DioxusRouterExt
, any values you insert into
the launch context will also be available in the server context.
Example:
use dioxus::prelude::*;
LaunchBuilder::new()
// You can provide context to your whole app (including server functions) with the `with_context` method on the launch builder
.with_context(server_only! {
1234567890u32
})
.launch(app);
#[server]
async fn read_context() -> Result<u32, ServerFnError> {
// You can extract values from the server context with the `extract` function
let FromContext(value) = extract().await?;
Ok(value)
}
fn app() -> Element {
let future = use_resource(read_context);
rsx! {
h1 { "{future:?}" }
}
}
Sourcepub fn insert<T: Any + Send + Sync + 'static>(&self, value: T)
pub fn insert<T: Any + Send + Sync + 'static>(&self, value: T)
Insert a value into the shared server context
Sourcepub fn insert_any(&self, value: Box<dyn Any + Send + Sync + 'static>)
pub fn insert_any(&self, value: Box<dyn Any + Send + Sync + 'static>)
Insert a boxed Any
value into the shared server context
Sourcepub fn insert_factory<F, T>(&self, value: F)
pub fn insert_factory<F, T>(&self, value: F)
Insert a factory that creates a non-sync value for the shared server context
Sourcepub fn insert_boxed_factory(
&self,
value: Box<dyn Fn() -> Box<dyn Any> + Send + Sync>,
)
pub fn insert_boxed_factory( &self, value: Box<dyn Fn() -> Box<dyn Any> + Send + Sync>, )
Insert a boxed factory that creates a non-sync value for the shared server context
Sourcepub fn response_parts(&self) -> RwLockReadGuard<'_, Parts>
pub fn response_parts(&self) -> RwLockReadGuard<'_, Parts>
Get the response parts from the server context
This method interacts with information from the current request. The request may come from:
- The initial SSR render if this method called from a
Component
or aserver
function that is called during the initial render
#[component]
fn PrintHtmlRequestInfo() -> Element {
// The server context only exists on the server, so we need to put it behind a server_only! config
server_only! {
// Since we are calling this from a component, the server context that is returned will be from
// the html request for ssr rendering
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
}
rsx! {}
}
- A request to a
server
function called directly from the client (either on desktop/mobile or on the web frontend after the initial render)
#[server]
async fn read_headers() -> Result<(), ServerFnError> {
// Since we are calling this from a server function, the server context that is may be from the
// initial request or a request from the client
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
Ok(())
}
#[component]
fn CallServerFunction() -> Element {
rsx! {
button {
// If you click the button, the server function will be called and the server context will be
// from the client request
onclick: move |_| async {
_ = read_headers().await
},
"Call server function"
}
}
}
§Example
#[server]
async fn set_headers() -> Result<(), ServerFnError> {
let server_context = server_context();
let response_parts = server_context.response_parts();
let cookies = response_parts
.headers
.get("Cookie")
.ok_or_else(|| ServerFnError::new("failed to find Cookie header in the response"))?;
println!("{:?}", cookies);
Ok(())
}
Sourcepub fn response_parts_mut(&self) -> RwLockWriteGuard<'_, Parts>
pub fn response_parts_mut(&self) -> RwLockWriteGuard<'_, Parts>
Get the response parts from the server context
This method interacts with information from the current request. The request may come from:
- The initial SSR render if this method called from a
Component
or aserver
function that is called during the initial render
#[component]
fn PrintHtmlRequestInfo() -> Element {
// The server context only exists on the server, so we need to put it behind a server_only! config
server_only! {
// Since we are calling this from a component, the server context that is returned will be from
// the html request for ssr rendering
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
}
rsx! {}
}
- A request to a
server
function called directly from the client (either on desktop/mobile or on the web frontend after the initial render)
#[server]
async fn read_headers() -> Result<(), ServerFnError> {
// Since we are calling this from a server function, the server context that is may be from the
// initial request or a request from the client
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
Ok(())
}
#[component]
fn CallServerFunction() -> Element {
rsx! {
button {
// If you click the button, the server function will be called and the server context will be
// from the client request
onclick: move |_| async {
_ = read_headers().await
},
"Call server function"
}
}
}
§Example
#[server]
async fn set_headers() -> Result<(), ServerFnError> {
let server_context = server_context();
server_context.response_parts_mut()
.headers
.insert("Cookie", http::HeaderValue::from_static("dioxus=fullstack"));
Ok(())
}
Sourcepub fn request_parts(&self) -> RwLockReadGuard<'_, Parts>
pub fn request_parts(&self) -> RwLockReadGuard<'_, Parts>
Get the request parts
This method interacts with information from the current request. The request may come from:
- The initial SSR render if this method called from a
Component
or aserver
function that is called during the initial render
#[component]
fn PrintHtmlRequestInfo() -> Element {
// The server context only exists on the server, so we need to put it behind a server_only! config
server_only! {
// Since we are calling this from a component, the server context that is returned will be from
// the html request for ssr rendering
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
}
rsx! {}
}
- A request to a
server
function called directly from the client (either on desktop/mobile or on the web frontend after the initial render)
#[server]
async fn read_headers() -> Result<(), ServerFnError> {
// Since we are calling this from a server function, the server context that is may be from the
// initial request or a request from the client
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
Ok(())
}
#[component]
fn CallServerFunction() -> Element {
rsx! {
button {
// If you click the button, the server function will be called and the server context will be
// from the client request
onclick: move |_| async {
_ = read_headers().await
},
"Call server function"
}
}
}
§Example
#[server]
async fn read_headers() -> Result<(), ServerFnError> {
let server_context = server_context();
let request_parts = server_context.request_parts();
let id: &i32 = request_parts
.extensions
.get()
.ok_or_else(|| ServerFnError::new("failed to find i32 extension in the request"))?;
println!("{:?}", id);
Ok(())
}
Sourcepub fn request_parts_mut(&self) -> RwLockWriteGuard<'_, Parts>
pub fn request_parts_mut(&self) -> RwLockWriteGuard<'_, Parts>
Get the request parts mutably
This method interacts with information from the current request. The request may come from:
- The initial SSR render if this method called from a
Component
or aserver
function that is called during the initial render
#[component]
fn PrintHtmlRequestInfo() -> Element {
// The server context only exists on the server, so we need to put it behind a server_only! config
server_only! {
// Since we are calling this from a component, the server context that is returned will be from
// the html request for ssr rendering
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
}
rsx! {}
}
- A request to a
server
function called directly from the client (either on desktop/mobile or on the web frontend after the initial render)
#[server]
async fn read_headers() -> Result<(), ServerFnError> {
// Since we are calling this from a server function, the server context that is may be from the
// initial request or a request from the client
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
Ok(())
}
#[component]
fn CallServerFunction() -> Element {
rsx! {
button {
// If you click the button, the server function will be called and the server context will be
// from the client request
onclick: move |_| async {
_ = read_headers().await
},
"Call server function"
}
}
}
§Example
#[server]
async fn read_headers() -> Result<(), ServerFnError> {
let server_context = server_context();
let id: i32 = server_context.request_parts_mut()
.extensions
.remove()
.ok_or_else(|| ServerFnError::new("failed to find i32 extension in the request"))?;
println!("{:?}", id);
Ok(())
}
Sourcepub async fn extract<M, T: FromServerContext<M>>(
&self,
) -> Result<T, T::Rejection>
pub async fn extract<M, T: FromServerContext<M>>( &self, ) -> Result<T, T::Rejection>
Extract part of the request.
This method interacts with information from the current request. The request may come from:
- The initial SSR render if this method called from a
Component
or aserver
function that is called during the initial render
#[component]
fn PrintHtmlRequestInfo() -> Element {
// The server context only exists on the server, so we need to put it behind a server_only! config
server_only! {
// Since we are calling this from a component, the server context that is returned will be from
// the html request for ssr rendering
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
}
rsx! {}
}
- A request to a
server
function called directly from the client (either on desktop/mobile or on the web frontend after the initial render)
#[server]
async fn read_headers() -> Result<(), ServerFnError> {
// Since we are calling this from a server function, the server context that is may be from the
// initial request or a request from the client
let context = server_context();
let request_parts = context.request_parts();
println!("headers are {:?}", request_parts.headers);
Ok(())
}
#[component]
fn CallServerFunction() -> Element {
rsx! {
button {
// If you click the button, the server function will be called and the server context will be
// from the client request
onclick: move |_| async {
_ = read_headers().await
},
"Call server function"
}
}
}
§Example
#[server]
async fn read_headers() -> Result<(), ServerFnError> {
let server_context = server_context();
let headers: http::HeaderMap = server_context.extract().await?;
println!("{:?}", headers);
Ok(())
}
Trait Implementations§
Source§impl Clone for DioxusServerContext
impl Clone for DioxusServerContext
Source§fn clone(&self) -> DioxusServerContext
fn clone(&self) -> DioxusServerContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more