pub struct ServeConfig { /* private fields */ }Expand description
A ServeConfig is used to configure how to serve a Dioxus application. It contains information about how to serve static assets, and what content to render with dioxus_ssr.
Implementations§
Source§impl ServeConfig
impl ServeConfig
Sourcepub fn builder() -> Self
pub fn builder() -> Self
Create a new ServeConfig with incremental static generation disabled and the default index.html settings.
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new ServeConfig with incremental static generation disabled and the default index.html settings
This will automatically use the index.html file in the /public directory if it exists.
The /public folder is meant located next to the current executable. If no index.html file is found,
a default index.html will be used, which will not include any JavaScript or WASM initialization code.
To provide an alternate index.html, you can use with_index_html method instead.
Sourcepub fn with_index_html(index: IndexHtml) -> Self
pub fn with_index_html(index: IndexHtml) -> Self
Create a new ServeConfig with the given parsed IndexHtml structure.
You can create the IndexHtml structure by using IndexHtml::new method, or manually from
a string or file.
Sourcepub fn incremental(self, cfg: IncrementalRendererConfig) -> Self
pub fn incremental(self, cfg: IncrementalRendererConfig) -> Self
Enable incremental static generation. Incremental static generation caches the rendered html in memory and/or the file system. It can be used to improve performance of heavy routes.
use dioxus::prelude::*;
// Finally, launch the app with the config
LaunchBuilder::new()
// Only set the server config if the server feature is enabled
.with_cfg(server_only!(dioxus_server::ServeConfig::default().incremental(dioxus_server::IncrementalRendererConfig::default())))
.launch(app);Sourcepub fn context_providers(
self,
state: Arc<Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>>>,
) -> Self
pub fn context_providers( self, state: Arc<Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>>>, ) -> Self
Provide context to the root and server functions. You can use this context while rendering with consume_context.
The context providers passed into this method will be called when the context type is requested which may happen many times in the lifecycle of the application.
Context will be forwarded from the LaunchBuilder if it is provided.
#![allow(non_snake_case)]
use dioxus::prelude::*;
use std::sync::Arc;
use std::any::Any;
fn main() {
// Hydrate the application on the client
#[cfg(not(feature = "server"))]
dioxus::launch(app);
#[cfg(feature = "server")]
dioxus_server::serve(|| async move {
use dioxus_server::{axum, ServeConfig, DioxusRouterExt};
let config = ServeConfig::default()
// You can provide context to your whole app on the server (including server functions) with the `context_provider` method on the launch builder
.context_providers(Arc::new(vec![Box::new(|| Box::new(1234u32) as Box<dyn Any>) as Box<dyn Fn() -> Box<dyn Any> + Send + Sync>]));
Ok(
axum::Router::new()
.serve_dioxus_application(config, app)
)
})
}
#[server]
async fn read_context() -> ServerFnResult<u32> {
Ok(123)
}
fn app() -> Element {
let future = use_resource(read_context);
rsx! {
h1 { "{future:?}" }
}
}Sourcepub fn context_provider<C: 'static>(
self,
state: impl Fn() -> C + Send + Sync + 'static,
) -> Self
pub fn context_provider<C: 'static>( self, state: impl Fn() -> C + Send + Sync + 'static, ) -> Self
Provide context to the root and server functions. You can use this context
while rendering with consume_context.
The context providers passed into this method will be called when the context type is requested which may happen many times in the lifecycle of the application.
Context will be forwarded from the LaunchBuilder if it is provided.
#![allow(non_snake_case)]
use dioxus::prelude::*;
fn main() {
#[cfg(not(feature = "server"))]
// Hydrate the application on the client
dioxus::launch(app);
#[cfg(feature = "server")]
dioxus_server::serve(|| async move {
use dioxus_server::{axum, ServeConfig, DioxusRouterExt};
let config = ServeConfig::default()
// You can provide context to your whole app on the server (including server functions) with the `context_provider` method on the launch builder
.context_provider(|| 1234u32);
Ok(
axum::Router::new()
.serve_dioxus_application(config, app)
)
});
}
#[server]
async fn read_context() -> ServerFnResult<u32> {
Ok(123)
}
fn app() -> Element {
let future = use_resource(read_context);
rsx! {
h1 { "{future:?}" }
}
}Sourcepub fn context(self, state: impl Any + Clone + Send + Sync + 'static) -> Self
pub fn context(self, state: impl Any + Clone + Send + Sync + 'static) -> Self
Provide context to the root and server functions. You can use this context while rendering with consume_context.
Context will be forwarded from the LaunchBuilder if it is provided.
#![allow(non_snake_case)]
use dioxus::prelude::*;
fn main() {
// Hydrate the application on the client
#[cfg(not(feature = "server"))]
dioxus::launch(app);
// Run a custom server with axum on the server
#[cfg(feature = "server")]
dioxus_server::serve(|| async move {
use dioxus_server::{axum, ServeConfig, DioxusRouterExt};
let config = ServeConfig::default()
// You can provide context to your whole app on the server (including server functions) with the `context_provider` method on the launch builder
.context(1234u32);
Ok(
axum::Router::new()
.serve_dioxus_application(config, app)
)
});
}
#[server]
async fn read_context() -> ServerFnResult<u32> {
Ok(123)
}
fn app() -> Element {
let future = use_resource(read_context);
rsx! {
h1 { "{future:?}" }
}
}Sourcepub fn streaming_mode(self, mode: StreamingMode) -> Self
pub fn streaming_mode(self, mode: StreamingMode) -> Self
Set the streaming mode for the server. By default, streaming is disabled.
dioxus::LaunchBuilder::new()
.with_context(server_only! {
dioxus::server::ServeConfig::builder().streaming_mode(dioxus::server::StreamingMode::OutOfOrder)
})
.launch(app);Sourcepub fn enable_out_of_order_streaming(self) -> Self
pub fn enable_out_of_order_streaming(self) -> Self
Enable out of order streaming. This will cause server futures to be resolved out of order and streamed to the client as they resolve.
It is equivalent to calling streaming_mode(StreamingMode::OutOfOrder)
dioxus::LaunchBuilder::new()
.with_context(server_only! {
dioxus::server::ServeConfig::builder().enable_out_of_order_streaming()
})
.launch(app);