Skip to content

Commit 0d5c3bc

Browse files
#328 - add doc how to handle reloads on the server in case useHash is false - done (#334)
1 parent 994f001 commit 0d5c3bc

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

etc/wiki/06.-Application.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,73 @@ Nalu offers the possibility to add a custom alert and confirm dialog. In case it
5656

5757
A custom alert needs to implement the `IsCustomAlertPresenter` and a custom confirm dialog needs to implement the `IsCustomConfirmPresenter`.
5858

59+
## Hash-less Routing
60+
61+
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.
62+
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.
63+
In case of Domino-UI you can configure the **ElementsFactory**-class to create an a-tag without the href-attribute.
64+
65+
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.
66+
67+
This is an example of an URL in case useHash is true:
68+
69+
```
70+
http://localhost:8080/index.html#/applicationShell/myRoute01"
71+
```
72+
73+
and this represents the same URI in cae useHash is false:
74+
75+
```
76+
http://localhost:8080/applicationShell/myRoute01"
77+
```
78+
79+
This will work with no difference with one exception:
80+
81+
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.
82+
83+
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:
84+
85+
```java
86+
@Component
87+
@Order(1)
88+
public class RedirectFilter extends GenericFilterBean {
89+
90+
// TODO: add here the list of shell names.
91+
private final String[] uriStarts = new String[] {"/login", "/application"};
92+
93+
@Override
94+
public void doFilter(
95+
ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
96+
throws IOException, jakarta.servlet.ServletException {
97+
HttpServletRequest request = (HttpServletRequest) servletRequest;
98+
HttpServletResponse response = (HttpServletResponse) servletResponse;
99+
if (this.isClientUri(request.getRequestURI())) {
100+
response.sendRedirect("/index.html?uri=" + request.getRequestURI());
101+
return;
102+
}
103+
filterChain.doFilter(servletRequest, servletResponse);
104+
}
105+
106+
/**
107+
* this method recognize path of the clients
108+
**/
109+
private boolean isClientUri(String path) {
110+
for (String s : this.uriStarts) {
111+
if (path.startsWith(s)) {
112+
return true;
113+
}
114+
}
115+
return false;
116+
}
117+
}
118+
```
119+
120+
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.
121+
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
122+
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
123+
will use this as start route.
124+
125+
59126
## Filter Annotation
60127
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.
61128

0 commit comments

Comments
 (0)