Skip to content

Respect base_path when pre-rendering static routes #3958

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ url = "2.5.4"
separator = "0.4.1"
pretty_assertions = "1.4.1"
serde_repr = "0.1"
hyper-util = "0.1.15"
hyper-util = "0.1"
krates = { version = "0.17.5" }
libloading = "0.8.8"
libc = "0.2.174"
Expand Down
69 changes: 39 additions & 30 deletions packages/cli/src/build/pre_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ pub(crate) async fn pre_render_static_routes(
let address = &address;
let port = &port;

tracing::info!("Running SSG at http://{address}:{port} for {server_exe:?}");
let url = if let Some(base_path) = builder.build.base_path() {
format!("http://{address}:{port}/{base_path}")
} else {
format!("http://{address}:{port}")
};

tracing::info!("Running SSG at {url} for {server_exe:?}");

let vars = builder.child_environment_variables(
devserver_ip,
Expand Down Expand Up @@ -71,7 +77,7 @@ pub(crate) async fn pre_render_static_routes(
);

let request = reqwest_client
.post(format!("http://{address}:{port}/api/static_routes"))
.post(format!("{url}/api/static_routes"))
.body("{}".to_string())
.send()
.await;
Expand Down Expand Up @@ -102,34 +108,37 @@ pub(crate) async fn pre_render_static_routes(
// Create a pool of futures that cache each route
let mut resolved_routes = routes
.into_iter()
.map(|route| async move {
tracing::info!("Rendering {route} for SSG");

// For each route, ping the server to force it to cache the response for ssg
let request = reqwest_client
.get(format!("http://{address}:{port}{route}"))
.header("Accept", "text/html")
.send()
.await?;

// If it takes longer than 30 seconds to resolve the route, log a warning
let warning_task = tokio::spawn({
let route = route.clone();
async move {
tokio::time::sleep(Duration::from_secs(30)).await;
tracing::warn!("Route {route} has been rendering for 30 seconds");
}
});

// Wait for the streaming response to completely finish before continuing. We don't use the html it returns directly
// because it may contain artifacts of intermediate streaming steps while the page is loading. The SSG app should write
// the final clean HTML to the disk automatically after the request completes.
let _html = request.text().await?;

// Cancel the warning task if it hasn't already run
warning_task.abort();

Ok::<_, reqwest::Error>(route)
.map(|route| {
let url = url.clone();
async move {
tracing::info!("Rendering {route} for SSG");

// For each route, ping the server to force it to cache the response for ssg
let request = reqwest_client
.get(format!("{url}{route}"))
.header("Accept", "text/html")
.send()
.await?;

// If it takes longer than 30 seconds to resolve the route, log a warning
let warning_task = tokio::spawn({
let route = route.clone();
async move {
tokio::time::sleep(Duration::from_secs(30)).await;
tracing::warn!("Route {route} has been rendering for 30 seconds");
}
});

// Wait for the streaming response to completely finish before continuing. We don't use the html it returns directly
// because it may contain artifacts of intermediate streaming steps while the page is loading. The SSG app should write
// the final clean HTML to the disk automatically after the request completes.
let _html = request.text().await?;

// Cancel the warning task if it hasn't already run
warning_task.abort();

Ok::<_, reqwest::Error>(route)
}
})
.collect::<FuturesUnordered<_>>();

Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ impl RunArgs {
.inspect_err(|e| tracing::error!("Failed to open app: {}", e));

if platform == Platform::Web {
let base_path = match builder.client.build.base_path() {
Some(base_path) => format!("/{}", base_path.trim_matches('/')),
None => "".to_owned(),
};
tracing::info!(
"Serving app at http://{}:{}",
"Serving app at http://{}:{}{base_path}",
builder.devserver_bind_ip,
builder.devserver_port
);
Expand Down