Skip to content
Open
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
6 changes: 2 additions & 4 deletions packages/perseus/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[cfg(engine)]
use crate::server::HtmlShell;
#[cfg(engine)]
use crate::utils::get_path_prefix_server;
use crate::{
error_views::ErrorViews,
i18n::{Locales, TranslationsManager},
Expand Down Expand Up @@ -824,10 +822,10 @@ impl<G: Html, M: MutableStore, T: TranslationsManager> PerseusAppBase<G, M, T> {
root: &str,
render_cfg: &HashMap<String, String>,
plugins: &Plugins,
path_prefix_server: &str,
) -> Result<HtmlShell, PluginError> {
// Construct an HTML shell
let mut html_shell =
HtmlShell::new(index_view_str, root, render_cfg, &get_path_prefix_server());
let mut html_shell = HtmlShell::new(index_view_str, root, render_cfg, path_prefix_server);

// Apply the myriad plugin actions to the HTML shell (replacing the whole thing
// first if need be)
Expand Down
1 change: 1 addition & 0 deletions packages/perseus/src/turbine/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl<M: MutableStore, T: TranslationsManager> Turbine<M, T> {
&self.root_id,
&self.render_cfg,
&self.plugins,
self.get_path_prefix_server().as_ref(),
)
.await?;
self.html_shell = Some(html_shell);
Expand Down
3 changes: 1 addition & 2 deletions packages/perseus/src/turbine/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
plugins::PluginAction,
state::TemplateState,
stores::MutableStore,
utils::get_path_prefix_server,
};
use fs_extra::dir::{copy as copy_dir, CopyOptions};
use futures::future::{try_join, try_join_all};
Expand Down Expand Up @@ -105,7 +104,7 @@ impl<M: MutableStore, T: TranslationsManager> Turbine<M, T> {
// We assume we've already built the app, which would have populated this
let html_shell = self.html_shell.as_ref().unwrap();

let path_prefix = get_path_prefix_server();
let path_prefix = self.get_path_prefix_server();
// We need the encoded path to reference flattened build artifacts
// But we don't create a flattened system with exporting, everything is properly
// created in a directory structure
Expand Down
22 changes: 22 additions & 0 deletions packages/perseus/src/turbine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub struct Turbine<M: MutableStore, T: TranslationsManager> {
/// The app's global state, kept cached throughout the build process because
/// every template we build will need access to it through context.
global_state: TemplateState,
/// The prefix to place in front of the URL the website will be deployed to.
pub path_prefix_server: Option<String>,
/// The HTML shell that can be used for constructing the full pages this app
/// returns.
html_shell: Option<HtmlShell>,
Expand Down Expand Up @@ -114,6 +116,7 @@ impl<M: MutableStore, T: TranslationsManager> TryFrom<PerseusAppBase<SsrNode, M,
render_cfg: HashMap::new(),
// This will be immediately overriden
global_state: TemplateState::empty(),
path_prefix_server: None,
html_shell: None,
})
}
Expand Down Expand Up @@ -145,10 +148,29 @@ impl<M: MutableStore, T: TranslationsManager> Turbine<M, T> {
&self.root_id,
&self.render_cfg,
&self.plugins,
self.get_path_prefix_server().as_ref(),
)
.await?;
self.html_shell = Some(html_shell);

Ok(())
}

/// Gets the path prefix to apply on the server. If `path_prefix_server` is not set
/// on `Turbine`, `PERSEUS_BASE_PATH` environment variable is used to resolve,
/// which avoids hardcoding something as changeable as this into the final binary.
/// Hence however, that variable must be the same as what's set in `<base>`
/// (done automatically).
/// Trailing forward slashes will be trimmed automatically.
#[cfg(engine)]
pub fn get_path_prefix_server(&self) -> String {
self.path_prefix_server.clone().unwrap_or_else(|| {
use std::env;
let base_path = env::var("PERSEUS_BASE_PATH").unwrap_or_else(|_| "".to_string());
base_path
.strip_suffix('/')
.unwrap_or(&base_path)
.to_string()
})
}
}
3 changes: 1 addition & 2 deletions packages/perseus/src/turbine/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
server::get_path_slice,
state::TemplateState,
stores::MutableStore,
utils::get_path_prefix_server,
Request,
};
use fmterr::fmt_err;
Expand Down Expand Up @@ -309,7 +308,7 @@ impl<M: MutableStore, T: TranslationsManager> Turbine<M, T> {
// bundle will do.
&format!(
"{}/{}/{}",
get_path_prefix_server(),
self.get_path_prefix_server(),
&self.locales.default,
// This is a `PathWithoutLocale`
redirect_path.0,
Expand Down
16 changes: 0 additions & 16 deletions packages/perseus/src/utils/path_prefix.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/// Gets the path prefix to apply on the server. This uses the
/// `PERSEUS_BASE_PATH` environment variable, which avoids hardcoding
/// something as changeable as this into the final binary. Hence however, that
/// variable must be the same as what's set in `<base>` (done automatically).
/// Trailing forward slashes will be trimmed automatically.
#[cfg(engine)]
pub fn get_path_prefix_server() -> String {
use std::env;

let base_path = env::var("PERSEUS_BASE_PATH").unwrap_or_else(|_| "".to_string());
base_path
.strip_suffix('/')
.unwrap_or(&base_path)
.to_string()
}

/// Gets the path prefix to apply in the browser. This uses the HTML `<base>`
/// element, which would be required anyway to make Sycamore's router co-operate
/// with a relative path hosting.
Expand Down