Skip to content

Commit

Permalink
Stop scrolling on socket reload
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanowen committed Nov 13, 2019
1 parent 441a10b commit b660346
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
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;
use mdbook::MDBook;

struct ErrorRecover;

struct NoCache;

// Create clap subcommand arguments
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("serve")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};
Expand Down

0 comments on commit b660346

Please sign in to comment.