Spring live reload and webpack out of sync

已回答

I've been working on a new Spring Boot MVC project which uses Freemarker, Tailwind CSS, and Alpine.js + some custom JavaScript for the front end (webpack is used for the JS). I have live reload and devtools and it does it's job except for one problem. When I edit a template or make a change to JavaScript files and save them it kicks off the reload and the problem is my new JS or CSS hasn't been generated yet (it's a second or two later). So what ends up happening is I have to save my changes, wait a couple seconds, and then hit the build button so it reloads again. This is more problematic with JavaScript since I don't know for sure if I'm using the latest built script or not.

I've been searching a lot on this but haven't found a solution that fixes my problem. Reload happens at the time I tell it to save (CTRL+S). It doesn't reload again when the JS or CSS is built a second or two later (and written into the static folder).

Things I've tried:

1. Excluding the templates folder from "spring.devtools.restart.additional-exclude". Since Tailwind re-generates the CSS when saving them I thought it might eliminate the reload from the template itself being saved and then detect a new CSS and then reload. Somehow it's still seems to be getting triggered.
2. Playing with the timing of "spring.devtools.restart.poll-interval" and "spring.devtools.restart.quiet-period".
3. I moved my JS that I edit to a new folder of the root of my project to avoid possibly triggering a reload on save. Same reason as #1.

My project run configuration is set to on update update classes and resources.

Anyone have any ideas on this? Reluctantly I'm considering starting the project over using .NET.

评论操作 固定链接

Where the file that you change is located? And how the files are generated?

>It doesn't reload again when the JS or CSS is built a second or two later (and written into the static folder).

Can you configure the static files generation to copy them directly into the build output? then, once the file is copied - it should be licked up.

0
评论操作 固定链接

One example is Tailwind. Tailwind's config is set to watch the .ftl files in the resources/templates folder and then save the generated css file to the resources/static folder. What happens is I save the template and I see (on my 2nd screen) the browser reload but the style changes aren't there. So I hit build in IntelliJ and I see the browser reload again and it has the styles. I've tried just waiting but nothing happens so I have to click build.

Can you configure the static files generation to copy them directly into the build output? then, once the file is copied - it should be licked up.

Do you mean have a process that copies from the static folder into the target/classes/static folder? I wasn't sure if that would cause an issue (like anything that might be tracking changes and managing it) so I hadn't tried it. I'll give it a shot though and see if that resolves the issue.

0
评论操作 固定链接

I created a bash file and a file watcher to run it and it does seem to have solved the problem.

Edit: Still not exactly working great. IntelliJ apparently only triggers the watcher when switching away and switch back so I changed to using inotifywait and rsync to watch for changes and just run my shell script. But still some timing issues with live reload. It reloads once, then again after the files are copied. But most of the time I don't see changes until I manually reload the browser. Even tried adding sleep but it didn't help. I feel like I'm using Spring in a way that's abnormal. I guess this isn't an IntelliJ issue. I thought it might have something to do with it but  apparently not.

0
评论操作 固定链接

Thank you for the information.

>Still not exactly working great. IntelliJ apparently only triggers the watcher when switching away and switch back 

There are, actually related requests for enhancing such workflows:

IDEA-168617 "Background" changes by external tool not being picked up until VFS refresh happens

WEB-45482 Make Live Edit track External changes

Please feel free to vote and track them for updates.

0
评论操作 固定链接

I looked at the problem again today using the 2022.2 EAP. First thing I did though was try VSCode. With VSCode everything works as I expect it to and the page reloads very quickly. Neither running as "Spring Boot" application or maven in IntelliJ work right (both ways refresh in 2-3 seconds but the style changes aren't there).

I did notice that if I leave VSCode open with the project open (not running anything) and I make changes in IntelliJ then the changes are there after the page reloads, but the page reloads 2 times, sometimes 3. I closed and re-opened VSCode a couple times to see if I was crazy.

I just wasn't sure if there was something I was missing with IntelliJ settings or spring boot settings that may be the problem or a solution to the problem. I'll check out those issues you linked and vote on them. From the titles it does sound related.

0
评论操作 固定链接

I'm pretty persistent and this keeps bugging me. I found something that works, for anyone with a similar problem. Adding the following to my application.properties file for development:

spring.freemarker.template-loader-path=file:src/main/resources/templates/
spring.web.resources.static-locations=file:src/main/resources/static/

It seems to be working reliably except browser reloading doesn't work well (slow and most of the time doesn't refresh at all). Manual refreshing the page works every time though.

0

请先登录再写评论。