From 0d5c3bc6dd48f4f74715828a8bde1cd2febdb5d3 Mon Sep 17 00:00:00 2001 From: Frank Hossfeld Date: Sat, 12 Aug 2023 11:16:00 +0200 Subject: [PATCH] #328 - add doc how to handle reloads on the server in case useHash is false - done (#334) --- etc/wiki/06.-Application.md | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/etc/wiki/06.-Application.md b/etc/wiki/06.-Application.md index 0c7d27e0..9f2076e3 100644 --- a/etc/wiki/06.-Application.md +++ b/etc/wiki/06.-Application.md @@ -56,6 +56,73 @@ Nalu offers the possibility to add a custom alert and confirm dialog. In case it A custom alert needs to implement the `IsCustomAlertPresenter` and a custom confirm dialog needs to implement the `IsCustomConfirmPresenter`. +## Hash-less Routing + +Usually Nalu uses hashes to route. A hash looks something like this: **#/applicationShell/myroute01**. In case you want to use anchors or use Domino-UI v2 as widget library, you might run into trouble. +For example Domino-UI generates **href="#"** attributes for links. This is correct, but, when clicked will update the hash which wil trigger a - unwanted - routing. +In case of Domino-UI you can configure the **ElementsFactory**-class to create an a-tag without the href-attribute. + +Nalu offers also an alternative. By setting the **useHash**-attribute of the **Applicaiton**-interface to **false**, Nalu will not use a hash. Instead it uses the URI. + +This is an example of an URL in case useHash is true: + +``` +http://localhost:8080/index.html#/applicationShell/myRoute01" +``` + +and this represents the same URI in cae useHash is false: + +``` +http://localhost:8080/applicationShell/myRoute01" +``` + +This will work with no difference with one exception: + +In case of a reload, the hash will be ignored on the server side and the hash-less URI will create a Status 404 because the path is unknown. + +To avoid that, you have to implement a filter, that recognize this URIs and redirect after updating the URL. Here is an example (based on Spring Boot v3) how the filter looks like: + +```java +@Component +@Order(1) +public class RedirectFilter extends GenericFilterBean { + + // TODO: add here the list of shell names. + private final String[] uriStarts = new String[] {"/login", "/application"}; + + @Override + public void doFilter( + ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) + throws IOException, jakarta.servlet.ServletException { + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + if (this.isClientUri(request.getRequestURI())) { + response.sendRedirect("/index.html?uri=" + request.getRequestURI()); + return; + } + filterChain.doFilter(servletRequest, servletResponse); + } + + /** + * this method recognize path of the clients + **/ + private boolean isClientUri(String path) { + for (String s : this.uriStarts) { + if (path.startsWith(s)) { + return true; + } + } + return false; + } +} +``` + +The classes need to identify the routes of the clients. The easiest way to do that is look for the shell names at the start of the path. +In case a route is identified, change the route to something like **index.html?uri=[route]** and redirect the URL. Now the client calls the server +again. This time with and URL that looks like: **index.html?uri=[route]**. Now the server works as expected and because of the **uri**-key Nalu +will use this as start route. + + ## Filter Annotation Nalu allows you to use filters to stop routings in order to interrupt a route before it is handled. In case a route is interrupted, you can redirect to another route.