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

Pass locale to build/request state #43

Merged
merged 1 commit into from
Oct 2, 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
2 changes: 1 addition & 1 deletion examples/basic/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.set_headers_fn(Rc::new(set_headers))
}

pub async fn get_build_props(_path: String) -> RenderFnResultWithCause<String> {
pub async fn get_build_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Ok(serde_json::to_string(&IndexPageProps {
greeting: "Hello World!".to_string(),
})?) // This `?` declares the default, that the server is the cause of the error
Expand Down
2 changes: 1 addition & 1 deletion examples/i18n/src/templates/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
}))
}

pub async fn get_static_props(path: String) -> RenderFnResultWithCause<String> {
pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {
// This is just an example
let title = urlencoding::decode(&path).unwrap();
let content = format!(
Expand Down
8 changes: 6 additions & 2 deletions examples/showcase/src/templates/amalgamation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ pub fn amalgamate_states(states: States) -> RenderFnResultWithCause<Option<Strin
})?))
}

pub async fn get_build_state(_path: String) -> RenderFnResultWithCause<String> {
pub async fn get_build_state(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Ok(serde_json::to_string(&AmalagamationPageProps {
message: "Hello from the build process!".to_string(),
})?)
}

pub async fn get_request_state(_path: String, _req: Request) -> RenderFnResultWithCause<String> {
pub async fn get_request_state(
_path: String,
_locale: String,
_req: Request,
) -> RenderFnResultWithCause<String> {
// Err(perseus::GenericErrorWithCause {
// error: "this is a test error!".into(),
// cause: perseus::ErrorCause::Client(None)
Expand Down
2 changes: 1 addition & 1 deletion examples/showcase/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
}))
}

pub async fn get_static_props(_path: String) -> RenderFnResultWithCause<String> {
pub async fn get_static_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Ok(serde_json::to_string(&IndexPageProps {
greeting: "Hello World!".to_string(),
})?)
Expand Down
6 changes: 5 additions & 1 deletion examples/showcase/src/templates/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
}))
}

pub async fn get_request_state(_path: String, req: Request) -> RenderFnResultWithCause<String> {
pub async fn get_request_state(
_path: String,
_locale: String,
req: Request,
) -> RenderFnResultWithCause<String> {
// Err(perseus::GenericErrorWithCause {
// error: "this is a test error!".into(),
// cause: perseus::ErrorCause::Client(None)
Expand Down
2 changes: 1 addition & 1 deletion examples/showcase/src/templates/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
}))
}

pub async fn get_static_props(path: String) -> RenderFnResultWithCause<String> {
pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {
// This path is illegal, and can't be rendered
if path == "post/tests" {
return Err(GenericErrorWithCause {
Expand Down
2 changes: 1 addition & 1 deletion examples/showcase/src/templates/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.build_paths_fn(Rc::new(get_build_paths))
}

pub async fn get_build_state(path: String) -> RenderFnResultWithCause<String> {
pub async fn get_build_state(path: String, _locale: String) -> RenderFnResultWithCause<String> {
// This path is illegal, and can't be rendered
if path == "timeisr/tests" {
return Err(GenericErrorWithCause {
Expand Down
2 changes: 1 addition & 1 deletion examples/showcase/src/templates/time_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.build_state_fn(Rc::new(get_build_state))
}

pub async fn get_build_state(_path: String) -> RenderFnResultWithCause<String> {
pub async fn get_build_state(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Ok(serde_json::to_string(&TimePageProps {
time: format!("{:?}", std::time::SystemTime::now()),
})?)
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub async fn build_template(
if template.uses_build_state() && template.revalidates() {
// We pass in the path to get a state (including the template path for consistency with the incremental logic)
let initial_state = template
.get_build_state(full_path_without_locale.clone())
.get_build_state(full_path_without_locale.clone(), translator.get_locale())
.await?;
// Write that intial state to a static JSON file
mutable_store
Expand All @@ -112,7 +112,7 @@ pub async fn build_template(
} else if template.uses_build_state() {
// We pass in the path to get a state (including the template path for consistency with the incremental logic)
let initial_state = template
.get_build_state(full_path_without_locale.clone())
.get_build_state(full_path_without_locale.clone(), translator.get_locale())
.await?;
// Write that intial state to a static JSON file
immutable_store
Expand Down
17 changes: 14 additions & 3 deletions packages/perseus/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ async fn render_request_state(
req: Request,
) -> Result<(String, String, Option<String>), ServerError> {
// Generate the initial state (this may generate an error, but there's no file that can't exist)
let state = Some(template.get_request_state(path.to_string(), req).await?);
let state = Some(
template
.get_request_state(path.to_string(), translator.get_locale(), req)
.await?,
);
// Use that to render the static HTML
let html = sycamore::render_to_string(|| {
template.render_for_template(state.clone(), Rc::clone(&translator), true)
Expand Down Expand Up @@ -177,7 +181,10 @@ async fn revalidate(
// We need to regenerate and cache this page for future usage (until the next revalidation)
let state = Some(
template
.get_build_state(format!("{}/{}", template.get_path(), path))
.get_build_state(
format!("{}/{}", template.get_path(), path),
translator.get_locale(),
)
.await?,
);
let html = sycamore::render_to_string(|| {
Expand Down Expand Up @@ -297,7 +304,11 @@ pub async fn get_page_for_template(
// All this uses the mutable store because this will be done at runtime
None => {
// We need to generate and cache this page for future usage
let state = Some(template.get_build_state(path.to_string()).await?);
let state = Some(
template
.get_build_state(path.to_string(), locale.to_string())
.await?,
);
let html_val = sycamore::render_to_string(|| {
template.render_for_template(state.clone(), Rc::clone(&translator), true)
});
Expand Down
20 changes: 14 additions & 6 deletions packages/perseus/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ make_async_trait!(GetBuildPathsFnType, RenderFnResult<Vec<String>>);
make_async_trait!(
GetBuildStateFnType,
RenderFnResultWithCause<String>,
path: String
path: String,
locale: String
);
make_async_trait!(
GetRequestStateFnType,
RenderFnResultWithCause<String>,
path: String,
locale: String,
req: Request
);
make_async_trait!(ShouldRevalidateFnType, RenderFnResultWithCause<bool>);
Expand Down Expand Up @@ -279,10 +281,15 @@ impl<G: GenericNode> Template<G> {
}
}
/// Gets the initial state for a template. This needs to be passed the full path of the template, which may be one of those generated by
/// `.get_build_paths()`.
pub async fn get_build_state(&self, path: String) -> Result<String, ServerError> {
/// `.get_build_paths()`. This also needs the locale being rendered to so that more compelx applications like custom documentation
/// systems can be enabled.
pub async fn get_build_state(
&self,
path: String,
locale: String,
) -> Result<String, ServerError> {
if let Some(get_build_state) = &self.get_build_state {
let res = get_build_state.call(path).await;
let res = get_build_state.call(path, locale).await;
match res {
Ok(res) => Ok(res),
Err(GenericErrorWithCause { error, cause }) => Err(ServerError::RenderFnFailed {
Expand All @@ -302,14 +309,15 @@ impl<G: GenericNode> Template<G> {
}
/// Gets the request-time state for a template. This is equivalent to SSR, and will not be performed at build-time. Unlike
/// `.get_build_paths()` though, this will be passed information about the request that triggered the render. Errors here can be caused
/// by either the server or the client, so the user must specify an [`ErrorCause`].
/// by either the server or the client, so the user must specify an [`ErrorCause`]. This is also passed the locale being rendered to.
pub async fn get_request_state(
&self,
path: String,
locale: String,
req: Request,
) -> Result<String, ServerError> {
if let Some(get_request_state) = &self.get_request_state {
let res = get_request_state.call(path, req).await;
let res = get_request_state.call(path, locale, req).await;
match res {
Ok(res) => Ok(res),
Err(GenericErrorWithCause { error, cause }) => Err(ServerError::RenderFnFailed {
Expand Down