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

Fix build paths #41

Merged
merged 3 commits into from
Oct 1, 2021
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
6 changes: 4 additions & 2 deletions docs/0.3.x/src/strategies/build-paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ Note that, like _build state_, this strategy may be invoked at build-time or whi

## Usage

Here's the same example as given in the previous section (taken from [here](https://github.com/arctic-hen7/perseus/blob/main/examples/showcase/src/templates/post.rs)), which uses _build paths_ together with _build state_ and _incremental generation_:
Here's the same example as given in the previous section (taken from [here](https://github.com/arctic-hen7/perseus/blob/main/examples/showcase/i18n/templates/post.rs)), which uses _build paths_ together with _build state_:

```rust,no_run,no_playground
{{#include ../../../../examples/showcase/src/templates/post.rs}}
{{#include ../../../../examples/i18n/src/templates/post.rs}}
```

Note the return type of the `get_build_paths` function, which returns a `RenderFnResult<Vec<String>>`, which is just an alias for `Result<T, Box<dyn std::error::Error>>`, which means that you can return any error you want. If you need to explicitly `return Err(..)`, then you should use `.into()` to perform the conversion from your error type to this type automatically. Perseus will then format your errors nicely for you using [`fmterr`](https://github.com/arctic-hen7/fmterr).

Also note how this page renders the page `/docs` by specifying an empty string as one of the paths exported from `get_build_paths`.
6 changes: 4 additions & 2 deletions docs/next/src/strategies/build-paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ Note that, like _build state_, this strategy may be invoked at build-time or whi

## Usage

Here's the same example as given in the previous section (taken from [here](https://github.com/arctic-hen7/perseus/blob/main/examples/showcase/src/templates/post.rs)), which uses _build paths_ together with _build state_ and _incremental generation_:
Here's the same example as given in the previous section (taken from [here](https://github.com/arctic-hen7/perseus/blob/main/examples/showcase/i18n/templates/post.rs)), which uses _build paths_ together with _build state_:

```rust,no_run,no_playground
{{#include ../../../../examples/showcase/src/templates/post.rs}}
{{#include ../../../../examples/i18n/src/templates/post.rs}}
```

Note the return type of the `get_build_paths` function, which returns a `RenderFnResult<Vec<String>>`, which is just an alias for `Result<T, Box<dyn std::error::Error>>`, which means that you can return any error you want. If you need to explicitly `return Err(..)`, then you should use `.into()` to perform the conversion from your error type to this type automatically. Perseus will then format your errors nicely for you using [`fmterr`](https://github.com/arctic-hen7/fmterr).

Also note how this page renders the page `/docs` by specifying an empty string as one of the paths exported from `get_build_paths`.
2 changes: 1 addition & 1 deletion examples/basic/.perseus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn run() -> Result<(), JsValue> {
// Right now, we don't provide translators to any error pages that have come from the server
error_pages.hydrate_page(&url, &status, &err, None, &container_rx_elem);
} else {
get_error_pages::<DomNode>().get_template_for_page("", &404, "not found", None);
error_pages.hydrate_page("", &404, "not found", None, &container_rx_elem);
}
},
};
Expand Down
9 changes: 7 additions & 2 deletions examples/i18n/src/templates/post.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use perseus::{RenderFnResult, RenderFnResultWithCause, Template};
use perseus::{link, RenderFnResult, RenderFnResultWithCause, Template};
use serde::{Deserialize, Serialize};
use std::rc::Rc;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
Expand All @@ -20,6 +20,7 @@ pub fn post_page(props: PostPageProps) -> SycamoreTemplate<G> {
p {
(content)
}
a(href = link!("/post")) { "Root post page" }
}
}

Expand Down Expand Up @@ -51,5 +52,9 @@ pub async fn get_static_props(path: String) -> RenderFnResultWithCause<String> {
}

pub async fn get_static_paths() -> RenderFnResult<Vec<String>> {
Ok(vec!["test".to_string(), "blah/test/blah".to_string()])
Ok(vec![
"".to_string(),
"test".to_string(),
"blah/test/blah".to_string(),
])
}
28 changes: 23 additions & 5 deletions packages/perseus/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ pub async fn build_template(
// Handle static path generation
// Because we iterate over the paths, we need a base path if we're not generating custom ones (that'll be overriden if needed)
let paths = match template.uses_build_paths() {
true => template.get_build_paths().await?,
true => template
.get_build_paths()
.await?
// Trim away any trailing `/`s so we don't insert them into the render config
// That makes rendering an index page from build paths impossible (see #39)
.iter()
.map(|p| match p.strip_suffix('/') {
Some(stripped) => stripped.to_string(),
None => p.to_string(),
})
.collect(),
false => {
single_page = true;
vec![String::new()]
Expand All @@ -61,6 +71,11 @@ pub async fn build_template(
// We don't want to concatenate the name twice if we don't have to
false => urlencoding::encode(&template_path).to_string(),
};
// Strip trailing `/`s (but they're encoded) for the reasons described above
let full_path_without_locale = match full_path_without_locale.strip_suffix("%2F") {
Some(stripped) => stripped.to_string(),
None => full_path_without_locale,
};
// Add the current locale to the front of that
let full_path = format!("{}-{}", translator.get_locale(), full_path_without_locale);

Expand Down Expand Up @@ -185,10 +200,13 @@ async fn build_template_and_get_cfg(
} else {
// Add each page that the template explicitly generated (ignoring ISR for now)
for page in pages {
render_cfg.insert(
format!("{}/{}", &template_root_path, &page),
template_root_path.clone(),
);
let path = format!("{}/{}", &template_root_path, &page);
// Remove any trailing `/`s for the reasons described above
let path = match path.strip_suffix('/') {
Some(stripped) => stripped.to_string(),
None => path,
};
render_cfg.insert(path, template_root_path.clone());
}
// Now if the page uses ISR, add an explicit `/*` in there after the template root path
// Incremental rendering requires build-time path generation
Expand Down