Skip to content

Commit

Permalink
#328 - add doc how to handle reloads on the server in case useHash is…
Browse files Browse the repository at this point in the history
… false - done (#334)
  • Loading branch information
FrankHossfeld authored Aug 12, 2023
1 parent 994f001 commit 0d5c3bc
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions etc/wiki/06.-Application.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 0d5c3bc

Please sign in to comment.