Skip to content

Commit a4a5826

Browse files
committed
Should now work with static sites too
1 parent 4e95657 commit a4a5826

File tree

10 files changed

+66
-41
lines changed

10 files changed

+66
-41
lines changed

build.mill

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//| mill-jvm-version: 21
2-
//| mill-version: 1.0.4
2+
//| mill-version: 1.0.5
33
//| mvnDeps:
44
//| - com.lihaoyi::mill-contrib-buildinfo:$MILL_VERSION
55
//| - com.goyeau::mill-scalafix::0.6.0

routes/src/appRoute.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ def appRoute[F[_]: Files](stringPath: String)(using f: Async[F]): HttpRoutes[F]
1717
case req @ GET -> Root / fName ~ "js" =>
1818
StaticFile
1919
.fromPath(fs2.io.file.Path(stringPath) / req.uri.path.renderString, Some(req))
20-
.getOrElseF(
21-
fileService[F](FileService.Config(stringPath)).run(req)
22-
.getOrElseF(f.pure(Response[F](Status.NotFound)))
23-
)
20+
.getOrElseF(f.pure(Response[F](Status.NotFound)))
2421

2522
case req @ GET -> Root / fName ~ "wasm" =>
2623
StaticFile

site/docs/routes.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Routes
2+
3+
Files are served under three seperate routing strategies. Conflicts are resolved in the order listed here.
4+
5+
## `--out-dir` Route
6+
7+
These files are assumed to be created by a build tool emitting `.js`, `.js.map`, `.wasm`, and `.wasm.map` files. They are watched in the directory specified by the `--out-dir` argument, and served at the root of the server. For example, scala-js will usually emit a file called `main.js`.
8+
9+
You'll find it at http://localhost:8080/main.js
10+
11+
12+
## SPA Routes
13+
14+
For things like client side routing to work, the backend must return the `index.html` file for any route that doesn't match a static asset. This is done by specifying a `--client-routes-prefix` argument. For example, if you specify `--client-routes-prefix app`, then any request to http://localhost:8080/app/anything/here will return the `index.html` file in response to a request made by the browser. The browser will then handle the routing / rendering.
15+
16+
## Static Routes
17+
18+
Static assets are served from the directory specified by the `--path-to-index-html` argument to the root of the site. For example, if you have a file `styles/index.less` in that directory, it will be served at http://localhost:8080/styles/index.less

sjsls/src/BuildTool.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.github.quafadas.sjsls
2+
3+
sealed trait BuildTool(val invokedVia: String)
4+
class ScalaCli
5+
extends BuildTool(
6+
if isWindows then "scala-cli.bat" else "scala-cli"
7+
)
8+
class Mill
9+
extends BuildTool(
10+
if isWindows then "mill.bat" else "mill"
11+
)
12+
13+
class NoBuildTool extends BuildTool("")

sjsls/src/buildRunner.scala

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ import cats.effect.ResourceIO
1414
import cats.effect.kernel.Resource
1515
import cats.syntax.all.*
1616

17-
sealed trait BuildTool(val invokedVia: String)
18-
class ScalaCli
19-
extends BuildTool(
20-
if isWindows then "scala-cli.bat" else "scala-cli"
21-
)
22-
class Mill
23-
extends BuildTool(
24-
if isWindows then "mill.bat" else "mill"
25-
)
26-
27-
class NoBuildTool extends BuildTool("")
28-
2917
private lazy val isWindows: Boolean =
3018
System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")
3119

sjsls/src/liveServer.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ object LiveServer extends IOApp:
9393
linkingTopic <- Topic[IO, Unit].toResource
9494
client <- EmberClientBuilder.default[IO].build
9595
baseDirPath <- lsc.baseDir.fold(Files[IO].currentWorkingDirectory.toResource)(toDirectoryPath)
96-
outDirPath <- lsc.outDir.fold(Files[IO].tempDirectory)(toDirectoryPath)
96+
outDirPath <- lsc
97+
.outDir
98+
.fold {
99+
// If we arent' responsible for linking and the user has specified a location for index html, we're essentially serving a static site.
100+
(lsc.buildTool, lsc.indexHtmlTemplate) match
101+
case (_: NoBuildTool, Some(indexHtml)) => toDirectoryPath(indexHtml)
102+
case _ => Files[IO].tempDirectory
103+
}(toDirectoryPath)
97104
outDirString = outDirPath.show
98105
indexHtmlTemplatePath <- lsc.indexHtmlTemplate.traverse(toDirectoryPath)
99106
stylesDirPath <- lsc.stylesDir.traverse(toDirectoryPath)

sjsls/src/middleware/ETagMiddleware.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object ETagMiddleware:
3939
)
4040
)
4141
case None =>
42-
logger.debug("No hash found in map at path :" + req.uri.toString) >>
42+
logger.debug(s"No hash found in map at path: ${req.uri.toString}. Adding revalidate headers") >>
4343
IO(
4444
resp.putHeaders(
4545
Header.Raw(ci"Cache-control", "Must-Revalidate"),

sjsls/src/middleware/staticpathMiddleware.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ object StaticMiddleware:
8787
(req: Request[IO]) =>
8888
val epochInstant: Instant = Instant.EPOCH
8989
val fullPath = staticDir / req.uri.path.toString.drop(1)
90-
91-
cachedFileResponse(epochInstant, fullPath, req, service)(logger: Scribe[IO])
90+
OptionT.liftF(logger.debug(s"Attempting to serve static file at: $fullPath")) >>
91+
cachedFileResponse(epochInstant, fullPath, req, service)(logger: Scribe[IO])
9292
}
9393
end StaticMiddleware

sjsls/src/routes.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def routes[F[_]: Files: MonadThrow](
5050
.combineK(proxyRoutes)
5151
.combineK(routes)
5252
)
53-
54-
IO(refreshableApp).toResource
53+
logger.info("Routes created at : ").toResource >>
54+
logger.info("Path: " + stringPath).toResource >>
55+
IO(refreshableApp).toResource
5556

5657
end routes

sjsls/src/staticRoutes.scala

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,23 @@ def staticAssetRoutes(
5252
)
5353
)(logger).combineK(generatedIndexHtml(injectStyles = true, modules, zdt, injectPreloads)(logger))
5454

55-
def serveIndexHtml(from: fs2.io.file.Path, modules: Ref[IO, Map[String, String]], injectPreloads: Boolean) = StaticFile
56-
.fromPath[IO](from / "index.html")
57-
.getOrElseF(NotFound())
58-
.flatMap {
59-
f =>
60-
f.body
61-
.through(text.utf8.decode)
62-
.compile
63-
.string
64-
.flatMap {
65-
body =>
66-
for str <- if injectPreloads then (injectModulePreloads(modules, body)) else IO.pure(body)
67-
yield
68-
val bytes = str.getBytes()
69-
f.withEntity(bytes)
70-
Response[IO]().withEntity(bytes).putHeaders("Content-Type" -> "text/html")
55+
def serveIndexHtml(from: fs2.io.file.Path, modules: Ref[IO, Map[String, String]], injectPreloads: Boolean) =
56+
StaticFile
57+
.fromPath[IO](from / "index.html")
58+
.getOrElseF(NotFound())
59+
.flatMap {
60+
f =>
61+
f.body
62+
.through(text.utf8.decode)
63+
.compile
64+
.string
65+
.flatMap {
66+
body =>
67+
for str <- if injectPreloads then (injectModulePreloads(modules, body)) else IO.pure(body)
68+
yield
69+
val bytes = str.getBytes()
70+
f.withEntity(bytes)
71+
Response[IO]().withEntity(bytes).putHeaders("Content-Type" -> "text/html")
7172

72-
}
73-
}
73+
}
74+
}

0 commit comments

Comments
 (0)