@@ -43,25 +43,25 @@ pub fn get_page(
43
43
if let Some ( template_root_path) = render_cfg. get ( path) {
44
44
template_name = template_root_path. to_string ( ) ;
45
45
}
46
- // Next, an ISR match (more complex)
47
- // We progressively look for more and more specificity of the path, adding each segment
48
- // That way, we're searching forwards rather than backwards, which is more efficient
49
- let path_segments: Vec < & str > = path. split ( '/' ) . collect ( ) ;
50
- for ( idx, _) in path_segments. iter ( ) . enumerate ( ) {
51
- // Make a path out of this and all the previous segments
52
- // For some reason, [0..0] gives nothing, so we need to `match` here
53
- let path_to_try = match idx {
54
- 0 => path_segments[ 0 ] . to_string ( ) ,
55
- _ => path_segments[ 0 ..idx] . join ( "/" )
56
- } + "/*" ;
46
+ // Next, an ISR match (more complex), which we only want to run if we didn't get an exact match above
47
+ if template_name. is_empty ( ) {
48
+ // We progressively look for more and more specificity of the path, adding each segment
49
+ // That way, we're searching forwards rather than backwards, which is more efficient
50
+ let path_segments: Vec < & str > = path. split ( '/' ) . collect ( ) ;
51
+ for ( idx, _) in path_segments. iter ( ) . enumerate ( ) {
52
+ // Make a path out of this and all the previous segments
53
+ let path_to_try = path_segments[ 0 ..( idx + 1 ) ] . join ( "/" ) + "/*" ;
57
54
58
- // If we find something, keep going until we don't (maximise specificity)
59
- if let Some ( template_root_path) = render_cfg. get ( & path_to_try) {
60
- template_name = template_root_path. to_string ( ) ;
61
- } else {
62
- break ;
55
+ // If we find something, keep going until we don't (maximise specificity)
56
+ if let Some ( template_root_path) = render_cfg. get ( & path_to_try) {
57
+ template_name = template_root_path. to_string ( ) ;
58
+ } else {
59
+ break ;
60
+ }
63
61
}
64
62
}
63
+
64
+ // if we still have nothing, then the page doesn't exist
65
65
if template_name. is_empty ( ) {
66
66
bail ! ( ErrorKind :: PageNotFound ( path. to_string( ) ) )
67
67
}
@@ -77,7 +77,37 @@ pub fn get_page(
77
77
let state: Option < String > ;
78
78
79
79
// Handle each different type of rendering (static paths have already been done though, so we don't need to deal with them)
80
- if template. uses_build_state ( ) || template. is_basic ( ) {
80
+ // TODO make this system completely modular with state amalgamation
81
+ if template. uses_incremental ( ) {
82
+ // The template uses ISR, check if it's already been rendered before and cached
83
+ let html_res = config_manager. read ( & format ! ( "../app/dist/static/{}.html" , path_encoded) ) ;
84
+ if matches ! ( html_res, Ok ( _) ) && !cfg ! ( debug_assertions) {
85
+ html = html_res. unwrap ( ) ;
86
+ // Get the static JSON (if it exists, but it should)
87
+ state = match config_manager. read ( & format ! ( "../app/dist/static/{}.json" , path_encoded) ) {
88
+ Ok ( state) => Some ( state) ,
89
+ Err ( _) => None
90
+ } ;
91
+ } else {
92
+ // Note that we assume ISR is used with SSG (otherwise it would be completely pointless...)
93
+ // We need to generate and cache this page for future usage
94
+ state = Some (
95
+ template. get_build_state (
96
+ format ! ( "{}/{}" , template. get_path( ) , path)
97
+ ) ?
98
+ ) ;
99
+ html = sycamore:: render_to_string (
100
+ ||
101
+ template. render_for_template ( state. clone ( ) )
102
+ ) ;
103
+ // Cache all that
104
+ config_manager
105
+ . write ( & format ! ( "../app/dist/static/{}.json" , path_encoded) , & state. clone ( ) . unwrap ( ) ) ?;
106
+ // Write that prerendered HTML to a static file
107
+ config_manager
108
+ . write ( & format ! ( "../app/dist/static/{}.html" , path_encoded) , & html) ?;
109
+ }
110
+ } else if template. uses_build_state ( ) || template. is_basic ( ) {
81
111
// Get the static HTML
82
112
html = config_manager. read ( & format ! ( "../app/dist/static/{}.html" , path_encoded) ) ?;
83
113
// Get the static JSON
0 commit comments