Skip to content

Commit 5b336df

Browse files
committed
refactor
1 parent 607fd59 commit 5b336df

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

sjsls/src/buildSpaRoute.scala

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import cats.effect.kernel.Async
1313
import org.http4s.scalatags.*
1414
import java.time.ZonedDateTime
1515

16+
/** This is expected to be hidden behind a route with the SPA prefix. It will serve the index.html file from all routes.
17+
*
18+
* @param indexOpts
19+
* @param modules
20+
* @param zdt
21+
* @param logger
22+
* @return
23+
*/
1624
def buildSpaRoute(indexOpts: Option[IndexHtmlConfig], modules: Ref[IO, Map[String, String]], zdt: ZonedDateTime)(
1725
logger: Scribe[IO]
1826
)(using
@@ -23,7 +31,7 @@ def buildSpaRoute(indexOpts: Option[IndexHtmlConfig], modules: Ref[IO, Map[Strin
2331
// Root / spaRoute
2432
StaticHtmlMiddleware(
2533
HttpRoutes.of[IO] {
26-
case req @ GET -> root /: path =>
34+
case req @ GET -> _ =>
2735
vanillaTemplate(false, modules).map: html =>
2836
Response[IO]().withEntity(html)
2937

@@ -35,7 +43,7 @@ def buildSpaRoute(indexOpts: Option[IndexHtmlConfig], modules: Ref[IO, Map[Strin
3543
case Some(IndexHtmlConfig.StylesOnly(dir)) =>
3644
StaticHtmlMiddleware(
3745
HttpRoutes.of[IO] {
38-
case GET -> root /: spaRoute /: path =>
46+
case GET -> _ =>
3947
vanillaTemplate(true, modules).map: html =>
4048
Response[IO]().withEntity(html)
4149
},
@@ -46,25 +54,7 @@ def buildSpaRoute(indexOpts: Option[IndexHtmlConfig], modules: Ref[IO, Map[Strin
4654
case Some(IndexHtmlConfig.IndexHtmlPath(dir)) =>
4755
StaticFileMiddleware(
4856
HttpRoutes.of[IO] {
49-
case req @ GET -> spaRoute /: path =>
50-
StaticFile
51-
.fromPath(dir / "index.html", Some(req))
52-
.getOrElseF(NotFound())
53-
.flatMap {
54-
f =>
55-
f.body
56-
.through(text.utf8.decode)
57-
.compile
58-
.string
59-
.flatMap: body =>
60-
for str <- injectModulePreloads(modules, body)
61-
yield
62-
val bytes = str.getBytes()
63-
f.withEntity(bytes)
64-
f
65-
66-
}
67-
57+
case req @ GET -> _ => serveIndexHtml(dir)
6858
},
6959
dir / "index.html"
7060
)(logger)

sjsls/src/static.routes.scala

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,7 @@ def staticAssetRoutes(
3131
case Some(IndexHtmlConfig.IndexHtmlPath(path)) =>
3232
HttpRoutes
3333
.of[IO] {
34-
case req @ GET -> Root =>
35-
StaticFile
36-
.fromPath[IO](path / "index.html")
37-
.getOrElseF(NotFound())
38-
.flatMap {
39-
f =>
40-
f.body
41-
.through(text.utf8.decode)
42-
.compile
43-
.string
44-
.flatMap {
45-
body =>
46-
for str <- injectModulePreloads(modules, body)
47-
yield
48-
val bytes = str.getBytes()
49-
f.withEntity(bytes)
50-
Response[IO]().withEntity(bytes).putHeaders("Content-Type" -> "text/html")
51-
52-
}
53-
}
34+
case req @ GET -> Root => serveIndexHtml(path)
5435

5536
}
5637
.combineK(
@@ -68,3 +49,23 @@ def staticAssetRoutes(
6849
"" -> fileService[IO](FileService.Config(stylesPath.toString()))
6950
)
7051
)(logger).combineK(generatedIndexHtml(injectStyles = true, modules, zdt)(logger))
52+
53+
def serveIndexHtml(from: fs2.io.file.Path) = StaticFile
54+
.fromPath[IO](from / "index.html")
55+
.getOrElseF(NotFound())
56+
.flatMap {
57+
f =>
58+
f.body
59+
.through(text.utf8.decode)
60+
.compile
61+
.string
62+
.flatMap {
63+
body =>
64+
for str <- injectModulePreloads(modules, body)
65+
yield
66+
val bytes = str.getBytes()
67+
f.withEntity(bytes)
68+
Response[IO]().withEntity(bytes).putHeaders("Content-Type" -> "text/html")
69+
70+
}
71+
}

0 commit comments

Comments
 (0)