@@ -18,14 +18,19 @@ import cats.effect.IO
1818import cats .effect .kernel .Ref
1919import cats .syntax .all .*
2020
21- def generatedIndexHtml (injectStyles : Boolean , modules : Ref [IO , Map [String , String ]], zdt : ZonedDateTime )(
21+ def generatedIndexHtml (
22+ injectStyles : Boolean ,
23+ modules : Ref [IO , Map [String , String ]],
24+ zdt : ZonedDateTime ,
25+ attemptPreload : Boolean
26+ )(
2227 logger : Scribe [IO ]
2328) =
2429 StaticHtmlMiddleware (
2530 HttpRoutes .of[IO ] {
2631 case req @ GET -> Root =>
2732 logger.trace(" Generated index.html" ) >>
28- vanillaTemplate(injectStyles, modules).flatMap: html =>
33+ vanillaTemplate(injectStyles, modules, attemptPreload ).flatMap: html =>
2934 userBrowserCacheHeaders(Response [IO ]().withEntity(html).withStatus(Status .Ok ), zdt, injectStyles)
3035
3136 },
@@ -35,7 +40,7 @@ def generatedIndexHtml(injectStyles: Boolean, modules: Ref[IO, Map[String, Strin
3540 StaticHtmlMiddleware (
3641 HttpRoutes .of[IO ] {
3742 case GET -> Root / " index.html" =>
38- vanillaTemplate(injectStyles, modules).flatMap: html =>
43+ vanillaTemplate(injectStyles, modules, attemptPreload ).flatMap: html =>
3944 userBrowserCacheHeaders(Response [IO ]().withEntity(html).withStatus(Status .Ok ), zdt, injectStyles)
4045
4146 },
@@ -105,20 +110,23 @@ def injectModulePreloads(ref: Ref[IO, Map[String, String]], template: String) =
105110
106111end injectModulePreloads
107112
108- def makeHeader (modules : Seq [(Path , String )], withStyles : Boolean ) =
113+ def makeHeader (modules : Seq [(Path , String )], withStyles : Boolean , attemptPreload : Boolean = false ) =
109114 val scripts =
110115 for
111116 m <- modules
112117 if m._1.toString.endsWith(" .js" )
113- yield link(rel := " modulepreload" , href := s " ${m._1}?hash= ${m._2}" )
118+ yield link(
119+ rel := " modulepreload" ,
120+ href := s " ${m._1}?hash= ${m._2}"
121+ )
114122
115123 html(
116124 head(
117125 meta(
118126 httpEquiv := " Cache-control" ,
119127 content := " no-cache, no-store, must-revalidate"
120128 ),
121- scripts
129+ if attemptPreload then scripts else ()
122130 ),
123131 body(
124132 lessStyle(withStyles),
@@ -143,16 +151,17 @@ def makeInternalPreloads(ref: Ref[IO, Map[String, String]]) =
143151
144152end makeInternalPreloads
145153
146- def vanillaTemplate (withStyles : Boolean , ref : Ref [IO , Map [String , String ]]) =
154+ def vanillaTemplate (withStyles : Boolean , ref : Ref [IO , Map [String , String ]], attemptPreload : Boolean ) =
155+
147156 val preloads = makeInternalPreloads(ref)
148157 preloads.map: modules =>
149158 html(
150159 head(
151160 meta(
152161 httpEquiv := " Cache-control" ,
153- content := " no-cache, no-store, must-revalidate" ,
154- modules
155- )
162+ content := " no-cache, no-store, must-revalidate"
163+ ),
164+ if attemptPreload then modules else ( )
156165 ),
157166 body(
158167 lessStyle(withStyles),
0 commit comments