99
1010use std:: borrow:: Cow ;
1111use std:: ops:: Not ;
12- use std:: sync:: { Arc , OnceLock } ;
12+ use std:: sync:: { Arc , LazyLock , OnceLock } ;
1313
1414use axum:: extract:: Request ;
1515use axum:: middleware:: Next ;
@@ -32,14 +32,12 @@ const PATH_PREFIX_CRATES: &str = "/crates/";
3232type TemplateEnvFut = Shared < BoxFuture < ' static , Arc < minijinja:: Environment < ' static > > > > ;
3333type TemplateCache = moka:: future:: Cache < Cow < ' static , str > , String > ;
3434
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" )
4139 . 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?" ) ;
4341
4442 let mut env = Environment :: empty ( ) ;
4543 env. add_template_owned ( INDEX_TEMPLATE_NAME , template_j2)
@@ -55,7 +53,8 @@ fn init_html_cache(max_capacity: u64) -> TemplateCache {
5553}
5654
5755pub 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 ( ) ) ;
5958 static RENDERED_HTML_CACHE : OnceLock < TemplateCache > = OnceLock :: new ( ) ;
6059
6160 let path = & request. uri ( ) . path ( ) ;
@@ -75,7 +74,7 @@ pub async fn serve_html(state: AppState, request: Request, next: Next) -> Respon
7574 }
7675
7776 // `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
7978 // middleware won't be executed; see `crate::middleware::apply_axum_middleware`.
8079 let og_image_base_url = state. config . og_image_base_url . as_ref ( ) . unwrap ( ) ;
8180 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
8483 let html = RENDERED_HTML_CACHE
8584 . get_or_init ( || init_html_cache ( state. config . html_render_cache_max_capacity ) )
8685 . 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.
8887 // Note that this won't take long, as the constructed Futures are not awaited
8988 // 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 ;
9790
9891 // Render the HTML given the OG image URL
9992 let env = template_env. clone ( ) . await ;
0 commit comments