Setup cache busting for style.css (#100)

This commit is contained in:
Paolo Barbolini 2021-02-10 12:49:35 +01:00 committed by GitHub
parent 461dfe2fbd
commit b0243d5dac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 9 deletions

60
Cargo.lock generated
View file

@ -73,6 +73,15 @@ dependencies = [
"constant_time_eq", "constant_time_eq",
] ]
[[package]]
name = "block-buffer"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
dependencies = [
"generic-array",
]
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.4.0" version = "3.4.0"
@ -168,6 +177,12 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]]
name = "cpuid-bool"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
[[package]] [[package]]
name = "crates-index" name = "crates-index"
version = "0.15.0" version = "0.15.0"
@ -254,6 +269,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
dependencies = [
"generic-array",
]
[[package]] [[package]]
name = "dirs" name = "dirs"
version = "2.0.2" version = "2.0.2"
@ -416,6 +440,16 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "generic-array"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
dependencies = [
"typenum",
"version_check",
]
[[package]] [[package]]
name = "getopts" name = "getopts"
version = "0.2.21" version = "0.2.21"
@ -871,6 +905,12 @@ version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"
[[package]]
name = "opaque-debug"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]] [[package]]
name = "openssl" name = "openssl"
version = "0.10.31" version = "0.10.31"
@ -1399,6 +1439,19 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "sha-1"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4b312c3731e3fe78a185e6b9b911a7aa715b8e31cce117975219aab2acf285d"
dependencies = [
"block-buffer",
"cfg-if 1.0.0",
"cpuid-bool",
"digest",
"opaque-debug",
]
[[package]] [[package]]
name = "shiny-robots" name = "shiny-robots"
version = "0.1.0" version = "0.1.0"
@ -1424,6 +1477,7 @@ dependencies = [
"semver 0.10.0", "semver 0.10.0",
"serde", "serde",
"serde_json", "serde_json",
"sha-1",
"slog", "slog",
"slog-async", "slog-async",
"slog-term", "slog-term",
@ -1739,6 +1793,12 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc" checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc"
[[package]]
name = "typenum"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]] [[package]]
name = "ucd-trie" name = "ucd-trie"
version = "0.1.3" version = "0.1.3"

View file

@ -40,3 +40,4 @@ toml = "0.5"
[build-dependencies] [build-dependencies]
sass-rs = "0.2" sass-rs = "0.2"
sha-1 = "0.9"

View file

