Skip to content

Commit e56bb30

Browse files
committed
Fix silly preload
1 parent ace5b06 commit e56bb30

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

sjsls/src/buildSpaRoute.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def buildSpaRoute(
3636
StaticHtmlMiddleware(
3737
HttpRoutes.of[IO] {
3838
case req @ GET -> _ =>
39-
vanillaTemplate(false, modules).map: html =>
39+
vanillaTemplate(false, modules, injectPreloads).map: html =>
4040
Response[IO]().withEntity(html)
4141

4242
},
@@ -48,7 +48,7 @@ def buildSpaRoute(
4848
StaticHtmlMiddleware(
4949
HttpRoutes.of[IO] {
5050
case GET -> _ =>
51-
vanillaTemplate(true, modules).map: html =>
51+
vanillaTemplate(true, modules, injectPreloads).map: html =>
5252
Response[IO]().withEntity(html)
5353
},
5454
true,

sjsls/src/htmlGen.scala

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ import cats.effect.IO
1818
import cats.effect.kernel.Ref
1919
import 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

106111
end 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

144152
end 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),

sjsls/src/static.routes.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def staticAssetRoutes(
2828
injectPreloads: Boolean
2929
)(logger: Scribe[IO]): HttpRoutes[IO] =
3030
indexOpts match
31-
case None => generatedIndexHtml(injectStyles = false, modules, zdt)(logger)
31+
case None => generatedIndexHtml(injectStyles = false, modules, zdt, injectPreloads)(logger)
3232

3333
case Some(IndexHtmlConfig.IndexHtmlPath(path)) =>
3434
HttpRoutes
@@ -50,7 +50,7 @@ def staticAssetRoutes(
5050
Router(
5151
"" -> fileService[IO](FileService.Config(stylesPath.toString()))
5252
)
53-
)(logger).combineK(generatedIndexHtml(injectStyles = true, modules, zdt)(logger))
53+
)(logger).combineK(generatedIndexHtml(injectStyles = true, modules, zdt, injectPreloads)(logger))
5454

5555
def serveIndexHtml(from: fs2.io.file.Path, modules: Ref[IO, Map[String, String]], injectPreloads: Boolean) = StaticFile
5656
.fromPath[IO](from / "index.html")

sjsls/test/src/UtilityFcts.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class UtilityFcs extends CatsEffectSuite:
1313
modules = Seq(
1414
(fs2.io.file.Path("main.js"), "hash")
1515
),
16-
withStyles = false
16+
withStyles = false,
17+
attemptPreload = true
1718
)
1819

1920
assert(html.render.contains("modulepreload"))

0 commit comments

Comments
 (0)