Reloading with folder that is deleted and re-created
Closed this issue · 2 comments
Hi! Thanks for this tool.
I was trying to use it to live-reload my static site which is generated by a Python script. To keep things simple, my script just deletes the entire build folder and then fills it with the generated HTML. I ran into two problems:
- Originally, my script just deletes the
_build
folder and then recreates it. However, then the live-reload doesn't work, presumably because the watcher is still watching the old deleted folder, and doesn't track the newly created folder. - I changed my script to just delete all of the contents of
_build
but not the folder itself, and then fill it with content. However, if (and when) live-server tries to reload the page I'm currently on after it has been deleted, but before it has been generated, it reloads to a blank page, and can't auto-reload to the generated page after that.
Would it be possible to make live-reload such that it works with the deleted and re-created folder, and/or so that it doesn't try to reload if the currently open html file does not exist?
For reference, here's a quick script that works like the first version:
import shutil
import os
shutil.rmtree("_build", ignore_errors=True)
os.mkdir("_build")
with open("_build/index.html", "w") as f:
f.write(
"""
<!DOCTYPE html>
<html><body>
<h1>Hello world<h1>
</body></html>
"""
)
And here's one that works like the second version:
import shutil
import os
import time
from pathlib import Path
Path("_build").mkdir(exist_ok=True)
for path in Path("_build").glob("**/*"):
if path.is_file():
path.unlink()
elif path.is_dir():
shutil.rmtree(path)
time.sleep(1.0) # simulate delay of generating HTML
with open("_build/index.html", "w") as f:
f.write(
"""
<!DOCTYPE html>
<html><body>
<h1>Hello world<h1>
</body></html>
"""
)
Hi, @mgunyho, I just fixed the second problem in #45. But as for the first problem, it's relative to the upstream crate notify:
On some platforms, if the path is renamed or removed while being watched, behaviour may be unexpected. See discussions in #165 and #166. If less surprising behaviour is wanted one may non-recursively watch the parent directory as well and manage related events.
So you can just delete all of the contents of _build
but not the folder itself, like what you just mentioned.
Cool, thanks! I was suspecting that the first case is more tricky, but I can manage with this.