Autoreloading your UI pages in the browser with each build, while developing web apps in Spring boot
I had been working on a simple gradle project where I use Spring boot to develop the backend and REST API using Spring data REST and Frontend using Vue.js + spring boot thymeleaf. Spring boot's controllers will handle the routing and all frontend work can be done in the html pages.
I will post these other pieces separately.
If you have done this, you may know that you can load your .html pages using Spring Boot's Thymeleaf rendering engine which comes configured by default. As long as you place them under src/main/resources/templates (default location for renderable templates)
But once that page is rendered you can use any frontend framework on it. In my case, I am using Vue.js as the frontend framework (more on this in other posts)
So if you have a spring boot project setup, you can install the spring boot devtools to your project. Once you do that, by default it will reload your spring boot application whenever your source code changes.
However, normally your html pages are going to be under src/main/resources/templates and these won't be automatically reloaded in the browser even if you refresh it. The reason is that those pages need to be built under build/ directory only then they will be re-served.
You can come up with a simple recipe such that when you save those pages, spring will reload your page in the browser!
Here is how
1. Setup spring-boot-devtools in your project by adding something like this to your dependencies
runtime("org.springframework.boot:spring-boot-devtools")
2. Now add this to src/main/resources/application.properties
spring.devtools.restart.additional-paths=src/main/resources/templates,src/main/resources/static
Just add as many paths as you like to be watched by the autorestarterThat's it now you will need to leave your application running and build your project after changing the static files (you would not need to even build if you changed java files of course)
At this time, you still have to refresh your browser to reload the changes.
To make this even cooler, install (in chrome) the LiveReload extension
Now it will automatically reload your page just leave the browser open, every time you hit 'build' or run 'gradlew build'!
This is now almost as good (but not quite) as using grunt watch in NodeJS projects.
Comments
Post a Comment