mirror of
https://github.com/phfroidmont/scalive.git
synced 2025-12-25 05:26:59 +01:00
Serve hashed static resources
This commit is contained in:
parent
3278ddbd1c
commit
5381487d4f
8 changed files with 141 additions and 14 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import zio.*
|
||||
import zio.http.*
|
||||
import zio.http.codec.PathCodec
|
||||
import zio.logging.ConsoleLoggerConfig
|
||||
import zio.logging.LogColor
|
||||
import zio.logging.LogFilter
|
||||
|
|
@ -47,7 +48,9 @@ object Example extends ZIOAppDefault:
|
|||
)
|
||||
)
|
||||
|
||||
val routes = liveRouter.routes @@ Middleware.serveResources(Path.empty / "static", "public")
|
||||
val routes =
|
||||
liveRouter.routes @@
|
||||
ServeHashedResourcesMiddleware(Path.empty / "static", "public")
|
||||
|
||||
override val run = Server.serve(routes).provide(Server.default)
|
||||
end Example
|
||||
|
|
|
|||
|
|
@ -1,6 +1,24 @@
|
|||
import scalive.*
|
||||
|
||||
object RootLayout:
|
||||
import java.nio.file.Paths
|
||||
|
||||
private val runtime = zio.Runtime.default
|
||||
|
||||
// TODO externalize config in common with ServeHashedResourcesMiddleware
|
||||
private lazy val resourceRoot: java.nio.file.Path =
|
||||
Option(getClass.getClassLoader.getResource("public"))
|
||||
.map(url => Paths.get(url.toURI))
|
||||
.getOrElse(throw new IllegalArgumentException("public resources directory not found"))
|
||||
|
||||
private def hashOrDie(rel: String): String =
|
||||
zio.Unsafe.unsafe { implicit u =>
|
||||
runtime.unsafe.run(StaticAssetHasher.hashedPath(rel, resourceRoot)).getOrThrowFiberFailure()
|
||||
}
|
||||
|
||||
private val hashedJs = s"/static/${hashOrDie("app.js")}"
|
||||
private val hashedCss = s"/static/${hashOrDie("app.css")}"
|
||||
|
||||
def apply(content: HtmlElement): HtmlElement =
|
||||
htmlRootTag(
|
||||
lang := "en",
|
||||
|
|
@ -12,12 +30,13 @@ object RootLayout:
|
|||
defer := true,
|
||||
phx.trackStatic := true,
|
||||
typ := "text/javascript",
|
||||
src := "/static/app.js"
|
||||
src := hashedJs
|
||||
),
|
||||
linkTag(phx.trackStatic := true, rel := "stylesheet", href := "/static/app.css"),
|
||||
linkTag(phx.trackStatic := true, rel := "stylesheet", href := hashedCss),
|
||||
titleTag("Scalive Example")
|
||||
),
|
||||
bodyTag(
|
||||
content
|
||||
)
|
||||
)
|
||||
end RootLayout
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue