Can the `singlePageApp` helper be used on a subpat...
# http4k
l
Can the
singlePageApp
helper be used on a subpath? When it's configured on the root path, it works as expected. But when it's configured on a subpath, it still seems to match any path, even ones outside the subpath.
For example:
Copy code
import org.http4k.core.Method
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.routing.ResourceLoader
import org.http4k.routing.bind
import org.http4k.routing.routes
import org.http4k.routing.singlePageApp
import org.http4k.server.Netty
import org.http4k.server.asServer

fun main() {
  println("running example")

  val handler = routes(
    "/api" bind Method.GET to { Response(Status.OK).body("Hello World!") },
    "/spa" bind singlePageApp(ResourceLoader.Classpath("example")),
  )

  handler.asServer(Netty(port = 8080)).start()
}
The
singlePageApp
matches /spa as well as anything else like /test
d
In theory this should definitely work.... it may be because of the work we did in aligning the routing for v6 that it doesn't seem to 🤔
K 1
l
Ah okay, well good to know we're not using it incorrect or misconfiguring something. We were really scratching our heads the other day. Does the example I shared here identify the issue well enough? Or would it help tremendously if I made a PR with a failing test in: https://github.com/http4k/http4k/blob/master/core/core/src/test/kotlin/org/http4k/routing/SinglePageAppRoutingHttpHandlerTest.kt
On the chance that it helps someone else, we found a workaround that I thought I'd share. Use the
static
helper for the files that exist and manually declare the
index.html
fallback like so:
Copy code
val handler = routes(
//  "/spa" bind singlePageApp(ResourceLoader.Classpath("example")),
  "/spa" bind static(ResourceLoader.Classpath("example")),
  "/spa/{path:.*}" bind { static(ResourceLoader.Classpath("example"))(Request(Method.GET, "index.html")) }
)
Assuming that it makes it easier to track, I created an issue: https://github.com/http4k/http4k/issues/1376
👍 1
s
Hey @leonhardt, I'm having trouble creating a failing scenario, probably because I don't fully understand how your files were structured in the classpath.
would it help tremendously if I made a PR with a failing test
If that can be reproduced there, that'd be great! Otherwise, please provide some examples of files in the classpath that shouldn't be matched in the example here.
🫡 1
l
@s4nchez I wasn't able to quickly wrap my head around SinglePageAppRoutingHttpHandlerTest.kt (in the time I have available today), so I made a simple reproduction project: https://github.com/lnhrdt/http4k-issue-1376 If you read and run the test, I believe the issue will be abundantly clear.
@s4nchez just in case you've already checked out the repo, heads up I just pushed a couple more commits that I think make it even simpler to follow.
s
Those tests are exactly what I had in mind. Thank you for putting them together! I’ll take a look and translate the relevant bits to the http4k router test.
fist bump 1