9
9
10
10
use std:: borrow:: Cow ;
11
11
use std:: ops:: Not ;
12
- use std:: sync:: { Arc , OnceLock } ;
12
+ use std:: sync:: { Arc , LazyLock , OnceLock } ;
13
13
14
14
use axum:: extract:: Request ;
15
15
use axum:: middleware:: Next ;
@@ -32,14 +32,12 @@ const PATH_PREFIX_CRATES: &str = "/crates/";
32
32
type TemplateEnvFut = Shared < BoxFuture < ' static , Arc < minijinja:: Environment < ' static > > > > ;
33
33
type TemplateCache = moka:: future:: Cache < Cow < ' static , str > , String > ;
34
34
35
- /// Initialize [`minijinja::Environment`] given the path to the index.html file. This should
36
- /// only be done once as it will load said file from persistent storage.
37
- async fn init_template_env (
38
- index_html_template_path : impl AsRef < Path > ,
39
- ) -> Arc < minijinja:: Environment < ' static > > {
40
- let template_j2 = tokio:: fs:: read_to_string ( index_html_template_path. as_ref ( ) )
35
+ /// Initialize [`minijinja::Environment`] given the index.html file at `dist/index.html`.
36
+ /// This should only be done once as it will load said file from persistent storage.
37
+ async fn init_template_env ( ) -> Arc < minijinja:: Environment < ' static > > {
38
+ let template_j2 = tokio:: fs:: read_to_string ( "dist/index.html" )
41
39
. await
42
- . expect ( "Error loading index.html template. Is the frontend package built yet?" ) ;
40
+ . expect ( "Error loading dist/ index.html template. Is the frontend package built yet?" ) ;
43
41
44
42
let mut env = Environment :: empty ( ) ;
45
43
env. add_template_owned ( INDEX_TEMPLATE_NAME , template_j2)
@@ -55,7 +53,8 @@ fn init_html_cache(max_capacity: u64) -> TemplateCache {
55
53
}
56
54
57
55
pub async fn serve_html ( state : AppState , request : Request , next : Next ) -> Response {
58
- static TEMPLATE_ENV : OnceLock < TemplateEnvFut > = OnceLock :: new ( ) ;
56
+ static TEMPLATE_ENV : LazyLock < TemplateEnvFut > =
57
+ LazyLock :: new ( || init_template_env ( ) . boxed ( ) . shared ( ) ) ;
59
58
static RENDERED_HTML_CACHE : OnceLock < TemplateCache > = OnceLock :: new ( ) ;
60
59
61
60
let path = & request. uri ( ) . path ( ) ;
@@ -75,7 +74,7 @@ pub async fn serve_html(state: AppState, request: Request, next: Next) -> Respon
75
74
}
76
75
77
76
// `state.config.og_image_base_url` will always be `Some` as that's required
78
- // if `state.config.index_html_template_path ` is `Some `, and otherwise this
77
+ // if `state.config.serve_html ` is `true `, and otherwise this
79
78
// middleware won't be executed; see `crate::middleware::apply_axum_middleware`.
80
79
let og_image_base_url = state. config . og_image_base_url . as_ref ( ) . unwrap ( ) ;
81
80
let og_image_url = generate_og_image_url ( path, og_image_base_url) ;
@@ -84,16 +83,10 @@ pub async fn serve_html(state: AppState, request: Request, next: Next) -> Respon
84
83
let html = RENDERED_HTML_CACHE
85
84
. get_or_init ( || init_html_cache ( state. config . html_render_cache_max_capacity ) )
86
85
. get_with_by_ref ( & og_image_url, async {
87
- // `OnceLock::get_or_init ` blocks as long as its intializer is running in another thread.
86
+ // `LazyLock::deref ` blocks as long as its intializer is running in another thread.
88
87
// Note that this won't take long, as the constructed Futures are not awaited
89
88
// during initialization.
90
- let template_env = TEMPLATE_ENV . get_or_init ( || {
91
- // At this point we can safely assume `state.config.index_html_template_path` is `Some`,
92
- // as this middleware won't be executed otherwise; see `crate::middleware::apply_axum_middleware`.
93
- init_template_env ( state. config . index_html_template_path . clone ( ) . unwrap ( ) )
94
- . boxed ( )
95
- . shared ( )
96
- } ) ;
89
+ let template_env = & * TEMPLATE_ENV ;
97
90
98
91
// Render the HTML given the OG image URL
99
92
let env = template_env. clone ( ) . await ;
0 commit comments