Skip to main content

redirect_external

Function redirect_external 

Source
pub fn redirect_external(url: &str)
Expand description

Redirect to an external URL. Works correctly during both SSR (server-side rendering) and client-side navigation.

During SSR: sets HTTP 302 (Found) status and a Location header on the response via [FullstackContext], causing a real HTTP redirect before any HTML reaches the browser.

On the client (post-hydration): uses [navigator().replace()] with NavigationTarget::External for a client-side navigation.

§Arguments

  • url - The external URL to redirect to.

§Example

use dioxus::prelude::*;
use dx_utils::redirect_external;

#[component]
fn MyGuard() -> Element {
    let auth = use_server_future(|| api::check_auth())?;
    let binding = auth.read();
    let status = binding.as_ref().and_then(|r| r.as_ref().ok());

    if let Some(s) = status {
        if !s.authenticated {
            redirect_external(&s.login_url);
            return rsx! {};
        }
    }

    rsx! { Outlet::<Route> {} }
}