Skip to content

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

@mgunyho

Description

@mgunyho

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>
"""
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions