Skip to content
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

Reloading with folder that is deleted and re-created #44

Closed
mgunyho opened this issue Dec 18, 2023 · 2 comments
Closed

Reloading with folder that is deleted and re-created #44

mgunyho opened this issue Dec 18, 2023 · 2 comments

Comments

@mgunyho
Copy link

mgunyho commented Dec 18, 2023

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>
"""
)
@lomirus lomirus mentioned this issue Dec 18, 2023
@lomirus
Copy link
Owner

lomirus commented Dec 18, 2023

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.

@lomirus lomirus closed this as completed Dec 18, 2023
@mgunyho
Copy link
Author

mgunyho commented Dec 18, 2023

Cool, thanks! I was suspecting that the first case is more tricky, but I can manage with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants