Skip to content

Commit acbd14d

Browse files
committed
styles and SPA...
1 parent 828b695 commit acbd14d

File tree

4 files changed

+73
-31
lines changed

4 files changed

+73
-31
lines changed

project/src/htmlGen.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def vanillaTemplate(withStyles: Boolean) = html(
102102
),
103103
body(
104104
lessStyle(withStyles),
105-
script(src := "main.js", `type` := "module"),
105+
script(src := "/main.js", `type` := "module"),
106106
div(id := "app"),
107107
refreshScript
108108
)

project/src/middleware/noCache.middleware.scala

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ import org.typelevel.ci.CIStringSyntax
66
import cats.data.Kleisli
77
import cats.effect.*
88
import cats.effect.IO
9+
import scribe.Scribe
10+
import cats.data.OptionT
11+
import cats.syntax.all.*
912

1013
object NoCacheMiddlware:
1114

12-
def apply(service: HttpRoutes[IO]): HttpRoutes[IO] = Kleisli {
15+
def apply(service: HttpRoutes[IO])(logger: Scribe[IO]): HttpRoutes[IO] = Kleisli {
1316
(req: Request[IO]) =>
14-
service(req).map {
15-
resp =>
16-
resp.putHeaders(
17-
Header.Raw(ci"Cache-Control", "no-cache")
18-
)
19-
}
17+
OptionT.liftF(logger.trace("No cache middleware")) >>
18+
OptionT.liftF(logger.trace(req.toString)) >>
19+
service(req).map {
20+
resp =>
21+
resp.putHeaders(
22+
Header.Raw(ci"Cache-Control", "no-cache")
23+
)
24+
}
2025
}
2126

2227
end NoCacheMiddlware

project/src/routes.scala

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import cats.effect.kernel.Resource
3535
import cats.syntax.all.*
3636

3737
import _root_.io.circe.syntax.EncoderOps
38+
import org.http4s.Uri.Path.SegmentEncoder
3839

3940
def routes[F[_]: Files: MonadThrow](
4041
stringPath: String,
@@ -105,7 +106,7 @@ def routes[F[_]: Files: MonadThrow](
105106
StaticHtmlMiddleware(
106107
HttpRoutes.of[IO] {
107108
case req @ GET -> Root =>
108-
logger.trace(req.headers.toString) >>
109+
logger.trace("Generated index.html") >>
109110
IO(
110111
Response[IO]()
111112
.withEntity(vanillaTemplate(injectStyles))
@@ -151,13 +152,11 @@ def routes[F[_]: Files: MonadThrow](
151152
)(logger)
152153

153154
case Some(IndexHtmlConfig.StylesOnly(stylesPath)) =>
154-
generatedIndexHtml(injectStyles = true).combineK(
155-
NoCacheMiddlware(
156-
Router(
157-
"" -> fileService[IO](FileService.Config(stylesPath.toString()))
158-
)
155+
NoCacheMiddlware(
156+
Router(
157+
"" -> fileService[IO](FileService.Config(stylesPath.toString()))
159158
)
160-
)
159+
)(logger).combineK(generatedIndexHtml(injectStyles = true))
161160

162161
val clientSpaRoutes: HttpRoutes[IO] =
163162
clientRoutingPrefix match
@@ -168,7 +167,7 @@ def routes[F[_]: Files: MonadThrow](
168167
Root / spaRoute
169168
StaticHtmlMiddleware(
170169
HttpRoutes.of[IO] {
171-
case GET -> aPath /: path =>
170+
case GET -> root /: spaRoute /: path =>
172171
// logger.trace(path) >>
173172
IO(
174173
Response[IO]().withEntity(vanillaTemplate(false))
@@ -178,10 +177,9 @@ def routes[F[_]: Files: MonadThrow](
178177
)(logger)
179178

180179
case Some(IndexHtmlConfig.StylesOnly(dir)) =>
181-
// val aPath = Root / spaRoute
182180
StaticHtmlMiddleware(
183181
HttpRoutes.of[IO] {
184-
case GET -> spaRoute /: path =>
182+
case GET -> root /: spaRoute /: path =>
185183
IO(
186184
Response[IO]().withEntity(vanillaTemplate(true))
187185
)

project/test/src/RoutesSpec.scala

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class RoutesSuite extends CatsEffectSuite:
3030

3131
val md = MessageDigest.getInstance("MD5")
3232
val testStr = "const hi = 'Hello, world'"
33+
val simpleCss = "h1 {color: red;}"
3334
val testHash = md.digest(testStr.getBytes()).map("%02x".format(_)).mkString
3435
given filesInstance: Files[IO] = Files.forAsync[IO]
3536

@@ -67,14 +68,23 @@ class RoutesSuite extends CatsEffectSuite:
6768
val externalSyles = FunFixture[os.Path](
6869
setup = test =>
6970
val tempDir = os.temp.dir()
70-
os.write(tempDir / "index.less", testStr)
71+
os.write(tempDir / "index.less", "h1 {color: red;}")
7172
tempDir
7273
,
7374
teardown = tempDir =>
7475
// Always gets called, even if test failed.
7576
os.remove.all(tempDir)
7677
)
7778

79+
override def beforeAll(): Unit =
80+
scribe
81+
.Logger
82+
.root
83+
.clearHandlers()
84+
.clearModifiers()
85+
.withHandler(minimumLevel = Some(Level.get("trace").get))
86+
.replace()
87+
7888
files.test("seed map puts files in the map on start") {
7989
tempDir =>
8090
for
@@ -144,7 +154,7 @@ class RoutesSuite extends CatsEffectSuite:
144154

145155
val app: Resource[IO, HttpApp[IO]] = for
146156
logger <- IO(
147-
aLogger
157+
scribe.cats[IO]
148158
).toResource
149159
fileToHashRef <- Ref[IO].of(Map.empty[String, String]).toResource
150160
_ <- updateMapRef(tempDir.toFs2, fileToHashRef)(logger).toResource
@@ -195,7 +205,7 @@ class RoutesSuite extends CatsEffectSuite:
195205
.use {
196206
resp3 =>
197207
assertEquals(resp3.status.code, 200)
198-
assertIOBoolean(resp3.bodyText.compile.string.map(_.contains("src=\"main.js"))) >>
208+
assertIOBoolean(resp3.bodyText.compile.string.map(_.contains("src=\"/main.js"))) >>
199209
IO.unit
200210
}
201211

@@ -211,17 +221,17 @@ class RoutesSuite extends CatsEffectSuite:
211221
IO.unit
212222
}
213223

214-
val requestHtml2 = Request[IO](uri = uri"/").withHeaders(Header.Raw(ci"If-None-Match", etag))
224+
// val requestHtml2 = Request[IO](uri = uri"/").withHeaders(Header.Raw(ci"If-None-Match", etag))
215225

216-
val checkRespHtml2 = client
217-
.run(requestHtml2)
218-
.use {
219-
respH =>
220-
assertEquals(respH.status.code, 304)
221-
IO.unit
222-
}
226+
// val checkRespHtml2 = client
227+
// .run(requestHtml2)
228+
// .use {
229+
// respH =>
230+
// assertEquals(respH.status.code, 304)
231+
// IO.unit
232+
// }
223233

224-
checkResp1 >> checkResp2 >> checkRespSpa >> checkRespHtml >> checkRespHtml2
234+
checkResp1 >> checkResp2 >> checkRespSpa >> checkRespHtml
225235

226236
}
227237
}
@@ -315,7 +325,36 @@ class RoutesSuite extends CatsEffectSuite:
315325
val responseLess = served(requestLess)
316326

317327
assertIO(responseHtml.map(_.status.code), 200) >>
318-
assertIO(responseLess.map(_.status.code), 200)
328+
assertIO(responseLess.map(_.status.code), 200) >>
329+
assertIO(responseLess.flatMap(_.bodyText.compile.string), simpleCss)
330+
}
331+
}
332+
333+
FunFixture
334+
.map2(files, externalSyles)
335+
.test("That styles and SPA play nicely together") {
336+
(appDir, styleDir) =>
337+
val app = for
338+
logger <- IO(scribe.cats[IO]).toResource
339+
fileToHashRef <- Ref[IO].of(Map.empty[String, String]).toResource
340+
fileToHashMapRef = MapRef.fromSingleImmutableMapRef[IO, String, String](fileToHashRef)
341+
refreshPub <- Topic[IO, Unit].toResource
342+
theseRoutes <- routes(
343+
appDir.toString,
344+
refreshPub,
345+
Some(IndexHtmlConfig.StylesOnly(styleDir.toFs2)),
346+
HttpRoutes.empty[IO],
347+
fileToHashRef,
348+
Some("app")
349+
)(logger)
350+
yield theseRoutes.orNotFound
351+
352+
app.use {
353+
served =>
354+
val requestLess = org.http4s.Request[IO](uri = org.http4s.Uri.unsafeFromString("/index.less"))
355+
val responseLess = served(requestLess)
356+
assertIO(responseLess.map(_.status.code), 200) >>
357+
assertIO(responseLess.flatMap(_.bodyText.compile.string), simpleCss)
319358
}
320359
}
321360

0 commit comments

Comments
 (0)