support cache-control for static resources

This commit is contained in:
Peter Cai 2020-04-13 18:10:34 +08:00
parent e6df5544f4
commit 44ca2f6de3
No known key found for this signature in database
GPG Key ID: 71F5FB4E4F3FD54F
4 changed files with 18 additions and 2 deletions

View File

@ -66,6 +66,7 @@ This is the main configuration file. The file will be compiled statically into t
"description": "<description>", "description": "<description>",
"plugin_identifier": "com.example.change.this.to.whatever.you.like", "plugin_identifier": "com.example.change.this.to.whatever.you.like",
"posts_per_page": 5, "posts_per_page": 5,
"cache_maxage": 86400,
"redirects": { "redirects": {
"/foo": "/bar", "/foo": "/bar",
... ...
@ -87,6 +88,8 @@ This is the main configuration file. The file will be compiled statically into t
`redirects`: OPTIONAL. A map of URLs where the key will be mapped to the value by Paprika using 301 redirects. This is mainly useful for migration from another blogging platform. `redirects`: OPTIONAL. A map of URLs where the key will be mapped to the value by Paprika using 301 redirects. This is mainly useful for migration from another blogging platform.
`cache_maxage`: OPTIONAL. A value in seconds determining how long the browser should cache static resources from the blog. If omitted, the default value is a week.
`hljs`: An array of language support from `highlight.js` to be included in the final binary. The full `highlight.js` is notoriously huge and there's really no reason to include a bazillion languages you will never actually use in your blog posts. This will be read by `build.rs` to generate a JS shim that will load all languages in the array to the final binary via `webpack` support for `require`. `hljs`: An array of language support from `highlight.js` to be included in the final binary. The full `highlight.js` is notoriously huge and there's really no reason to include a bazillion languages you will never actually use in your blog posts. This will be read by `build.rs` to generate a JS shim that will load all languages in the array to the final binary via `webpack` support for `require`.
Configuration: theme_config.json Configuration: theme_config.json

View File

@ -43,6 +43,10 @@ lazy_static! {
pub static ref CONFIG: utils::Config = { pub static ref CONFIG: utils::Config = {
serde_json::from_str(std::include_str!("../config.json")).unwrap() serde_json::from_str(std::include_str!("../config.json")).unwrap()
}; };
pub static ref CACHE_CONTROL_STATIC_FILE: String = {
format!("max-age={}", CONFIG.cache_maxage)
};
} }
fn build_routes() -> router::Router { fn build_routes() -> router::Router {
@ -97,7 +101,8 @@ async fn proxy_remote_image(req: Request, url: Url) -> MyResult<Response> {
ResponseInit::new() ResponseInit::new()
.status(remote_resp.status()) .status(remote_resp.status())
.headers(headers!{ .headers(headers!{
"Content-Type" => &get_header!(remote_headers, "content-type") "Content-Type" => &get_header!(remote_headers, "content-type"),
"Cache-Control" => &CACHE_CONTROL_STATIC_FILE
}.as_ref()) }.as_ref())
).internal_err() ).internal_err()
} }

View File

@ -29,7 +29,8 @@ async fn serve_static(_req: Request, url: Url) -> MyResult<Response> {
ResponseInit::new() ResponseInit::new()
.status(200) .status(200)
.headers(headers!{ .headers(headers!{
"Content-Type" => mime_guess::from_path(path).first().unwrap().essence_str() "Content-Type" => mime_guess::from_path(path).first().unwrap().essence_str(),
"Cache-Control" => &crate::CACHE_CONTROL_STATIC_FILE
}.as_ref()) }.as_ref())
).internal_err() ).internal_err()
} else { } else {

View File

@ -156,10 +156,17 @@ pub struct Config {
pub plugin_identifier: String, pub plugin_identifier: String,
// How many posts to show in one page // How many posts to show in one page
pub posts_per_page: usize, pub posts_per_page: usize,
// The browser cache timeout for static resources in seconds
#[serde(default = "default_maxage")]
pub cache_maxage: u64,
// Hard-coded redirects (for migrating old articles and such) // Hard-coded redirects (for migrating old articles and such)
// Paths here MUST include the starting "/" // Paths here MUST include the starting "/"
// UNLIKE in article headers // UNLIKE in article headers
pub redirects: Option<HashMap<String, String>> pub redirects: Option<HashMap<String, String>>
} }
fn default_maxage() -> u64 {
60 * 60 * 24 * 7 // default to a week
}
include!(concat!(env!("OUT_DIR"), "/build_timestamp.rs")); include!(concat!(env!("OUT_DIR"), "/build_timestamp.rs"));