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 { + 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 { 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(); } };