Skip to content

Commit 554f297

Browse files
authored
Merge pull request #1097 from dylanowen/cache
Prevent scrolling to the top of the page on websocket reload
2 parents 441a10b + b660346 commit 554f297

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/cmd/serve.rs

+15
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
use super::watch;
33
use crate::{get_book_dir, open};
44
use clap::{App, Arg, ArgMatches, SubCommand};
5+
use iron::headers;
56
use iron::{status, AfterMiddleware, Chain, Iron, IronError, IronResult, Request, Response, Set};
67
use mdbook::errors::*;
78
use mdbook::utils;
89
use mdbook::MDBook;
910

1011
struct ErrorRecover;
1112

13+
struct NoCache;
14+
1215
// Create clap subcommand arguments
1316
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
1417
SubCommand::with_name("serve")
@@ -86,6 +89,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
8689
book.build()?;
8790

8891
let mut chain = Chain::new(staticfile::Static::new(book.build_dir_for("html")));
92+
chain.link_after(NoCache);
8993
chain.link_after(ErrorRecover);
9094
let _iron = Iron::new(chain)
9195
.http(&*address)
@@ -133,6 +137,17 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
133137
Ok(())
134138
}
135139

140+
impl AfterMiddleware for NoCache {
141+
fn after(&self, _: &mut Request, mut res: Response) -> IronResult<Response> {
142+
res.headers.set(headers::CacheControl(vec![
143+
headers::CacheDirective::NoStore,
144+
headers::CacheDirective::MaxAge(0u32),
145+
]));
146+
147+
Ok(res)
148+
}
149+
}
150+
136151
impl AfterMiddleware for ErrorRecover {
137152
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
138153
match err.response.status {

src/theme/index.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
socket.onmessage = function (event) {
205205
if (event.data === "reload") {
206206
socket.close();
207-
location.reload(true); // force reload from server (not from cache)
207+
location.reload();
208208
}
209209
};
210210

0 commit comments

Comments
 (0)