New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Thymeleaf templates cache even when spring.template.cache: false #34
Comments
You can use this to disable the Thymeleaf cache:
|
This ticket is noting that even when using that property, they are being cached. |
|
I guess if you |
It works! I user IntelliJ Idea debug the application. After add
Then,after edit html template, must use CTRL+F9 to make the project. In this way,it works fine for me. |
I run the application from main() in Idea IDE. I set the application.properties as stated above. If I'm using jsp then I can expect changes to the jsp files to be visible immediately without recompiling. I think the issue here is that there is an expectation that the thymeleaf templates will have the same behaviour. This does not appear to be the case. Is it possible to have changes to the th templates visible any time they are changed, without any recompiling or rebuilding of the project? The point is that front-end developers need to make tons of changes to templates and view the results often. Having to recompile every time is a ton of overhead for a front-end developer so they want to avoid this. |
Well it works for other people so you must be doing something different, I'd say. Did you read everything above? E.g. you seem to be using the wrong property name for the cache setting. |
Hi Dave, I have done the following: I read all the posts. I put the system in debug (running from main()) and verified that the TemplateResolver/ViewResolver had caching set to false. I used both properties mentioned above, both set to false. I tried changing the template and opened the page in two separate browsers to see if it was the browser that was caching the older version but this is not the case. I checked using curl as well. I think what it comes down to is how you define: "it works" Above, yanhua365 says "Then,after edit html template, must use CTRL+F9 to make the project" For me, "it works" means: change the template => refresh browser => changes are visible. So yanhua365 is correct, it does work in the way he describes. However, as far as I can determine, it does not work in the way I am describing. I'm just trying to point out that the intermediate step of having to rebuild is a problem as it adds a boatload of overhead to the development flow for front-end developers. Front-end developers are constantly making changes and then refreshing the page to check the result. (to the point of developing repetitive stress injuries) Adding an intermediate step is a problem from that perspective. I don't know if it possible for it to work in the way I'm describing, but if it is it would be a great improvement. |
"CTRL+F9" must be something you can automate (build on save or something). Other IntelliJ users manage it, so it must be possible (cc @snicoll in case he knows what the incantation is). |
https://www.jetbrains.com/idea/documentation/migration_faq.html
@MattFriedman if you want to discuss this further, please create a separate issue. |
For the ones that are struggling with this too. |
@MattFriedman The default template resolver registered by spring is classpath based, meaning that it loads the templates from the compiled resources. That's why it requires a recompilation. Thymeleaf includes a file-system based resolver, this loads the templates from the file-system directly not through the classpath (compiled resources). Spring Boot allows us to override the default resolver by making a bean with the name @Configuration
public class ThymeleafConfiguration {
@Bean
public ITemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new FileTemplateResolver();
resolver.setSuffix(".html");
resolver.setPrefix("path/to/your/templates");
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(false);
return resolver;
}
} With this, I basically Ctrl+S and refresh the browser. A better variation would be one that uses @Configuration
public class ThymeleafConfiguration {
@Inject
private ThymeleafProperties properties;
@Value("${spring.thymeleaf.templates_root:}")
private String templatesRoot;
@Bean
public ITemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new FileTemplateResolver();
resolver.setSuffix(properties.getSuffix());
resolver.setPrefix(templatesRoot);
resolver.setTemplateMode(properties.getMode());
resolver.setCharacterEncoding(properties.getEncoding());
resolver.setCacheable(properties.isCache());
return resolver;
}
} This would work with an spring:
thymeleaf:
mode: HTML5
cache: false
templates_root: src/main/resources/templates/ In my application, I annotate that resolver with |
You should be able to just use the default classpath loader as long as you are running in debug mode (and your IDE is configured to copy resources on save - Eclipse does this out of the box). |
For other folks that might run into this issue. I had the same problem until I realized that IntelliJ's key mapping for Ctrl-F9 was not actually doing what it was supposed to (Make Project). You can verify this in a couple of ways:
The fix for my particular case was to remap the keyboard shortcut for "Make Project". After I did this I was able to make changes to my templates, use my new keyboard shortcut for "Make Project", then reload the browser to see the changes. |
@amr : Thank you. |
In my application-development.yml:
This way in development it use the direct files, and in production the classpath |
@giovannicandido Thanks for the tip. |
@henryabra |
@giovannicandido You are absolutely correct. |
Springboot with IntelliJ and configurations in src/main/resources/application.properties I put in the following relative path, and started the Application.main(...) from IntelliJ:
|
@oak1980 You are using a debug configuration and hitting CTRL-F9? (See #34 (comment)) What versions of SpringBoot are you using if you believe it is a regression? |
With this approach, I don't need to press CTRL-F9. |
Ah so you are reporting an alternative approach that works for you and avoids the need to rebuild in IntelliJ. It is good to call out that you wouldn't want to check it |
@oak1980 you are awesome! |
If someone else drop's by this long-closed issue, the parameter should be(as of Spring Boot 2.0.5 release) |
I recommend using Gulp to automate templates and resources reload. Following is a little Gulp task that does the magic for me: var gulp = require('gulp'),
watch = require('gulp-watch');
gulp.task('watch', function () {
return watch('src/main/resources/**/*.*', () => {
gulp.src('src/main/resources/**')
//replace with build/resources/main/ for netBeans
.pipe(gulp.dest('out/production/resources/'));
});
});
gulp.task('default', ['watch']); I wrote a short blog post on this topic as well. |
A working solution is to override the defaultTemplateResolver by using a file system based resolver: application.properties
Application class @SpringBootApplication
public class MyApplication {
@Autowired
private ThymeleafProperties properties;
@Value("${spring.thymeleaf.templates_root:}")
private String templatesRoot;
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public ITemplateResolver defaultTemplateResolver() {
FileTemplateResolver resolver = new FileTemplateResolver();
resolver.setSuffix(properties.getSuffix());
resolver.setPrefix(templatesRoot);
resolver.setTemplateMode(properties.getMode());
resolver.setCacheable(properties.isCache());
return resolver;
}
} |
You saved my day buddy! |
The latest configuration names seem to be as follows, based off Spring Boot 3.1.0: Unchanged:
Changed:
Or in YAML (
|
Thank you @dtonhofer, still working in 2024~ 👍 |
When editing Thymeleaf templates, a server restart is required to view the updates.
Even with the following setting in
application.properties
:To reproduce:
With
spring-boot-sample-web-ui
, run it withmvn spring-boot:run
and edit any of the templates, they don't reload after server has been started.The text was updated successfully, but these errors were encountered: