|
| 1 | +// This binary builds all the pages with SSG |
| 2 | + |
| 3 | +use serde::{Serialize, de::DeserializeOwned}; |
| 4 | +use crate::{ |
| 5 | + page::Page, |
| 6 | + config_manager::ConfigManager, |
| 7 | + render_cfg::RenderOpt |
| 8 | +}; |
| 9 | +use crate::errors::*; |
| 10 | +use std::any::Any; |
| 11 | + |
| 12 | +/// Builds a page, writing static data as appropriate. This should be used as part of a larger build process. |
| 13 | +pub fn build_page<Props: Serialize + DeserializeOwned + Any>(page: Page<Props>, config_manager: &impl ConfigManager) -> Result<Vec<RenderOpt>> { |
| 14 | + let mut render_opts: Vec<RenderOpt> = Vec::new(); |
| 15 | + let page_path = page.get_path(); |
| 16 | + |
| 17 | + // Handle the boolean properties |
| 18 | + if page.revalidates() { |
| 19 | + render_opts.push(RenderOpt::Revalidated); |
| 20 | + } |
| 21 | + if page.uses_incremental() { |
| 22 | + render_opts.push(RenderOpt::Incremental); |
| 23 | + } |
| 24 | + |
| 25 | + // Handle static path generation |
| 26 | + // Because we iterate over the paths, we need a base path if we're not generating custom ones (that'll be overriden if needed) |
| 27 | + let paths = match page.uses_build_paths() { |
| 28 | + true => { |
| 29 | + render_opts.push(RenderOpt::StaticPaths); |
| 30 | + page.get_build_paths()? |
| 31 | + }, |
| 32 | + false => vec![page_path.clone()] |
| 33 | + }; |
| 34 | + |
| 35 | + // Iterate through the paths to generate initial states if needed |
| 36 | + for path in paths.iter() { |
| 37 | + // If needed, we'll contruct a full path that's URL encoded so we can easily save it as a file |
| 38 | + // BUG: insanely nested paths won't work whatsoever if the filename is too long, maybe hash instead? |
| 39 | + let full_path = match render_opts.contains(&RenderOpt::StaticPaths) { |
| 40 | + true => urlencoding::encode(&format!("{}/{}", &page_path, path)).to_string(), |
| 41 | + // We don't want to concatenate the name twice if we don't have to |
| 42 | + false => page_path.clone() |
| 43 | + }; |
| 44 | + |
| 45 | + // Handle static initial state generation |
| 46 | + // We'll only write a static state if one is explicitly generated |
| 47 | + if page.uses_build_state() { |
| 48 | + render_opts.push(RenderOpt::StaticProps); |
| 49 | + // We pass in the latter part of the path, without the base specifier (because that would be the same for everything in the template) |
| 50 | + let initial_state = page.get_build_state(path.to_string())?; |
| 51 | + let initial_state_str = serde_json::to_string(&initial_state).unwrap(); |
| 52 | + // Write that intial state to a static JSON file |
| 53 | + config_manager |
| 54 | + .write(&format!("./dist/static/{}.json", full_path), &initial_state_str) |
| 55 | + .unwrap(); |
| 56 | + // Prerender the page using that state |
| 57 | + let prerendered = sycamore::render_to_string( |
| 58 | + || |
| 59 | + page.render_for_template(Some(initial_state)) |
| 60 | + ); |
| 61 | + // Write that prerendered HTML to a static file |
| 62 | + config_manager |
| 63 | + .write(&format!("./dist/static/{}.html", full_path), &prerendered) |
| 64 | + .unwrap(); |
| 65 | + } |
| 66 | + |
| 67 | + // Handle server-side rendering |
| 68 | + // By definition, everything here is done at request-time, so there's not really much to do |
| 69 | + // Note also that if a page only uses SSR, it won't get prerendered at build time whatsoever |
| 70 | + if page.uses_request_state() { |
| 71 | + render_opts.push(RenderOpt::Server); |
| 72 | + } |
| 73 | + |
| 74 | + // If the page is very basic, prerender without any state |
| 75 | + if page.is_basic() { |
| 76 | + render_opts.push(RenderOpt::StaticProps); |
| 77 | + let prerendered = sycamore::render_to_string( |
| 78 | + || |
| 79 | + page.render_for_template(None) |
| 80 | + ); |
| 81 | + // Write that prerendered HTML to a static file |
| 82 | + config_manager |
| 83 | + .write(&format!("./dist/static/{}.html", full_path), &prerendered) |
| 84 | + .unwrap(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + Ok(render_opts) |
| 89 | +} |
| 90 | + |
| 91 | +/// 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 | +/// things on the heap. |
| 93 | +/// (Any better solutions are welcome in PRs!) |
| 94 | +#[macro_export] |
| 95 | +macro_rules! build_pages { |
| 96 | + ( |
| 97 | + [$($page:expr),+], |
| 98 | + $config_manager:expr |
| 99 | + ) => { |
| 100 | + let mut render_conf: $crate::render_cfg::RenderCfg = ::std::collections::HashMap::new(); |
| 101 | + $( |
| 102 | + render_conf.insert( |
| 103 | + $page.get_path(), |
| 104 | + $crate::build::build_page($page, $config_manager) |
| 105 | + .unwrap() |
| 106 | + ); |
| 107 | + )+ |
| 108 | + $config_manager |
| 109 | + .write("./dist/render_conf.json", &serde_json::to_string(&render_conf).unwrap()) |
| 110 | + .unwrap(); |
| 111 | + }; |
| 112 | +} |
0 commit comments