@ -1,10 +1,11 @@
extern crate sass_rs as sass; extern crate sass_rs as sass;
use std::env; use std::env;
use std::fs::File; use std::fs;
use std::io::Write;
use std::path::Path; use std::path::Path;
use sha1::{Digest, Sha1};
fn build_style() -> String { fn build_style() -> String {
let options = sass::Options { let options = sass::Options {
output_style: sass::OutputStyle::Compressed, output_style: sass::OutputStyle::Compressed,
@ -16,8 +17,13 @@ fn build_style() -> String {
fn main() { fn main() {
let out_dir = env::var("OUT_DIR").unwrap(); let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("style.css");
let mut f = File::create(&dest_path).unwrap();
f.write_all(build_style().as_bytes()).unwrap(); let style = build_style();
let css_path = Path::new(&out_dir).join("style.css");
fs::write(css_path, style.as_bytes()).unwrap();
let hash_path = Path::new(&out_dir).join("style.css.sha1");
let digest = Sha1::digest(style.as_bytes());
fs::write(hash_path, format!("{:x}", digest)).unwrap();
} }

View file

@ -1,2 +1,12 @@
pub static STATIC_STYLE_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/style.css")); pub static STATIC_STYLE_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/style.css"));
pub const STATIC_STYLE_CSS_PATH: &str = concat!(
"/static/style.",
include_str!(concat!(env!("OUT_DIR"), "/style.css.sha1")),
".css"
);
pub const STATIC_STYLE_CSS_ETAG: &str = concat!(
"\"",
include_str!(concat!(env!("OUT_DIR"), "/style.css.sha1")),
"\""
);
pub static STATIC_FAVICON: &[u8] = include_bytes!("../../assets/logo.svg"); pub static STATIC_FAVICON: &[u8] = include_bytes!("../../assets/logo.svg");

View file

@ -2,7 +2,7 @@ use std::{env, sync::Arc, time::Instant};
use futures::future; use futures::future;
use hyper::{ use hyper::{
header::{CONTENT_TYPE, LOCATION}, header::{CACHE_CONTROL, CONTENT_TYPE, ETAG, LOCATION},
Body, Error as HyperError, Method, Request, Response, StatusCode, Body, Error as HyperError, Method, Request, Response, StatusCode,
}; };
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
@ -13,6 +13,7 @@ use slog::{error, info, o, Logger};
mod assets; mod assets;
mod views; mod views;
use self::assets::{STATIC_STYLE_CSS_ETAG, STATIC_STYLE_CSS_PATH};
use crate::engine::{AnalyzeDependenciesOutcome, Engine}; use crate::engine::{AnalyzeDependenciesOutcome, Engine};
use crate::models::crates::{CrateName, CratePath}; use crate::models::crates::{CrateName, CratePath};
use crate::models::repo::RepoPath; use crate::models::repo::RepoPath;
@ -51,7 +52,7 @@ impl App {
router.add("/", Route::Index); router.add("/", Route::Index);
router.add("/static/style.css", Route::Static(StaticFile::StyleCss)); router.add(STATIC_STYLE_CSS_PATH, Route::Static(StaticFile::StyleCss));
router.add("/static/logo.svg", Route::Static(StaticFile::FaviconPng)); router.add("/static/logo.svg", Route::Static(StaticFile::FaviconPng));
router.add( router.add(
@ -347,7 +348,9 @@ impl App {
fn static_file(file: StaticFile) -> Response<Body> { fn static_file(file: StaticFile) -> Response<Body> {
match file { match file {
StaticFile::StyleCss => Response::builder() StaticFile::StyleCss => Response::builder()
.header(CONTENT_TYPE, "text/css") .header(CONTENT_TYPE, "text/css; charset=utf-8")
.header(ETAG, STATIC_STYLE_CSS_ETAG)
.header(CACHE_CONTROL, "public, max-age=365000000, immutable")
.body(Body::from(assets::STATIC_STYLE_CSS)) .body(Body::from(assets::STATIC_STYLE_CSS))
.unwrap(), .unwrap(),
StaticFile::FaviconPng => Response::builder() StaticFile::FaviconPng => Response::builder()

View file

@ -8,6 +8,7 @@ pub mod error;
pub mod index; pub mod index;
pub mod status; pub mod status;
use crate::server::assets::STATIC_STYLE_CSS_PATH;
use crate::server::SELF_BASE_URL; use crate::server::SELF_BASE_URL;
fn render_html<B: Render>(title: &str, body: B) -> Response<Body> { fn render_html<B: Render>(title: &str, body: B) -> Response<Body> {
@ -18,7 +19,7 @@ fn render_html<B: Render>(title: &str, body: B) -> Response<Body> {
meta name="viewport" content="width=device-width, initial-scale=1"; meta name="viewport" content="width=device-width, initial-scale=1";
title { (format!("{} - Deps.rs", title)) } title { (format!("{} - Deps.rs", title)) }
link rel="icon" type="image/svg+xml" href="/static/logo.svg"; link rel="icon" type="image/svg+xml" href="/static/logo.svg";
link rel="stylesheet" type="text/css" href="/static/style.css"; link rel="stylesheet" type="text/css" href=(STATIC_STYLE_CSS_PATH);
link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Fira+Sans:400,500,600"; link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Fira+Sans:400,500,600";
link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Source+Code+Pro"; link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Source+Code+Pro";
link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"; link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css";