Skip to content
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

Load release list only when needed #2113

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,53 @@ pub(crate) async fn crate_details_handler(
Ok(res.into_response())
}

#[derive(Debug, Clone, PartialEq, Serialize)]
struct ReleaseList {
releases: Vec<Release>,
crate_name: String,
}

impl_axum_webpage! {
ReleaseList = "rustdoc/releases.html",
cpu_intensive_rendering = true,
}

#[tracing::instrument]
pub(crate) async fn get_all_releases(
Path(params): Path<CrateDetailHandlerParams>,
Extension(pool): Extension<Pool>,
) -> AxumResult<AxumResponse> {
let releases: Vec<Release> = spawn_blocking({
let pool = pool.clone();
let params = params.clone();
move || {
let mut conn = pool.get()?;
let query = "
SELECT
crates.id AS crate_id
FROM crates
WHERE crates.name = $1;";

let rows = conn.query(query, &[&params.name])?;

let result = if rows.is_empty() {
return Ok(Vec::new());
} else {
&rows[0]
};
// get releases, sorted by semver
releases_for_crate(&mut *conn, result.get("crate_id"))
}
})
.await?;

let res = ReleaseList {
releases,
crate_name: params.name,
};
Ok(res.into_response())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 4 additions & 0 deletions src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ pub(super) fn build_axum_routes() -> AxumRouter {
"/crate/:name",
get_internal(super::crate_details::crate_details_handler),
)
.route(
"/:name/releases",
get_internal(super::crate_details::get_all_releases),
)
.route_with_tsr(
"/crate/:name/:version",
get_internal(super::crate_details::crate_details_handler),
Expand Down
8 changes: 2 additions & 6 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2240,12 +2240,8 @@ mod test {
.create()?;

// test rustdoc pages stay on the documentation
let page = kuchiki::parse_html().one(
env.frontend()
.get("/hexponent/0.3.0/hexponent/")
.send()?
.text()?,
);
let page = kuchiki::parse_html()
.one(env.frontend().get("/hexponent/releases").send()?.text()?);
let selector =
r#"ul > li a[href="/crate/hexponent/0.3.1/target-redirect/hexponent/index.html"]"#
.to_string();
Expand Down
29 changes: 29 additions & 0 deletions static/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ const updateMenuPositionForSubMenu = (currentMenuSupplier) => {
subMenu?.style.setProperty('--menu-x', `${currentMenu.getBoundingClientRect().x}px`);
}

function generateReleaseList(data, crateName) {
}

let loadReleases = function() {
const releaseListElem = document.getElementById('releases-list');
// To prevent reloading the list unnecessarily.
loadReleases = function() {};
if (!releaseListElem) {
// We're not in a documentation page, so no need to do anything.
return;
}
const crateName = window.location.pathname.split('/')[1];
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState !== XMLHttpRequest.DONE) {
return;
}
if (xhttp.status === 200) {
releaseListElem.innerHTML = xhttp.responseText;
} else {
console.error(`Failed to load release list: [${xhttp.status}] ${xhttp.responseText}`);
document.getElementById('releases-list').innerHTML = "Failed to load release list";
}
};
xhttp.open("GET", `/${crateName}/releases`, true);
xhttp.send();
};

// Allow menus to be open and used by keyboard.
(function() {
var currentMenu;
Expand Down Expand Up @@ -53,6 +81,7 @@ const updateMenuPositionForSubMenu = (currentMenuSupplier) => {
currentMenu = newMenu;
newMenu.className += " pure-menu-active";
backdrop.style.display = "block";
loadReleases();
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
}
function menuOnClick(e) {
if (this.getAttribute("href") != "#") {
Expand Down
6 changes: 6 additions & 0 deletions templates/rustdoc/releases.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% import "macros.html" as macros %}
{% set target = "" %}
{% set inner_path = releases[0].target_name ~ "/index.html" %}
<ul class="pure-menu-list">
{{ macros::releases_list(name=crate_name, releases=releases, target=target, inner_path=inner_path) }}
</ul>
7 changes: 2 additions & 5 deletions templates/rustdoc/topbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,8 @@
<li class="pure-menu-heading">Versions</li>

<li class="pure-menu-item">
<div class="pure-menu pure-menu-scrollable sub-menu" tabindex="-1">
<ul class="pure-menu-list">
{# Display all releases of this crate #}
{{ macros::releases_list(name=krate.name, releases=krate.releases, target=target, inner_path=inner_path) }}
</ul>
<div class="pure-menu pure-menu-scrollable sub-menu" id="releases-list" tabindex="-1">
<span class="rotate">{{ "spinner" | fas }}</span>
</div>
</li>
</ul>
Expand Down
17 changes: 17 additions & 0 deletions templates/style/_navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ body {
padding: 0;
}

@keyframes rotating_text {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

div.nav-container {
// Nothing is supposed to be over or hovering the top navbar. Maybe add a few others '('? :)
z-index: 999;
Expand Down Expand Up @@ -324,6 +333,14 @@ div.nav-container {
}
}
}

#releases-list {
.rotate {
display: inline-block;
font-size: 30px;
animation: rotating_text 2s linear infinite;
}
}
}

#nav-search {
Expand Down