-
Notifications
You must be signed in to change notification settings - Fork 0
Emit versioned asset bundles for immutable caching #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,27 @@ fn mime_from_path(path: &str) -> &'static str { | |
| } | ||
|
|
||
| fn is_immutable(path: &str) -> bool { | ||
| path.starts_with("fonts/") || path.starts_with("vendor/") || path.starts_with("img/") | ||
| path.starts_with("fonts/") | ||
| || path.starts_with("vendor/") | ||
| || path.starts_with("img/") | ||
| || is_versioned_bundle(path) | ||
| } | ||
|
|
||
| fn is_versioned_bundle(path: &str) -> bool { | ||
| path.strip_prefix("sf.") | ||
| .and_then(|rest| rest.rsplit_once('.')) | ||
| .map(|(version, ext)| { | ||
| !version.is_empty() | ||
| && version.chars().all(|ch| { | ||
| ch.is_ascii_digit() | ||
| || ch == '.' | ||
| || ch == '-' | ||
| || ch == '+' | ||
| || ch.is_ascii_alphabetic() | ||
| }) | ||
|
Comment on lines
+67
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Cargo accepts package versions with build metadata such as Useful? React with 👍 / 👎. |
||
| && matches!(ext, "css" | "js") | ||
| }) | ||
| .unwrap_or(false) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -65,6 +85,17 @@ mod tests { | |
| }; | ||
| use tower::util::ServiceExt; | ||
|
|
||
| #[test] | ||
| fn versioned_bundles_are_detected() { | ||
| assert!(is_versioned_bundle("sf.0.1.0.css")); | ||
| assert!(is_versioned_bundle("sf.0.1.0.js")); | ||
| assert!(is_versioned_bundle("sf.0.2.0-beta.1.js")); | ||
| assert!(is_versioned_bundle("sf.0.1.0+build.7.css")); | ||
| assert!(!is_versioned_bundle("sf.css")); | ||
| assert!(!is_versioned_bundle("sf.js")); | ||
| assert!(!is_versioned_bundle("vendor/sf.0.1.0.js")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn caches_paths_are_predicted_correctly() { | ||
| assert_eq!(mime_from_path("styles/sf.css"), "text/css; charset=utf-8"); | ||
|
|
@@ -78,9 +109,20 @@ mod tests { | |
| assert!(is_immutable("fonts/jetbrains-mono.woff2")); | ||
| assert!(is_immutable("vendor/leaflet/leaflet.js")); | ||
| assert!(is_immutable("img/solverforge-logo.svg")); | ||
| assert!(is_immutable("sf.0.1.0.css")); | ||
| assert!(is_immutable("sf.0.1.0+build.7.js")); | ||
| assert!(!is_immutable("sf.css")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn mime_detection_still_works_for_versioned_assets() { | ||
| assert_eq!(mime_from_path("sf.0.1.0.css"), "text/css; charset=utf-8"); | ||
| assert_eq!( | ||
| mime_from_path("sf.0.1.0+build.7.js"), | ||
| "application/javascript; charset=utf-8" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn serves_assets_with_expected_headers() { | ||
| let app = routes(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sf.$(VERSION)bundles in the release pathThese rules make the emitted filenames depend on
$(VERSION), but the existing release flow does not rebuild them:.versionrc.jsononly bumpsCargo.toml, andpublishstill runscargo publishwithout depending onassets. In a0.1.0 -> 0.1.1release, that means the crate can be published with onlysf.0.1.0.{css,js}present, so the newly documented/sf/sf.0.1.1.cssand/sf/sf.0.1.1.jsURLs will 404 unless the maintainer remembers to runmake assetsmanually.Useful? React with 👍 / 👎.