Skip to content

Commit 003f000

Browse files
committed
injectr preloads into templates too
1 parent d6a4524 commit 003f000

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

justfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ jvmLinker:
3333
mill project.run --build-tool scala-cli --project-dir /Users/simon/Code/helloScalaJs --out-dir /Users/simon/Code/helloScalaJs/out --extra-build-args --js-cli-on-jvm --port 3007
3434

3535
serveMill:
36-
mill project.run --build-tool mill --project-dir /Users/simon/Code/mill-full-stack/mill-full-stack --styles-dir /Users/simon/Code/mill-full-stack/mill-full-stack/frontend/ui/assets --out-dir /Users/simon/Code/mill-full-stack/mill-full-stack/out/frontend/fastLinkJS.dest --log-level info --proxy-target-port 8080 --proxy-prefix-path /api --port 3007 --mill-module-name frontend
36+
mill -j 0 project.run --build-tool mill --project-dir /Users/simon/Code/mill-full-stack/mill-full-stack \
37+
--path-to-index-html /Users/simon/Code/mill-full-stack/mill-full-stack/frontend/ui \
38+
--out-dir /Users/simon/Code/mill-full-stack/mill-full-stack/out/frontend/fastLinkJS.dest \
39+
--log-level info \
40+
--port 3007 \
41+
--mill-module-name frontend \
42+
--proxy-prefix-path /api \
43+
--proxy-target-port 8080
3744

3845
setupPlaywright:
3946
cs launch com.microsoft.playwright:playwright:1.41.1 -M "com.microsoft.playwright.CLI" -- install --with-deps

project/src/htmlGen.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def makeInternalPreloads(ref: Ref[IO, Map[String, String]]) =
9797
for
9898
m <- modules
9999
if m._1.toString.endsWith(".js") && m._1.toString.startsWith("internal")
100-
yield link(rel := "modulepreload", src := s"${m._1}")
100+
yield link(rel := "modulepreload", href := s"${m._1}?h=${m._2}")
101101
end for
102102
}
103103

project/src/routes.scala

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import cats.effect.kernel.Resource
3737
import cats.syntax.all.*
3838

3939
import _root_.io.circe.syntax.EncoderOps
40+
import org.http4s.Http
4041

4142
def routes[F[_]: Files: MonadThrow](
4243
stringPath: String,
@@ -143,12 +144,41 @@ def routes[F[_]: Files: MonadThrow](
143144
case None => generatedIndexHtml(injectStyles = false, modules)
144145

145146
case Some(IndexHtmlConfig.IndexHtmlPath(path)) =>
146-
StaticMiddleware(
147-
Router(
148-
"" -> fileService[IO](FileService.Config(path.toString()))
149-
),
150-
fs2.io.file.Path(path.toString())
151-
)(logger)
147+
// StaticMiddleware(
148+
// Router(
149+
// "" ->
150+
HttpRoutes
151+
.of[IO] {
152+
case req @ GET -> Root =>
153+
StaticFile
154+
.fromPath[IO](path / "index.html")
155+
.getOrElseF(NotFound())
156+
.flatMap {
157+
f =>
158+
f.body
159+
.through(text.utf8.decode)
160+
.compile
161+
.string
162+
.flatMap {
163+
body =>
164+
for str <- injectModulePreloads(modules, body)
165+
yield
166+
val bytes = str.getBytes()
167+
f.withEntity(bytes)
168+
Response[IO]().withEntity(bytes).putHeaders("Content-Type" -> "text/html")
169+
170+
}
171+
}
172+
173+
}
174+
.combineK(
175+
StaticMiddleware(
176+
Router(
177+
"" -> fileService[IO](FileService.Config(path.toString()))
178+
),
179+
fs2.io.file.Path(path.toString())
180+
)(logger)
181+
)
152182

153183
case Some(IndexHtmlConfig.StylesOnly(stylesPath)) =>
154184
NoCacheMiddlware(

project/test/src/liveServer.test.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ trait PlaywrightTest extends CatsEffectSuite:
248248
Ok
249249
) >>
250250
assertIO(
251-
client.expect[String](s"http://localhost:$port"),
252-
vanilla.render
251+
client.expect[String](s"http://localhost:$port").map(_.filterNot(_.isWhitespace)),
252+
vanilla.render.filterNot(_.isWhitespace)
253253
) >>
254254
assertIO(
255-
client.expect[String](s"http://localhost:$port/app/spaRoute"),
256-
vanilla.render
255+
client.expect[String](s"http://localhost:$port/app/spaRoute").map(_.filterNot(_.isWhitespace)),
256+
vanilla.render.filterNot(_.isWhitespace)
257257
)
258258

259259
}

0 commit comments

Comments
 (0)