|
2 | 2 | use super::watch;
|
3 | 3 | use crate::{get_book_dir, open};
|
4 | 4 | use clap::{App, Arg, ArgMatches, SubCommand};
|
| 5 | +use iron::headers; |
5 | 6 | use iron::{status, AfterMiddleware, Chain, Iron, IronError, IronResult, Request, Response, Set};
|
6 | 7 | use mdbook::errors::*;
|
7 | 8 | use mdbook::utils;
|
8 | 9 | use mdbook::MDBook;
|
9 | 10 |
|
10 | 11 | struct ErrorRecover;
|
11 | 12 |
|
| 13 | +struct NoCache; |
| 14 | + |
12 | 15 | // Create clap subcommand arguments
|
13 | 16 | pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
14 | 17 | SubCommand::with_name("serve")
|
@@ -86,6 +89,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
|
86 | 89 | book.build()?;
|
87 | 90 |
|
88 | 91 | let mut chain = Chain::new(staticfile::Static::new(book.build_dir_for("html")));
|
| 92 | + chain.link_after(NoCache); |
89 | 93 | chain.link_after(ErrorRecover);
|
90 | 94 | let _iron = Iron::new(chain)
|
91 | 95 | .http(&*address)
|
@@ -133,6 +137,17 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
|
133 | 137 | Ok(())
|
134 | 138 | }
|
135 | 139 |
|
| 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 | + |
136 | 151 | impl AfterMiddleware for ErrorRecover {
|
137 | 152 | fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
|
138 | 153 | match err.response.status {
|
|
0 commit comments