index : paprika

A blogging platform written in Rust for Cloudflare Workers, integrated with Standard Notes

diff options
context:
space:
mode:
authorPeter Cai <[email protected]>2020-04-13 18:10:34 +0800
committerPeter Cai <[email protected]>2020-04-13 18:10:34 +0800
commit44ca2f6de32f8fcc4180ece889207a6e6b29af41 (patch)
treeffb1b4b6c9b35f1aaca533cbcb67e7d14ec31bf2
parente6df5544f4095ef0ba262c9dd54277f220509e66 (diff)
downloadpaprika-44ca2f6de32f8fcc4180ece889207a6e6b29af41.tar.gz
support cache-control for static resources
-rw-r--r--README.md3
-rw-r--r--src/lib.rs7
-rw-r--r--src/render.rs3
-rw-r--r--src/utils.rs7
4 files changed, 18 insertions, 2 deletions
diff --git a/README.md b/README.md
index c235396..056b52b 100644
--- a/README.md
+++ b/README.md
@@ -66,6 +66,7 @@ This is the main configuration file. The file will be compiled statically into t
"description": "<description>",
"plugin_identifier": "com.example.change.this.to.whatever.you.like",
"posts_per_page": 5,
+ "cache_maxage": 86400,
"redirects": {
"/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.
+`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`.
Configuration: theme_config.json
diff --git a/src/lib.rs b/src/lib.rs
index 6f18c62..ddb3e63 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -43,6 +43,10 @@ lazy_static! {
pub static ref CONFIG: utils::Config = {
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 {
@@ -97,7 +101,8 @@ async fn proxy_remote_image(req: Request, url: Url) -> MyResult<Response> {
ResponseInit::new()
.status(remote_resp.status())
.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())
).internal_err()
}
diff --git a/src/render.rs b/src/render.rs
index fdbb38a..2c6dc34 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -29,7 +29,8 @@ async fn serve_static(_req: Request, url: Url) -> MyResult<Response> {
ResponseInit::new()
.status(200)
.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())
).internal_err()
} else {
diff --git a/src/utils.rs b/src/utils.rs
index f1a288d..3dc93ea 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -156,10 +156,17 @@ pub struct Config {
pub plugin_identifier: String,
// How many posts to show in one page
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)
// Paths here MUST include the starting "/"
// UNLIKE in article headers
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")); \ No newline at end of file