Description
Michael Gmeiner opened SPR-16788 and commented
I'm currently migrating a spring-boot app to spring-boot 2 and webflux. Everything looks great and webflux is an amazing integration of Project Reactor into the spring ecosystem. So first thanks for the great work!
Our app serves a SPA which takes use of the History-API, which means there are urls they can only be accessed via a client side router and do not have a corresponding resource in the serving app. That means it should return the index for every resource which cannot be found on the server because the client side router takes care of that. Before Webflux I implemented this with customizing the WebMvcConfigurer like this:
@Configuration
class WebMvcConfig @Autowired constructor(val resourceProperties: ResourceProperties): WebMvcConfigurer {
override fun addResourceHandlers( registry: ResourceHandlerRegistry ) {
registry.addResourceHandler( "/**" )
.addResourceLocations( *resourceProperties.staticLocations )
.setCachePeriod( 0 ) // no cache
.resourceChain( resourceProperties.chain.isCache )
.addResolver(object: PathResourceResolver() {
override fun resolveResource(request: HttpServletRequest?, requestPath: String, locations: MutableList<out Resource>, chain: ResourceResolverChain): Resource {
return super.resolveResource(request, requestPath, locations, chain) ?: super.resolveResource( request, "/index.html", locations, chain )
}
})
}
}
In Webflux this could be also possbile when i register my own lookup function for resources which just overrides the
org.springframework.web.reactive.function.server.PathResourceLookupFunction
But that is not possible because the PathResourceLookupFunction class is package private.
My question is now if it would be possible to make the PathResourceLookupFunction class public and make it easy to add something like a fallback resource for it?
Or: provide a function on RouterFunctions to create resource mappings with fallback option:
For example like:
RouterFunctions.resourcesWithFallback("/**", ClassPathResource("static/"), ClassPathResource("static/index.html"))
Please let me know your thoughts about that or if you have any other suggestions to achieve that. If you're ok with some possible solution i described above i could also provide a PR for this.
Thanks in advance
Affects: 5.0.5
Referenced from: commits bfb2eff