summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDylan Owen <dylan.owen@workday.com>2019-11-12 18:06:11 -0800
committerDylan Owen <dylan.owen@workday.com>2019-11-12 18:06:11 -0800
commitb6603468d62207e6ae1c807d4ffe17478ce17766 (patch)
tree8144da0a008f2ff2300c6df422a09b78f4887a83
parent441a10bdd7d3072ff24b251f7e4a321ed78d4796 (diff)
Stop scrolling on socket reload
-rw-r--r--src/cmd/serve.rs15
-rw-r--r--src/theme/index.hbs2
2 files changed, 16 insertions, 1 deletions
diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs
index 3c8c53dc..77843028 100644
--- a/src/cmd/serve.rs
+++ b/src/cmd/serve.rs
@@ -2,6 +2,7 @@
use super::watch;
use crate::{get_book_dir, open};
use clap::{App, Arg, ArgMatches, SubCommand};
+use iron::headers;
use iron::{status, AfterMiddleware, Chain, Iron, IronError, IronResult, Request, Response, Set};
use mdbook::errors::*;
use mdbook::utils;
@@ -9,6 +10,8 @@ use mdbook::MDBook;
struct ErrorRecover;
+struct NoCache;
+
// Create clap subcommand arguments
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("serve")
@@ -86,6 +89,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
book.build()?;
let mut chain = Chain::new(staticfile::Static::new(book.build_dir_for("html")));
+ chain.link_after(NoCache);
chain.link_after(ErrorRecover);
let _iron = Iron::new(chain)
.http(&*address)
@@ -133,6 +137,17 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
Ok(())
}
+impl AfterMiddleware for NoCache {
+ fn after(&self, _: &mut Request, mut res: Response) -> IronResult<Response> {
+ res.headers.set(headers::CacheControl(vec![
+ headers::CacheDirective::NoStore,
+ headers::CacheDirective::MaxAge(0u32),
+ ]));
+
+ Ok(res)
+ }
+}
+
impl AfterMiddleware for ErrorRecover {
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
match err.response.status {
diff --git a/src/theme/index.hbs b/src/theme/index.hbs
index 3a0cb4a8..b205670f 100644
--- a/src/theme/index.hbs
+++ b/src/theme/index.hbs
@@ -204,7 +204,7 @@
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
- location.reload(true); // force reload from server (not from cache)
+ location.reload();
}
};