Skip to content

Commit 1af26dc

Browse files
committed
refactor: ♻️ removed useless render options system
1 parent ac79996 commit 1af26dc

File tree

5 files changed

+30
-101
lines changed

5 files changed

+30
-101
lines changed

examples/showcase/server/src/main.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use actix_web::{web, App, HttpRequest, HttpServer, Result as ActixResult, error};
22
use actix_files::{NamedFile};
33
use sycamore::prelude::SsrNode;
4+
use std::collections::HashMap;
45

56
use perseus::{
67
serve::{get_render_cfg, get_page},
7-
render_cfg::RenderCfg,
88
config_manager::FsConfigManager,
99
template::TemplateMap
1010
};
@@ -48,7 +48,12 @@ async fn js_bundle() -> std::io::Result<NamedFile> {
4848
async fn wasm_bundle() -> std::io::Result<NamedFile> {
4949
NamedFile::open("../app/pkg/perseus_showcase_app_bg.wasm")
5050
}
51-
async fn page_data(req: HttpRequest, templates: web::Data<TemplateMap<SsrNode>>, render_cfg: web::Data<RenderCfg>, config_manager: web::Data<FsConfigManager>) -> ActixResult<String> {
51+
async fn page_data(
52+
req: HttpRequest,
53+
templates: web::Data<TemplateMap<SsrNode>>,
54+
render_cfg: web::Data<HashMap<String, String>>,
55+
config_manager: web::Data<FsConfigManager>
56+
) -> ActixResult<String> {
5257
let path = req.match_info().query("filename");
5358
// TODO match different types of errors here
5459
let page_data = get_page(path, &render_cfg, &templates, config_manager.get_ref()).map_err(error::ErrorNotFound)?;

src/build.rs

+11-41
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use crate::{
44
template::Template,
55
config_manager::ConfigManager,
6-
render_cfg::{RenderOpt, RenderCfg, TemplatesCfg, PagesCfg}
76
};
87
use crate::errors::*;
98
use std::collections::HashMap;
@@ -18,56 +17,36 @@ pub fn build_template(
1817
config_manager: &impl ConfigManager
1918
) -> Result<
2019
(
21-
Vec<RenderOpt>,
2220
Vec<String>,
2321
bool
2422
)
2523
> {
26-
let mut render_opts: Vec<RenderOpt> = Vec::new();
2724
let mut single_page = false;
2825
let template_path = template.get_path();
2926

30-
// Handle the boolean properties
31-
if template.revalidates() {
32-
render_opts.push(RenderOpt::Revalidated);
33-
}
34-
if template.uses_incremental() {
35-
render_opts.push(RenderOpt::Incremental);
36-
}
37-
3827
// Handle static path generation
3928
// Because we iterate over the paths, we need a base path if we're not generating custom ones (that'll be overriden if needed)
4029
let paths = match template.uses_build_paths() {
41-
true => {
42-
render_opts.push(RenderOpt::StaticPaths);
43-
template.get_build_paths()?
44-
},
30+
true => template.get_build_paths()?,
4531
false => {
4632
single_page = true;
4733
vec![String::new()]
4834
}
4935
};
50-
// Add the rest of the render options before we loop over defined pages
51-
if template.uses_build_state() {
52-
render_opts.push(RenderOpt::StaticProps);
53-
}
54-
if template.uses_request_state() {
55-
render_opts.push(RenderOpt::Server);
56-
}
5736

5837
// Iterate through the paths to generate initial states if needed
5938
for path in paths.iter() {
6039
// If needed, we'll contruct a full path that's URL encoded so we can easily save it as a file
6140
// BUG: insanely nested paths won't work whatsoever if the filename is too long, maybe hash instead?
62-
let full_path = match render_opts.contains(&RenderOpt::StaticPaths) {
41+
let full_path = match template.uses_build_paths() {
6342
true => urlencoding::encode(&format!("{}/{}", &template_path, path)).to_string(),
6443
// We don't want to concatenate the name twice if we don't have to
6544
false => template_path.clone()
6645
};
6746

6847
// Handle static initial state generation
6948
// We'll only write a static state if one is explicitly generated
70-
if render_opts.contains(&RenderOpt::StaticProps) {
49+
if template.uses_build_state() {
7150
// We pass in the latter part of the path, without the base specifier (because that would be the same for everything in the template)
7251
let initial_state = template.get_build_state(path.to_string())?;
7352
// Write that intial state to a static JSON file
@@ -89,7 +68,6 @@ pub fn build_template(
8968
// If the template is very basic, prerender without any state
9069
// It's safe to add a property to the render options here because `.is_basic()` will only return true if path generation is not being used (or anything else)
9170
if template.is_basic() {
92-
render_opts.push(RenderOpt::StaticProps);
9371
let prerendered = sycamore::render_to_string(
9472
||
9573
template.render_for_template(None)
@@ -100,55 +78,47 @@ pub fn build_template(
10078
}
10179
}
10280

103-
Ok((render_opts, paths, single_page))
81+
Ok((paths, single_page))
10482
}
10583

10684
// TODO function to build pages
10785
/// Runs the build process of building many different templates.
10886
pub fn build_templates(templates: Vec<Template<SsrNode>>, config_manager: &impl ConfigManager) -> Result<()> {
109-
let mut templates_conf: TemplatesCfg = HashMap::new();
110-
let mut pages_conf: PagesCfg = HashMap::new();
87+
// The render configuration stores a list of pages to the root paths of their templates
88+
let mut render_cfg = HashMap::new();
11189
// Create each of the templates
11290
for template in templates {
11391
let template_root_path = template.get_path();
11492
let is_incremental = template.uses_incremental();
11593

116-
let (render_opts, pages, single_page) = build_template(template, config_manager)?;
117-
templates_conf.insert(
118-
template_root_path.clone(),
119-
render_opts
120-
);
94+
let (pages, single_page) = build_template(template, config_manager)?;
12195
// If the tempalte represents a single page itself, we don't need any concatenation
12296
if single_page {
123-
pages_conf.insert(
97+
render_cfg.insert(
12498
template_root_path.clone(),
12599
template_root_path.clone()
126100
);
127101
} else {
128102
// Add each page that the template explicitly generated (ignoring ISR for now)
129103
for page in pages {
130-
pages_conf.insert(
104+
render_cfg.insert(
131105
format!("{}/{}", &template_root_path, &page),
132106
template_root_path.clone()
133107
);
134108
}
135109
// Now if the page uses ISR, add an explicit `/*` in there after the template root path
136110
// Incremental rendering requires build-time path generation
137111
if is_incremental {
138-
pages_conf.insert(
112+
render_cfg.insert(
139113
format!("{}/*", &template_root_path),
140114
template_root_path.clone()
141115
);
142116
}
143117
}
144118
}
145119

146-
let render_conf = RenderCfg {
147-
templates: templates_conf,
148-
pages: pages_conf
149-
};
150120
config_manager
151-
.write("./dist/render_conf.json", &serde_json::to_string(&render_conf)?)?;
121+
.write("./dist/render_conf.json", &serde_json::to_string(&render_cfg)?)?;
152122

153123
Ok(())
154124
}

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pub mod errors;
22
pub mod shell;
33
pub mod serve;
4-
pub mod render_cfg;
54
pub mod config_manager;
65
pub mod template;
76
pub mod build;

src/render_cfg.rs

-38
This file was deleted.

src/serve.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
use std::fs;
44
use serde::{Serialize, Deserialize};
55
use crate::errors::*;
6-
use crate::render_cfg::{RenderCfg, RenderOpt};
76
use crate::config_manager::ConfigManager;
87
use crate::template::TemplateMap;
98
use sycamore::prelude::SsrNode;
9+
use std::collections::HashMap;
1010

1111
/// Represents the data necessary to render a page.
1212
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -19,9 +19,9 @@ pub struct PageData {
1919
}
2020

2121
/// Gets the configuration of how to render each page.
22-
pub fn get_render_cfg() -> Result<RenderCfg> {
22+
pub fn get_render_cfg() -> Result<HashMap<String, String>> {
2323
let content = fs::read_to_string("../app/dist/render_conf.json")?;
24-
let cfg = serde_json::from_str::<RenderCfg>(&content)?;
24+
let cfg = serde_json::from_str::<HashMap<String, String>>(&content)?;
2525

2626
Ok(cfg)
2727
}
@@ -30,7 +30,7 @@ pub fn get_render_cfg() -> Result<RenderCfg> {
3030
// TODO let this function take a request struct of some form
3131
pub fn get_page(
3232
path: &str,
33-
render_cfg: &RenderCfg,
33+
render_cfg: &HashMap<String, String>,
3434
templates: &TemplateMap<SsrNode>,
3535
config_manager: &impl ConfigManager
3636
) -> Result<PageData> {
@@ -40,7 +40,7 @@ pub fn get_page(
4040
// Match the path to one of the templates
4141
let mut template_name = String::new();
4242
// We'll try a direct match first
43-
if let Some(template_root_path) = render_cfg.pages.get(path) {
43+
if let Some(template_root_path) = render_cfg.get(path) {
4444
template_name = template_root_path.to_string();
4545
}
4646
// Next, an ISR match (more complex)
@@ -56,7 +56,7 @@ pub fn get_page(
5656
} + "/*";
5757

5858
// If we find something, keep going until we don't (maximise specificity)
59-
if let Some(template_root_path) = render_cfg.pages.get(&path_to_try) {
59+
if let Some(template_root_path) = render_cfg.get(&path_to_try) {
6060
template_name = template_root_path.to_string();
6161
} else {
6262
break;
@@ -66,33 +66,26 @@ pub fn get_page(
6666
bail!(ErrorKind::PageNotFound(path.to_string()))
6767
}
6868

69-
// Get the render options of the template
70-
let render_opts = render_cfg.templates.get(&template_name);
71-
let render_opts = match render_opts {
72-
Some(render_opts) => render_opts,
69+
// Get the template to use
70+
let template = templates.get(&template_name);
71+
let template = match template {
72+
Some(template) => template,
7373
None => bail!(ErrorKind::PageNotFound(path.to_string()))
7474
};
7575

7676
let html: String;
7777
let state: Option<String>;
7878

79-
// TODO remove render options system altogether now that we're passing pages around
8079
// Handle each different type of rendering (static paths have already been done though, so we don't need to deal with them)
81-
if render_opts.contains(&RenderOpt::StaticProps) {
80+
if template.uses_build_state() || template.is_basic() {
8281
// Get the static HTML
8382
html = config_manager.read(&format!("../app/dist/static/{}.html", path_encoded))?;
8483
// Get the static JSON
8584
state = match config_manager.read(&format!("../app/dist/static/{}.json", path_encoded)) {
8685
Ok(state) => Some(state),
8786
Err(_) => None
8887
};
89-
} else if render_opts.contains(&RenderOpt::Server) {
90-
// Get the template itself (we need it for generation)
91-
let template = templates.get(&template_name);
92-
let template = match template {
93-
Some(template) => template,
94-
None => bail!(ErrorKind::PageNotFound(path.to_string()))
95-
};
88+
} else if template.uses_request_state() {
9689
// Generate the initial state (this may generate an error, but there's no file that can't exist)
9790
state = Some(template.get_request_state(path.to_string())?);
9891
// Use that to render the static HTML

0 commit comments

Comments
 (0)