1
- // This binary builds all the pages with SSG
1
+ // This binary builds all the templates with SSG
2
2
3
3
use serde:: { Serialize , de:: DeserializeOwned } ;
4
4
use crate :: {
5
- page :: Page ,
5
+ template :: Template ,
6
6
config_manager:: ConfigManager ,
7
7
render_cfg:: RenderOpt
8
8
} ;
9
9
use crate :: errors:: * ;
10
10
use std:: any:: Any ;
11
11
use sycamore:: prelude:: SsrNode ;
12
12
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 > > {
15
15
let mut render_opts: Vec < RenderOpt > = Vec :: new ( ) ;
16
- let page_path = page . get_path ( ) ;
16
+ let template_path = template . get_path ( ) ;
17
17
18
18
// Handle the boolean properties
19
- if page . revalidates ( ) {
19
+ if template . revalidates ( ) {
20
20
render_opts. push ( RenderOpt :: Revalidated ) ;
21
21
}
22
- if page . uses_incremental ( ) {
22
+ if template . uses_incremental ( ) {
23
23
render_opts. push ( RenderOpt :: Incremental ) ;
24
24
}
25
25
26
26
// Handle static path generation
27
27
// 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 ( ) {
29
29
true => {
30
30
render_opts. push ( RenderOpt :: StaticPaths ) ;
31
- page . get_build_paths ( ) ?
31
+ template . get_build_paths ( ) ?
32
32
} ,
33
- false => vec ! [ page_path . clone( ) ]
33
+ false => vec ! [ template_path . clone( ) ]
34
34
} ;
35
35
36
36
// Iterate through the paths to generate initial states if needed
37
37
for path in paths. iter ( ) {
38
38
// If needed, we'll contruct a full path that's URL encoded so we can easily save it as a file
39
39
// BUG: insanely nested paths won't work whatsoever if the filename is too long, maybe hash instead?
40
40
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 ( ) ,
42
42
// 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 ( )
44
44
} ;
45
45
46
46
// Handle static initial state generation
47
47
// We'll only write a static state if one is explicitly generated
48
- if page . uses_build_state ( ) {
48
+ if template . uses_build_state ( ) {
49
49
render_opts. push ( RenderOpt :: StaticProps ) ;
50
50
// 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 ( ) ) ?;
52
52
let initial_state_str = serde_json:: to_string ( & initial_state) . unwrap ( ) ;
53
53
// Write that intial state to a static JSON file
54
54
config_manager
55
55
. write ( & format ! ( "./dist/static/{}.json" , full_path) , & initial_state_str)
56
56
. unwrap ( ) ;
57
- // Prerender the page using that state
57
+ // Prerender the template using that state
58
58
let prerendered = sycamore:: render_to_string (
59
59
||
60
- page . render_for_template ( Some ( initial_state) )
60
+ template . render_for_template ( Some ( initial_state) )
61
61
) ;
62
62
// Write that prerendered HTML to a static file
63
63
config_manager
@@ -67,17 +67,17 @@ pub fn build_page<Props: Serialize + DeserializeOwned + Any>(page: Page<Props, S
67
67
68
68
// Handle server-side rendering
69
69
// 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 ( ) {
72
72
render_opts. push ( RenderOpt :: Server ) ;
73
73
}
74
74
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 ( ) {
77
77
render_opts. push ( RenderOpt :: StaticProps ) ;
78
78
let prerendered = sycamore:: render_to_string (
79
79
||
80
- page . render_for_template ( None )
80
+ template . render_for_template ( None )
81
81
) ;
82
82
// Write that prerendered HTML to a static file
83
83
config_manager
@@ -89,20 +89,20 @@ pub fn build_page<Props: Serialize + DeserializeOwned + Any>(page: Page<Props, S
89
89
Ok ( render_opts)
90
90
}
91
91
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
93
93
/// things on the heap.
94
94
/// (Any better solutions are welcome in PRs!)
95
95
#[ macro_export]
96
- macro_rules! build_pages {
96
+ macro_rules! build_templates {
97
97
(
98
- [ $( $page : expr) ,+] ,
98
+ [ $( $template : expr) ,+] ,
99
99
$config_manager: expr
100
100
) => {
101
101
let mut render_conf: $crate:: render_cfg:: RenderCfg = :: std:: collections:: HashMap :: new( ) ;
102
102
$(
103
103
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)
106
106
. unwrap( )
107
107
) ;
108
108
) +
0 commit comments