Skip to content

Commit

Permalink
fix: caching!
Browse files Browse the repository at this point in the history
  • Loading branch information
vidhanio committed Nov 26, 2023
1 parent 8ed22b9 commit b41fe8f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*

!/.git/
!/src/
!/build.rs
!/Cargo.toml
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ serde_yaml = "0.9"
thiserror = "1"
time = { version = "0.3", features = ["macros", "serde-human-readable"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tower = { version = "0.4", features = ["util"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
tree-sitter = "0.20"
Expand Down
20 changes: 20 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use std::{env, error::Error, path::PathBuf, process::Command};

fn main() -> Result<(), Box<dyn Error>> {
build_tailwind()?;
set_git_hash()?;

Ok(())
}

fn build_tailwind() -> Result<(), Box<dyn Error>> {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);

let tailwind_config_file = manifest_dir.join("tailwind.config.js");
Expand Down Expand Up @@ -39,3 +46,16 @@ fn main() -> Result<(), Box<dyn Error>> {

Ok(())
}

fn set_git_hash() -> Result<(), Box<dyn Error>> {
let git_out = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()?
.stdout;

let git_hash = String::from_utf8(git_out)?;

println!("cargo:rustc-env=GIT_HASH={git_hash}");

Ok(())
}
2 changes: 1 addition & 1 deletion src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn document(path: Option<&str>, content: Markup) -> Markup {
html lang="en" {
head {
(seo(path))
link rel="stylesheet" href="/static/styles.css";
link rel="stylesheet" href=(concat!("/static/styles.css?git-hash=", env!("GIT_HASH")));
}

body.min-h-screen."px-[10%]"."lg:px-[25%]".flex.flex-col.items-center.font-mono."bg-stone-100"."dark:bg-stone-900"."dark:text-stone-300"."text-stone-700" {
Expand Down
16 changes: 14 additions & 2 deletions src/static.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::{ffi::OsStr, path::PathBuf};
use std::{ffi::OsStr, path::PathBuf, time::Duration};

use axum::{extract::Path, headers::ContentType, Router, TypedHeader};
use axum::{
extract::Path,
headers::{CacheControl, ContentType},
Router, TypedHeader,
};
use axum_extra::response::Css;
use include_dir::{include_dir, Dir};
use tower::ServiceBuilder;
use tracing::instrument;

use crate::{App, Error};
Expand All @@ -13,6 +18,13 @@ pub fn router() -> Router<App> {
.route("/LICENSE.txt", axum::routing::get(license))
.route("/styles.css", axum::routing::get(styles))
.route("/fonts/:font", axum::routing::get(fonts))
.layer(ServiceBuilder::new().map_response(|res| {
let cc = CacheControl::new()
.with_max_age(Duration::from_secs(60 * 60 * 24 * 365))
.with_immutable();

(TypedHeader(cc), res)
}))
}

macro_rules! static_path {
Expand Down

0 comments on commit b41fe8f

Please sign in to comment.