Lab 5: Lazy Loading
Last updated
Last updated
In this lab, we simulate a slow system and we want to improve the user experience by giving them faster feedback.
We start by creating a lot of users in the initializeUsers
method in the Lab5Application
class with the library.
We also introduce a delay in the UserService.findAll
method by adding a Thread.sleep
to simulate a slow external system.
You will see that the request will take 3 seconds suggesting an unresponsive system to the user.
To improve the user experience we want to lazy load the slow user table after the initial page load with htmx.
We create a constant GET_USER_TABLE
and then add an HTTP GET endpoint in the UserController
where we render the userTableComponent
.
You should not be able to access it.
To render the user table we need to change the UserMangement back to a ViewComponent.
Now create a UserManagementComponent.jte
file in the same package as the UserManagementComponent
(1): The hx-get
attribute creates a GET
request to the GET_USER_TABLE
endpoint.
(2): The hx-trigger="load"
attribute triggers the request when the page loads.
We can show a loading spinner with an <img>
element.
(3): By adding the htmx-indicator
CSS class the element is shown while the HTTP request of the parent htmx element is in flight.
Now back in the UserController
we autowire the userMangementComponent
by injecting it into the constructor.
Then we replace the userTableComponent.render()
method with userManagementComponent.render() in
You can see that the page loads instantly and only the user table rendering is delayed.
Lab-5 Checkpoint 1
If you are stuck you can resume at this checkpoint with:
git checkout tags/lab-5-checkpoint-1 -b lab-5-c1
Now start the Lab5Application
and navigate to .
(1): We use the @HxRequest
annotation of the library.
The endpoint will only accept a request when it's made from htmx.
Now restart the Lab5Application
and try to navigate to .
Restart the Lab5Application
and navigate to .