You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi http4k! First of all, thanks for the great library.
I noticed that when applying filters to SinglePageAppRoutingHandler, the filters are in practice applied to inner static routing handler.
That means that during the SPA's rewrite of certain requests to its root (typically "/public"), the filters are applied with that URI and not the original URI that was requested.
This may or may not be what you expect as a user of http4k.
In our use case, it's a problem, because it makes it impossible/hard to redirect the user based on their original request.
I understand that there may be other use cases where the current behavior might be more appropriate (although none springs to mind right now).
In any case, the behavior is easily tweaked, by maintaining the filter in SinglePageAppRoutingHandler, as opposed to inside the staticHandler, as such:
data class SinglePageAppRoutingHandler(
private val pathSegments: String,
private val staticHandler: RoutingHttpHandler,
private val filter: Filter = Filter.NoOp, // <-- new
) : RoutingHttpHandler {
private val handlerWithoutFilter = { request: Request ->
val matchOnStatic = when (val matchResult = staticHandler.match(request)) {
is RouterMatch.MatchingHandler -> matchResult(request)
else -> null
}
val matchOnIndex = when (val matchResult = staticHandler.match(get(pathSegments))) {
is RouterMatch.MatchingHandler -> matchResult
else -> null
}
val fallbackHandler = matchOnIndex ?: { Response(NOT_FOUND) }
matchOnStatic ?: fallbackHandler(get(pathSegments))
}
private val handlerWithFilter = filter.then(handlerWithoutFilter)
override fun invoke(request: Request): Response = handlerWithFilter(request)
override fun match(request: Request) = when (request.method) {
Method.OPTIONS -> RouterMatch.MethodNotMatched(RouterDescription("template == '$pathSegments'"))
else -> RouterMatch.MatchingHandler(handlerWithFilter, description)
}
override fun withFilter(new: Filter) = copy(filter = new.then(filter))
override fun withBasePath(new: String) =
copy(pathSegments = new + pathSegments, staticHandler = staticHandler.withBasePath(new))
}
The text was updated successfully, but these errors were encountered:
At first glance, this looks like a relatively common issue of when filters are applied (pre vs post routing). To be sure though, are you able to put together a minimal failing test case demonstrating the behaviour you'd like to see? If you're confident about how to design a fix, feel free to put those in a PR and we'd be happy to review it.
Hi http4k! First of all, thanks for the great library.
I noticed that when applying filters to
SinglePageAppRoutingHandler
, the filters are in practice applied to inner static routing handler.That means that during the SPA's rewrite of certain requests to its root (typically "/public"), the filters are applied with that URI and not the original URI that was requested.
This may or may not be what you expect as a user of http4k.
In our use case, it's a problem, because it makes it impossible/hard to redirect the user based on their original request.
I understand that there may be other use cases where the current behavior might be more appropriate (although none springs to mind right now).
In any case, the behavior is easily tweaked, by maintaining the filter in
SinglePageAppRoutingHandler
, as opposed to inside thestaticHandler
, as such:The text was updated successfully, but these errors were encountered: