Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live reload for thymeleaf template

When I change a thymeleaf .html file residing inside /templates , I expect the browser to automatically reload the page. I have the live reload plugin installed and its able to do a handshake with the spring boot server. However , upon chaning a thymeleaf template file , I have to manually reload the browser. Any suggestions what I might be missing. Obviously I have spring-boot-devtools enabled and have also manually updated the property devtools.livereload.enabled = true. And spring devtools is correctly relecting changes to any template or controller in build target , and with a manual reload of browser , I see the changes.

As per spring docs.

Certain resources do not necessarily need to trigger a restart when they are changed. For example, Thymeleaf templates can be edited in-place. By default, changing resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates does not trigger a restart but does trigger a live reload.

I have my local running over a broken https. ( Some certificate issue , which causes a not secure message in chrome address bar. Could this be the reason live reload is not working.

like image 304
Rpant Avatar asked Oct 07 '19 18:10

Rpant


1 Answers

After 3 years of frustation here is a solution for a working HOT SWAP:

https://attacomsian.com/blog/spring-boot-auto-reload-thymeleaf-templates

Below are my tested settings (this is the only thing I've changed for it to work).

application.yaml:

spring:
  thymeleaf: # Thymeleaf
    cache: false
    mode: HTML
    encoding: UTF-8
    prefix: file:src/main/resources/templates/
  resources: # Static resources
    static-locations: file:src/main/resources/static/
    cache:
      period: 0

What it does:

  • Applies changes to your views (and/or static content) if you've altered anything in static/ or templates/
  • It does so without the need to manually compile or reboot the entire web server

What it does not:

  • Reload the page for you in the browser (you still have to refresh the page in the browser; btw that LiveReload Chrome extension won't work either, maybe with some webpack magic you can make it work)

This is all assuming you didn't override the default paths (/resources/templates, /resources/static).

P.S. I've tried with a self-signed TLS cert, it still works. Yes it won't behave like Angular with browser page reloading out of the box.

like image 51
downvoteit Avatar answered Sep 21 '22 16:09

downvoteit