@@ -653,6 +653,11 @@ impl Site {
653653 clean_site_output_folder ( & self . output_path , self . config . preserve_dotfiles_in_output )
654654 }
655655
656+ /// Minify HTML content if minification is enabled.
657+ fn maybe_minify ( & self , content : String ) -> Result < String > {
658+ if self . config . minify_html { minify:: html ( content) } else { Ok ( content) }
659+ }
660+
656661 /// Handles whether to write to disk or to memory
657662 pub fn write_content (
658663 & self ,
@@ -668,19 +673,11 @@ impl Site {
668673 site_path. push ( component) ;
669674 }
670675
671- let final_content = if !filename. ends_with ( "html" ) || !self . config . minify_html {
672- content
673- } else {
674- match minify:: html ( content) {
675- Ok ( minified_content) => minified_content,
676- Err ( error) => bail ! ( error) ,
677- }
678- } ;
679-
676+ // Content is assumed to already be finalized (minified if needed)
680677 match self . build_mode {
681678 BuildMode :: Disk | BuildMode :: Both => {
682679 let end_path = current_path. join ( filename) ;
683- create_file ( & end_path, & final_content ) ?;
680+ create_file ( & end_path, & content ) ?;
684681 }
685682 _ => ( ) ,
686683 }
@@ -689,7 +686,7 @@ impl Site {
689686 let site_path =
690687 if filename != "index.html" { site_path. join ( filename) } else { site_path } ;
691688
692- SITE_CONTENT . write ( ) . unwrap ( ) . insert ( site_path, final_content ) ;
689+ SITE_CONTENT . write ( ) . unwrap ( ) . insert ( site_path, content ) ;
693690 }
694691 _ => ( ) ,
695692 }
@@ -720,8 +717,19 @@ impl Site {
720717
721718 let output = page. render_html ( & self . tera , & self . config , & self . library . read ( ) . unwrap ( ) ) ?;
722719 let content = self . inject_livereload ( output) ;
720+
721+ // Determine if we should minify based on template extension
722+ let template = page. meta . template . as_deref ( ) . unwrap_or ( "page.html" ) ;
723+ let final_content = if template. to_lowercase ( ) . ends_with ( ".html" )
724+ || template. to_lowercase ( ) . ends_with ( ".htm" )
725+ {
726+ self . maybe_minify ( content) ?
727+ } else {
728+ content
729+ } ;
730+
723731 let components: Vec < & str > = page. path . split ( '/' ) . collect ( ) ;
724- let current_path = self . write_content ( & components, "index.html" , content ) ?;
732+ let current_path = self . write_content ( & components, "index.html" , final_content ) ?;
725733
726734 // Copy any asset we found previously into the same directory as the index.html
727735 self . copy_assets ( page. file . path . parent ( ) . unwrap ( ) , & page. assets , & current_path) ?;
@@ -889,7 +897,11 @@ impl Site {
889897 None => "index.html" ,
890898 } ;
891899 let content = render_redirect_template ( permalink, & self . tera ) ?;
892- self . write_content ( & split, page_name, content) ?;
900+
901+ // Aliases are always HTML redirects - minify if enabled
902+ let final_content = self . maybe_minify ( content) ?;
903+
904+ self . write_content ( & split, page_name, final_content) ?;
893905 Ok ( ( ) )
894906 }
895907
@@ -917,7 +929,11 @@ impl Site {
917929 context. insert ( "lang" , & self . config . default_language ) ;
918930 let output = render_template ( "404.html" , & self . tera , context, & self . config . theme ) ?;
919931 let content = self . inject_livereload ( output) ;
920- self . write_content ( & [ ] , "404.html" , content) ?;
932+
933+ // 404.html is always HTML - minify if enabled
934+ let final_content = self . maybe_minify ( content) ?;
935+
936+ self . write_content ( & [ ] , "404.html" , final_content) ?;
921937 Ok ( ( ) )
922938 }
923939
@@ -926,6 +942,8 @@ impl Site {
926942 let mut context = Context :: new ( ) ;
927943 context. insert ( "config" , & self . config . serialize ( & self . config . default_language ) ) ;
928944 let content = render_template ( "robots.txt" , & self . tera , context, & self . config . theme ) ?;
945+
946+ // robots.txt is plain text - never minify
929947 self . write_content ( & [ ] , "robots.txt" , content) ?;
930948 Ok ( ( ) )
931949 }
@@ -961,7 +979,11 @@ impl Site {
961979 let list_output =
962980 taxonomy. render_all_terms ( & self . tera , & self . config , & self . library . read ( ) . unwrap ( ) ) ?;
963981 let content = self . inject_livereload ( list_output) ;
964- self . write_content ( & components, "index.html" , content) ?;
982+
983+ // Taxonomy list pages are always HTML - minify if enabled
984+ let final_content = self . maybe_minify ( content) ?;
985+
986+ self . write_content ( & components, "index.html" , final_content) ?;
965987
966988 let library = self . library . read ( ) . unwrap ( ) ;
967989 taxonomy
@@ -986,7 +1008,11 @@ impl Site {
9861008 let single_output =
9871009 taxonomy. render_term ( item, & self . tera , & self . config , & library) ?;
9881010 let content = self . inject_livereload ( single_output) ;
989- self . write_content ( & comp, "index.html" , content) ?;
1011+
1012+ // Taxonomy term pages are always HTML - minify if enabled
1013+ let final_content = self . maybe_minify ( content) ?;
1014+
1015+ self . write_content ( & comp, "index.html" , final_content) ?;
9901016 }
9911017
9921018 if taxonomy. kind . feed {
@@ -1044,6 +1070,8 @@ impl Site {
10441070 let mut context = Context :: new ( ) ;
10451071 context. insert ( "entries" , & all_sitemap_entries) ;
10461072 let sitemap = render_template ( "sitemap.xml" , & self . tera , context, & self . config . theme ) ?;
1073+
1074+ // Sitemaps are XML - never minify
10471075 self . write_content ( & [ ] , "sitemap.xml" , sitemap) ?;
10481076 return Ok ( ( ) ) ;
10491077 }
@@ -1056,6 +1084,8 @@ impl Site {
10561084 let mut context = Context :: new ( ) ;
10571085 context. insert ( "entries" , & chunk) ;
10581086 let sitemap = render_template ( "sitemap.xml" , & self . tera , context, & self . config . theme ) ?;
1087+
1088+ // Sitemaps are XML - never minify
10591089 let file_name = format ! ( "sitemap{}.xml" , i + 1 ) ;
10601090 self . write_content ( & [ ] , & file_name, sitemap) ?;
10611091 let mut sitemap_url = self . config . make_permalink ( & file_name) ;
@@ -1072,6 +1102,8 @@ impl Site {
10721102 main_context,
10731103 & self . config . theme ,
10741104 ) ?;
1105+
1106+ // Sitemap index is XML - never minify
10751107 self . write_content ( & [ ] , "sitemap.xml" , sitemap) ?;
10761108
10771109 Ok ( ( ) )
@@ -1096,6 +1128,7 @@ impl Site {
10961128 for ( feed, feed_filename) in
10971129 feeds. into_iter ( ) . zip ( self . config . languages [ lang] . feed_filenames . iter ( ) )
10981130 {
1131+ // Feeds are XML - never minify
10991132 if let Some ( base) = base_path {
11001133 let mut components = Vec :: new ( ) ;
11011134 for component in base. components ( ) {
@@ -1164,11 +1197,12 @@ impl Site {
11641197 } else {
11651198 Cow :: Owned ( self . config . make_permalink ( redirect_to) )
11661199 } ;
1167- self . write_content (
1168- & components,
1169- "index.html" ,
1170- render_redirect_template ( & permalink, & self . tera ) ?,
1171- ) ?;
1200+ let content = render_redirect_template ( & permalink, & self . tera ) ?;
1201+
1202+ // Section redirects are always HTML - minify if enabled
1203+ let final_content = self . maybe_minify ( content) ?;
1204+
1205+ self . write_content ( & components, "index.html" , final_content) ?;
11721206
11731207 return Ok ( ( ) ) ;
11741208 }
@@ -1182,7 +1216,11 @@ impl Site {
11821216 let output =
11831217 section. render_html ( & self . tera , & self . config , & self . library . read ( ) . unwrap ( ) ) ?;
11841218 let content = self . inject_livereload ( output) ;
1185- self . write_content ( & components, "index.html" , content) ?;
1219+
1220+ // Section pages are always HTML - minify if enabled
1221+ let final_content = self . maybe_minify ( content) ?;
1222+
1223+ self . write_content ( & components, "index.html" , final_content) ?;
11861224 }
11871225
11881226 Ok ( ( ) )
@@ -1233,15 +1271,20 @@ impl Site {
12331271 ) ?;
12341272 let content = self . inject_livereload ( output) ;
12351273
1274+ // Pagination pages are always HTML - minify if enabled
1275+ let final_content = self . maybe_minify ( content) ?;
1276+
12361277 if pager. index > 1 {
1237- self . write_content ( & pager_components, "index.html" , content ) ?;
1278+ self . write_content ( & pager_components, "index.html" , final_content ) ?;
12381279 } else {
1239- self . write_content ( & index_components, "index.html" , content) ?;
1240- self . write_content (
1241- & pager_components,
1242- "index.html" ,
1243- render_redirect_template ( & paginator. permalink , & self . tera ) ?,
1244- ) ?;
1280+ self . write_content ( & index_components, "index.html" , final_content) ?;
1281+
1282+ // The redirect for page 1 is also HTML
1283+ let redirect_content =
1284+ render_redirect_template ( & paginator. permalink , & self . tera ) ?;
1285+ let final_redirect = self . maybe_minify ( redirect_content) ?;
1286+
1287+ self . write_content ( & pager_components, "index.html" , final_redirect) ?;
12451288 }
12461289
12471290 Ok ( ( ) )
0 commit comments