mirror of
https://github.com/rust-lang/mdBook
synced 2024-12-14 14:52:37 +00:00
Merge pull request #1097 from dylanowen/cache
Prevent scrolling to the top of the page on websocket reload
This commit is contained in:
commit
554f29703f
2 changed files with 16 additions and 1 deletions
|
@ -2,6 +2,7 @@
|
||||||
use super::watch;
|
use super::watch;
|
||||||
use crate::{get_book_dir, open};
|
use crate::{get_book_dir, open};
|
||||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||||
|
use iron::headers;
|
||||||
use iron::{status, AfterMiddleware, Chain, Iron, IronError, IronResult, Request, Response, Set};
|
use iron::{status, AfterMiddleware, Chain, Iron, IronError, IronResult, Request, Response, Set};
|
||||||
use mdbook::errors::*;
|
use mdbook::errors::*;
|
||||||
use mdbook::utils;
|
use mdbook::utils;
|
||||||
|
@ -9,6 +10,8 @@ use mdbook::MDBook;
|
||||||
|
|
||||||
struct ErrorRecover;
|
struct ErrorRecover;
|
||||||
|
|
||||||
|
struct NoCache;
|
||||||
|
|
||||||
// Create clap subcommand arguments
|
// Create clap subcommand arguments
|
||||||
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
||||||
SubCommand::with_name("serve")
|
SubCommand::with_name("serve")
|
||||||
|
@ -86,6 +89,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
book.build()?;
|
book.build()?;
|
||||||
|
|
||||||
let mut chain = Chain::new(staticfile::Static::new(book.build_dir_for("html")));
|
let mut chain = Chain::new(staticfile::Static::new(book.build_dir_for("html")));
|
||||||
|
chain.link_after(NoCache);
|
||||||
chain.link_after(ErrorRecover);
|
chain.link_after(ErrorRecover);
|
||||||
let _iron = Iron::new(chain)
|
let _iron = Iron::new(chain)
|
||||||
.http(&*address)
|
.http(&*address)
|
||||||
|
@ -133,6 +137,17 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
|
||||||
Ok(())
|
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 {
|
impl AfterMiddleware for ErrorRecover {
|
||||||
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
|
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
|
||||||
match err.response.status {
|
match err.response.status {
|
||||||
|
|
|
@ -204,7 +204,7 @@
|
||||||
socket.onmessage = function (event) {
|
socket.onmessage = function (event) {
|
||||||
if (event.data === "reload") {
|
if (event.data === "reload") {
|
||||||
socket.close();
|
socket.close();
|
||||||
location.reload(true); // force reload from server (not from cache)
|
location.reload();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue