Right now, docs.rs passes --static-root-path=/ to rustdoc, and static files are served directly from the docs.rs root, e.g. https://docs.rs/storage-20220709-1.64.0-nightly-6dba4ed21.js.
It would be aesthetically pleasing if these static files had their own directory. For instance, we could pass --static-root-path=/rustdoc.static/, and simultaneously change the storage path at which we upload these files to be /rustdoc.static/. Then static files would be served from URLs like `https://docs.rs/rustdoc.static/storage-20221225-1.68.0-nightly-abc1234567.js.
This does not require any coordinated changes from rustdoc. We could do it today. It happens to harmonize with (but not depend on) rust-lang/rust#101702, which envisions a similar change rustdoc's default output, where static files are put in their own directory.
It does depend on a tweak to how we serve static files, specifically SharedResourceHandler:
|
pub struct SharedResourceHandler; |
|
|
|
impl Handler for SharedResourceHandler { |
|
fn handle(&self, req: &mut Request) -> IronResult<Response> { |
|
let path = req.url.path(); |
|
let filename = path.last().unwrap(); // unwrap is fine: vector is non-empty |
|
if let Some(extension) = Path::new(filename).extension() { |
|
if ["js", "css", "woff", "woff2", "svg", "png"] |
|
.iter() |
|
.any(|s| *s == extension) |
|
{ |
|
let storage = extension!(req, Storage); |
|
let config = extension!(req, Config); |
|
|
|
if let Ok(file) = File::from_path(storage, filename, config) { |
|
return Ok(file.serve()); |
|
} |
|
} |
|
} |
|
|
|
// Just always return a 404 here - the main handler will then try the other handlers |
|
Err(Nope::ResourceNotFound.into()) |
|
} |
|
} |
SharedResourceHandler considers only the last component of a URL's path when serving ["js", "css", "woff", "woff2", "svg", "png"]. We could add a small bit of extra logic: If a URL's path begins with /rustdoc.static, consider the full path.
Right now, docs.rs passes
--static-root-path=/to rustdoc, and static files are served directly from the docs.rs root, e.g. https://docs.rs/storage-20220709-1.64.0-nightly-6dba4ed21.js.It would be aesthetically pleasing if these static files had their own directory. For instance, we could pass
--static-root-path=/rustdoc.static/, and simultaneously change the storage path at which we upload these files to be/rustdoc.static/. Then static files would be served from URLs like `https://docs.rs/rustdoc.static/storage-20221225-1.68.0-nightly-abc1234567.js.This does not require any coordinated changes from rustdoc. We could do it today. It happens to harmonize with (but not depend on) rust-lang/rust#101702, which envisions a similar change rustdoc's default output, where static files are put in their own directory.
It does depend on a tweak to how we serve static files, specifically SharedResourceHandler:
docs.rs/src/web/rustdoc.rs
Lines 726 to 749 in 802fd8e
SharedResourceHandler considers only the last component of a URL's path when serving
["js", "css", "woff", "woff2", "svg", "png"]. We could add a small bit of extra logic: If a URL's path begins with/rustdoc.static, consider the full path.