I have a Quarkus application which all packages an Angular SPA (bundled in the JAR). Quarkus provides the backend API routes for the frontend to consume. The Angular application build is copied to the META-INF/resources path in the target when the Quarkus application is built.
I am not using JAX-RS to register my routes. Instead I am directly registering them programmatically on the Vertx router inside the fun onRouterReady(@Observes router: Router) method.
I would like any route which is not recognised (either as a Vertx-registered route or as a static resource) to be re-directed to index.html so that my Angular application can load from all of its routes. Unfortunately I have not been able to find any way to do this.
I do not know how all the Quarkus routes function. I suspect that Quarkus itself is registering some routes using Servlets (or perhaps Vertx) because index.html is properly served on the root but my authentication root can prevent loading without authentication.
I have tried various things I have found through searches, such as:
router.route("/*").handler(StaticHandler.create())
router.route().handler({ context: RoutingContext ->
context.reroute("index.html)
})
where the idea is to create a static handler, followed by a catch-all which will re-write the request to index.html so that it is then caught by the static handler but this just results in an infinite loop (the static handler does not seem to be able to find index.html).
I have also tried adding a filter via JAX-RS (which I would like to avoid) but this does not work either.
There must be some way to set this up and I am sure that Quarkus code actually does somewhere, but I cannot find it. Any ideas much appreciated.
You need to observe Router somewhere in application scoped class and set 404 error handler to redirect to index.html
public void init(@Observes Router router) {
router.errorHandler(404, routingContext -> {
routingContext.response().setStatusCode(302).putHeader("Location", "/index.html").end();
});
}
P.S using routingContext.reroute("index.html"); will work as well, but it will keep path as it was(e.g you wrote /testabc, it will show your html file but path won't be /index.html). If you need exactly this then replace it with your option inside errorHandler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With