Skip to content

Commit 7c9e433

Browse files
committed
refactor: 🚚 renamed pages to templates for clarity
We create templates that in turn render pages, avoids naming confusion
1 parent b2e9a68 commit 7c9e433

File tree

10 files changed

+84
-83
lines changed

10 files changed

+84
-83
lines changed

examples/showcase/app/src/bin/build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use perseus::{
22
config_manager::{FsConfigManager, ConfigManager},
3-
build_pages
3+
build_templates
44
};
55
use perseus_showcase_app::pages;
66
use sycamore::prelude::SsrNode;
77

88
fn main() {
99
let config_manager = FsConfigManager::new();
1010

11-
build_pages!([
11+
build_templates!([
1212
pages::index::get_page::<SsrNode>(),
1313
pages::about::get_page::<SsrNode>(),
1414
pages::post::get_page::<SsrNode>()

examples/showcase/app/src/pages/about.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use sycamore::prelude::*;
2-
use perseus::page::Page;
1+
use sycamore::prelude::{template, component, GenericNode, Template as SycamoreTemplate};
2+
use perseus::template::Template;
33

44
#[component(AboutPage<G>)]
5-
pub fn about_page() -> Template<G> {
5+
pub fn about_page() -> SycamoreTemplate<G> {
66
template! {
77
p { "About." }
88
}
99
}
1010

11-
pub fn get_page<G: GenericNode>() -> Page<(), G> {
12-
Page::new("about")
11+
pub fn get_page<G: GenericNode>() -> Template<(), G> {
12+
Template::new("about")
1313
.template(Box::new(|_| template! {
1414
AboutPage()
1515
}

examples/showcase/app/src/pages/index.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
use sycamore::prelude::*;
21
use serde::{Serialize, Deserialize};
3-
use perseus::page::Page;
2+
use sycamore::prelude::{template, component, GenericNode, Template as SycamoreTemplate};
3+
use perseus::template::Template;
44

55
#[derive(Serialize, Deserialize, Debug)]
66
pub struct IndexPageProps {
77
pub greeting: String
88
}
99

1010
#[component(IndexPage<G>)]
11-
pub fn index_page(props: IndexPageProps) -> Template<G> {
11+
pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {
1212
template! {
1313
p {(props.greeting)}
1414
a(href = "/about") { "About!" }
1515
}
1616
}
1717

18-
pub fn get_page<G: GenericNode>() -> Page<IndexPageProps, G> {
19-
Page::new("index")
18+
pub fn get_page<G: GenericNode>() -> Template<IndexPageProps, G> {
19+
Template::new("index")
2020
.build_state_fn(Box::new(get_static_props))
2121
.template(Box::new(|props: Option<IndexPageProps>| template! {
2222
IndexPage(props.unwrap())

examples/showcase/app/src/pages/post.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use sycamore::prelude::*;
21
use serde::{Serialize, Deserialize};
3-
use perseus::page::Page;
2+
use sycamore::prelude::{template, component, GenericNode, Template as SycamoreTemplate};
3+
use perseus::template::Template;
44

55
#[derive(Serialize, Deserialize)]
66
pub struct PostPageProps {
@@ -9,7 +9,7 @@ pub struct PostPageProps {
99
}
1010

1111
#[component(PostPage<G>)]
12-
pub fn post_page(props: PostPageProps) -> Template<G> {
12+
pub fn post_page(props: PostPageProps) -> SycamoreTemplate<G> {
1313
let title = props.title;
1414
let content = props.content;
1515
template! {
@@ -22,8 +22,8 @@ pub fn post_page(props: PostPageProps) -> Template<G> {
2222
}
2323
}
2424

25-
pub fn get_page<G: GenericNode>() -> Page<PostPageProps, G> {
26-
Page::new("post")
25+
pub fn get_page<G: GenericNode>() -> Template<PostPageProps, G> {
26+
Template::new("post")
2727
.build_paths_fn(Box::new(get_static_paths))
2828
.build_state_fn(Box::new(get_static_props))
2929
.incremental_path_rendering(true)

src/build.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
// This binary builds all the pages with SSG
1+
// This binary builds all the templates with SSG
22

33
use serde::{Serialize, de::DeserializeOwned};
44
use crate::{
5-
page::Page,
5+
template::Template,
66
config_manager::ConfigManager,
77
render_cfg::RenderOpt
88
};
99
use crate::errors::*;
1010
use std::any::Any;
1111
use sycamore::prelude::SsrNode;
1212

13-
/// Builds a page, writing static data as appropriate. This should be used as part of a larger build process.
14-
pub fn build_page<Props: Serialize + DeserializeOwned + Any>(page: Page<Props, SsrNode>, config_manager: &impl ConfigManager) -> Result<Vec<RenderOpt>> {
13+
/// Builds a template, writing static data as appropriate. This should be used as part of a larger build process.
14+
pub fn build_template<Props: Serialize + DeserializeOwned + Any>(template: Template<Props, SsrNode>, config_manager: &impl ConfigManager) -> Result<Vec<RenderOpt>> {
1515
let mut render_opts: Vec<RenderOpt> = Vec::new();
16-
let page_path = page.get_path();
16+
let template_path = template.get_path();
1717

1818
// Handle the boolean properties
19-
if page.revalidates() {
19+
if template.revalidates() {
2020
render_opts.push(RenderOpt::Revalidated);
2121
}
22-
if page.uses_incremental() {
22+
if template.uses_incremental() {
2323
render_opts.push(RenderOpt::Incremental);
2424
}
2525

2626
// Handle static path generation
2727
// Because we iterate over the paths, we need a base path if we're not generating custom ones (that'll be overriden if needed)
28-
let paths = match page.uses_build_paths() {
28+
let paths = match template.uses_build_paths() {
2929
true => {
3030
render_opts.push(RenderOpt::StaticPaths);
31-
page.get_build_paths()?
31+
template.get_build_paths()?
3232
},
33-
false => vec![page_path.clone()]
33+
false => vec![template_path.clone()]
3434
};
3535

3636
// Iterate through the paths to generate initial states if needed
3737
for path in paths.iter() {
3838
// If needed, we'll contruct a full path that's URL encoded so we can easily save it as a file
3939
// BUG: insanely nested paths won't work whatsoever if the filename is too long, maybe hash instead?
4040
let full_path = match render_opts.contains(&RenderOpt::StaticPaths) {
41-
true => urlencoding::encode(&format!("{}/{}", &page_path, path)).to_string(),
41+
true => urlencoding::encode(&format!("{}/{}", &template_path, path)).to_string(),
4242
// We don't want to concatenate the name twice if we don't have to
43-
false => page_path.clone()
43+
false => template_path.clone()
4444
};
4545

4646
// Handle static initial state generation
4747
// We'll only write a static state if one is explicitly generated
48-
if page.uses_build_state() {
48+
if template.uses_build_state() {
4949
render_opts.push(RenderOpt::StaticProps);
5050
// We pass in the latter part of the path, without the base specifier (because that would be the same for everything in the template)
51-
let initial_state = page.get_build_state(path.to_string())?;
51+
let initial_state = template.get_build_state(path.to_string())?;
5252
let initial_state_str = serde_json::to_string(&initial_state).unwrap();
5353
// Write that intial state to a static JSON file
5454
config_manager
5555
.write(&format!("./dist/static/{}.json", full_path), &initial_state_str)
5656
.unwrap();
57-
// Prerender the page using that state
57+
// Prerender the template using that state
5858
let prerendered = sycamore::render_to_string(
5959
||
60-
page.render_for_template(Some(initial_state))
60+
template.render_for_template(Some(initial_state))
6161
);
6262
// Write that prerendered HTML to a static file
6363
config_manager
@@ -67,17 +67,17 @@ pub fn build_page<Props: Serialize + DeserializeOwned + Any>(page: Page<Props, S
6767

6868
// Handle server-side rendering
6969
// By definition, everything here is done at request-time, so there's not really much to do
70-
// Note also that if a page only uses SSR, it won't get prerendered at build time whatsoever
71-
if page.uses_request_state() {
70+
// Note also that if a template only uses SSR, it won't get prerendered at build time whatsoever
71+
if template.uses_request_state() {
7272
render_opts.push(RenderOpt::Server);
7373
}
7474

75-
// If the page is very basic, prerender without any state
76-
if page.is_basic() {
75+
// If the template is very basic, prerender without any state
76+
if template.is_basic() {
7777
render_opts.push(RenderOpt::StaticProps);
7878
let prerendered = sycamore::render_to_string(
7979
||
80-
page.render_for_template(None)
80+
template.render_for_template(None)
8181
);
8282
// Write that prerendered HTML to a static file
8383
config_manager
@@ -89,20 +89,20 @@ pub fn build_page<Props: Serialize + DeserializeOwned + Any>(page: Page<Props, S
8989
Ok(render_opts)
9090
}
9191

92-
/// Runs the build process of building many different pages. This is done with a macro because typing for a function means we have to do
92+
/// Runs the build process of building many different templates. This is done with a macro because typing for a function means we have to do
9393
/// things on the heap.
9494
/// (Any better solutions are welcome in PRs!)
9595
#[macro_export]
96-
macro_rules! build_pages {
96+
macro_rules! build_templates {
9797
(
98-
[$($page:expr),+],
98+
[$($template:expr),+],
9999
$config_manager:expr
100100
) => {
101101
let mut render_conf: $crate::render_cfg::RenderCfg = ::std::collections::HashMap::new();
102102
$(
103103
render_conf.insert(
104-
$page.get_path(),
105-
$crate::build::build_page($page, $config_manager)
104+
$template.get_path(),
105+
$crate::build::build_template($template, $config_manager)
106106
.unwrap()
107107
);
108108
)+

src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ error_chain! {
1313
display("the following error occurred while interfacing with javascript: {:?}", err)
1414
}
1515

16-
PageFeatureNotEnabled(name: String, feature: String) {
17-
description("a page feature required by a function called was not present")
18-
display("the page '{}' is missing the feature '{}'", name, feature)
16+
TemplateFeatureNotEnabled(name: String, feature: String) {
17+
description("a template feature required by a function called was not present")
18+
display("the template '{}' is missing the feature '{}'", name, feature)
1919
}
2020
}
2121
links {

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ pub mod shell;
33
pub mod serve;
44
pub mod render_cfg;
55
pub mod config_manager;
6-
pub mod page;
6+
pub mod template;
77
pub mod build;

src/serve.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn get_render_cfg() -> Result<RenderCfg> {
2828
pub fn get_page(raw_path: &str, render_cfg: &RenderCfg, config_manager: &impl ConfigManager) -> Result<PageData> {
2929
// Remove `/` from the path by encoding it as a URL (that's what we store)
3030
let path = urlencoding::encode(raw_path).to_string();
31+
// TODO Match the path to one of the templates
3132
// TODO support SSR
3233

3334
// Get the static HTML

src/shell.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use sycamore::prelude::*;
66
use serde::{Serialize, de::DeserializeOwned};
77
use crate::errors::*;
88
use crate::serve::PageData;
9-
use crate::page::TemplateFn;
9+
use crate::template::TemplateFn;
1010

1111
pub async fn fetch(url: String) -> Result<Option<String>> {
1212
let js_err_handler = |err: JsValue| ErrorKind::JsErr(format!("{:?}", err));

0 commit comments

Comments
 (0)