Skip to content

Commit 566451e

Browse files
authored
Merge pull request #1771 from FWYongxing/master
livereload uses host, port and HTTP(S) protocol of current page
2 parents 1562629 + 6899d94 commit 566451e

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

src/cmd/serve.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
6262

6363
let address = format!("{}:{}", hostname, port);
6464

65-
let livereload_url = format!("ws://{}/{}", address, LIVE_RELOAD_ENDPOINT);
6665
let update_config = |book: &mut MDBook| {
6766
book.config
68-
.set("output.html.livereload-url", &livereload_url)
69-
.expect("livereload-url update failed");
67+
.set("output.html.live-reload-endpoint", &LIVE_RELOAD_ENDPOINT)
68+
.expect("live-reload-endpoint update failed");
7069
if let Some(dest_dir) = args.value_of("dest-dir") {
7170
book.config.build.build_dir = dest_dir.into();
7271
}

src/config.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -531,16 +531,16 @@ pub struct HtmlConfig {
531531
pub cname: Option<String>,
532532
/// Edit url template, when set shows a "Suggest an edit" button for
533533
/// directly jumping to editing the currently viewed page.
534-
/// Contains {path} that is replaced with chapter source file path
534+
/// Contains {path} that is replaced with chapter source file path[[[[
535535
pub edit_url_template: Option<String>,
536-
/// This is used as a bit of a workaround for the `mdbook serve` command.
537-
/// Basically, because you set the websocket port from the command line, the
538-
/// `mdbook serve` command needs a way to let the HTML renderer know where
539-
/// to point livereloading at, if it has been enabled.
536+
/// Endpoint of websocket, for livereload usage. Value loaded from .toml file
537+
/// is ignored, because our code overrides this field with the value [`LIVE_RELOAD_ENDPOINT`]
538+
///
539+
/// [`LIVE_RELOAD_ENDPOINT`]: cmd::serve::LIVE_RELOAD_ENDPOINT
540540
///
541541
/// This config item *should not be edited* by the end user.
542542
#[doc(hidden)]
543-
pub livereload_url: Option<String>,
543+
pub live_reload_endpoint: Option<String>,
544544
/// The mapping from old pages to new pages/URLs to use when generating
545545
/// redirects.
546546
pub redirect: HashMap<String, String>,
@@ -569,7 +569,7 @@ impl Default for HtmlConfig {
569569
input_404: None,
570570
site_url: None,
571571
cname: None,
572-
livereload_url: None,
572+
live_reload_endpoint: None,
573573
redirect: HashMap::new(),
574574
}
575575
}

src/renderer/html_handlebars/hbs_renderer.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,11 @@ fn make_data(
606606
if theme.favicon_svg.is_some() {
607607
data.insert("favicon_svg".to_owned(), json!("favicon.svg"));
608608
}
609-
if let Some(ref livereload) = html_config.livereload_url {
610-
data.insert("livereload".to_owned(), json!(livereload));
609+
if let Some(ref live_reload_endpoint) = html_config.live_reload_endpoint {
610+
data.insert(
611+
"live_reload_endpoint".to_owned(),
612+
json!(live_reload_endpoint),
613+
);
611614
}
612615

613616
let default_theme = match html_config.default_theme {

src/theme/index.hbs

+4-2
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,12 @@
219219

220220
</div>
221221

222-
{{#if livereload}}
222+
{{#if live_reload_endpoint}}
223223
<!-- Livereload script (if served using the cli tool) -->
224224
<script type="text/javascript">
225-
var socket = new WebSocket("{{{livereload}}}");
225+
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
226+
const wsAddress = wsProtocol + "//" + location.host + "/" + "{{{live_reload_endpoint}}}";
227+
const socket = new WebSocket(wsAddress);
226228
socket.onmessage = function (event) {
227229
if (event.data === "reload") {
228230
socket.close();

0 commit comments

Comments
 (0)