Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions leptos/src/hydration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ pub fn AutoReload(
Some(val) => val,
None => options.reload_port,
};

let protocol = match options.reload_ws_protocol {
leptos_config::ReloadWSProtocol::Auto => "null",
leptos_config::ReloadWSProtocol::WS => "'ws://'",
leptos_config::ReloadWSProtocol::WSS => "'wss://'",

unrecognized => {
leptos::logging::error!(
"Unrecognized live reload protocol option '{}'",
unrecognized,
);

"null"
}
};

let script = format!(
Expand Down
7 changes: 4 additions & 3 deletions leptos/src/hydration/reload_script.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
if (window.location.protocol === 'https:') {
protocol = 'wss://';
let host = window.location.hostname;

if (protocol === null) {
protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
}

let host = window.location.hostname;
let ws = new WebSocket(`${protocol}${host}:${reload_port}/live_reload`);
ws.onmessage = (ev) => {
let msg = JSON.parse(ev.data);
Expand Down
25 changes: 20 additions & 5 deletions leptos_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::errors::LeptosConfigError;
use config::{Case, Config, File, FileFormat};
use regex::Regex;
use std::{
env::VarError, fs, net::SocketAddr, path::Path, str::FromStr, sync::Arc,
env::VarError, fmt::Display, fs, net::SocketAddr, path::Path, str::FromStr,
sync::Arc,
};
use typed_builder::TypedBuilder;

Expand Down Expand Up @@ -153,7 +154,7 @@ impl LeptosOptions {
None => None,
},
reload_ws_protocol: ws_from_str(
env_w_default("LEPTOS_RELOAD_WS_PROTOCOL", "ws")?.as_str(),
env_w_default("LEPTOS_RELOAD_WS_PROTOCOL", "auto")?.as_str(),
)?,
not_found_path: env_w_default("LEPTOS_NOT_FOUND_PATH", "/404")?
.into(),
Expand Down Expand Up @@ -222,6 +223,7 @@ fn env_w_default(
/// Setting this to the `PROD` variant will not include the WebSocket code for `cargo-leptos` watch mode.
/// Defaults to `DEV`.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
#[non_exhaustive]
pub enum Env {
PROD,
DEV,
Expand Down Expand Up @@ -280,25 +282,28 @@ impl TryFrom<String> for Env {
/// An enum that can be used to define the websocket protocol Leptos uses for hotreloading
/// Defaults to `ws`.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
#[non_exhaustive]
pub enum ReloadWSProtocol {
WS,
WSS,
Auto,
}

impl Default for ReloadWSProtocol {
fn default() -> Self {
Self::WS
Self::Auto
}
}

fn ws_from_str(input: &str) -> Result<ReloadWSProtocol, LeptosConfigError> {
let sanitized = input.to_lowercase();
match sanitized.as_ref() {
"auto" => Ok(ReloadWSProtocol::Auto),
"ws" | "WS" => Ok(ReloadWSProtocol::WS),
"wss" | "WSS" => Ok(ReloadWSProtocol::WSS),
_ => Err(LeptosConfigError::EnvVarError(format!(
"{input} is not a supported websocket protocol. Use only `ws` or \
`wss`.",
"{input} is not a supported websocket protocol. Use only `auto`, \
`ws` or `wss`.",
))),
}
}
Expand Down Expand Up @@ -333,6 +338,16 @@ impl TryFrom<String> for ReloadWSProtocol {
}
}

impl Display for ReloadWSProtocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ReloadWSProtocol::WS => f.write_str("ws"),
ReloadWSProtocol::WSS => f.write_str("wss"),
ReloadWSProtocol::Auto => f.write_str("auto"),
}
}
}

/// Loads [LeptosOptions] from a Cargo.toml text content with layered overrides.
/// If an env var is specified, like `LEPTOS_ENV`, it will override a setting in the file.
pub fn get_config_from_str(
Expand Down
14 changes: 14 additions & 0 deletions leptos_config/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ fn env_from_str_test() {

#[test]
fn ws_from_str_test() {
assert!(matches!(
ws_from_str("auto").unwrap(),
ReloadWSProtocol::Auto
));

assert!(matches!(ws_from_str("ws").unwrap(), ReloadWSProtocol::WS));
assert!(matches!(ws_from_str("WS").unwrap(), ReloadWSProtocol::WS));
assert!(matches!(ws_from_str("wss").unwrap(), ReloadWSProtocol::WSS));
Expand All @@ -28,6 +33,15 @@ fn ws_from_str_test() {
assert!(ws_from_str("?").is_err());
}

#[test]
fn ws_from_to_str_test() {
assert_eq!(ws_from_str("auto").unwrap().to_string(), "auto");
assert_eq!(ws_from_str("ws").unwrap().to_string(), "ws");
assert_eq!(ws_from_str("WS").unwrap().to_string(), "ws");
assert_eq!(ws_from_str("wss").unwrap().to_string(), "wss");
assert_eq!(ws_from_str("WSS").unwrap().to_string(), "wss");
}

#[test]
fn env_w_default_test() {
temp_env::with_var("LEPTOS_CONFIG_ENV_TEST", Some("custom"), || {
Expand Down
Loading