Skip to main content

ServeConfig

Struct ServeConfig 

Source
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

Source

pub fn builder() -> Self

Create a new ServeConfig with incremental static generation disabled and the default index.html settings.

Source

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.

Source

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.

Source

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);
Source

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:?}" }
    }
}
Source

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:?}" }
    }
}
Source

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:?}" }
    }
}
Source

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);
Source

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);

Trait Implementations§

Source§

impl Clone for ServeConfig

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for ServeConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl LaunchConfig for ServeConfig

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> InitializeFromFunction<T> for T

Source§

fn initialize_from_function(f: fn() -> T) -> T

Create an instance of this type from an initialization function
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<Ret> SpawnIfAsync<(), Ret> for Ret

Source§

fn spawn(self) -> Ret

Spawn the value into the dioxus runtime if it is an async block
Source§

impl<T, O> SuperFrom<T> for O
where O: From<T>,

Source§

fn super_from(input: T) -> O

Convert from a type to another type.
Source§

impl<T, O, M> SuperInto<O, M> for T
where O: SuperFrom<T, M>,

Source§

fn super_into(self) -> O

Convert from a type to another type.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more