Skip to content

Commit

Permalink
Revert to disregarding the mount path for /livereload/ endpoint (#2804)
Browse files Browse the repository at this point in the history
Previously there was a pull request "Move livereload endpoint under the mount path (#2740)"
and it definitely makes sense as a way to make livereload work behind a proxy.

However, this change also caused a major annoyance when working on several different sites.
Steps to reproduce:

1. `serve` one site (needs to have `site_url` customized)
2. open the site in a browser
3. stop serving it but keep the browser
4. `serve` another site with a different mount point
5. observe spam of "404" errors in the new terminal (also in the old browser but that's whatever)

So, this reverts the observable effect of commit fafdcc2
  • Loading branch information
oprypin committed Mar 26, 2022
1 parent 17ff566 commit b727215
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions mkdocs/livereload/__init__.py
Expand Up @@ -32,7 +32,7 @@
setTimeout(launchNext, 3000);
}
};
req.open("GET", "${mount_path}livereload/" + epoch + "/" + requestId);
req.open("GET", "/livereload/" + epoch + "/" + requestId);
req.send();
console.log('Enabled live reload');
Expand Down Expand Up @@ -201,10 +201,8 @@ def _serve_request(self, environ, start_response):
# https://github.com/bottlepy/bottle/blob/f9b1849db4/bottle.py#L984
path = environ["PATH_INFO"].encode("latin-1").decode("utf-8", "ignore")

if path.startswith(self.mount_path):
rel_file_path = path[len(self.mount_path):]

m = re.fullmatch(r"livereload/([0-9]+)/[0-9]+", rel_file_path)
if path.startswith("/livereload/"):
m = re.fullmatch(r"/livereload/([0-9]+)/[0-9]+", path)
if m:
epoch = int(m[1])
start_response("200 OK", [("Content-Type", "text/plain")])
Expand All @@ -220,6 +218,9 @@ def condition():
self._epoch_cond.wait_for(condition, timeout=self.poll_response_timeout)
return [b"%d" % self._visible_epoch]

if path.startswith(self.mount_path):
rel_file_path = path[len(self.mount_path):]

if path.endswith("/"):
rel_file_path += "index.html"
# Prevent directory traversal - normalize the path.
Expand Down Expand Up @@ -266,9 +267,7 @@ def _inject_js_into_html(self, content, epoch):
body_end = len(content)
# The page will reload if the livereload poller returns a newer epoch than what it knows.
# The other timestamp becomes just a unique identifier for the initiating page.
script = _SCRIPT_TEMPLATE.substitute(
mount_path=self.mount_path, epoch=epoch, request_id=_timestamp()
)
script = _SCRIPT_TEMPLATE.substitute(epoch=epoch, request_id=_timestamp())
return b"%b<script>%b</script>%b" % (
content[:body_end],
script.encode(),
Expand Down
4 changes: 2 additions & 2 deletions mkdocs/tests/livereload_tests.py
Expand Up @@ -343,9 +343,9 @@ def test_serves_polling_instantly(self, site_dir):
self.assertTrue(output.isdigit())

@tempdir()
def test_serves_polling_from_mount_path(self, site_dir):
def test_serves_polling_with_mount_path(self, site_dir):
with testing_server(site_dir, mount_path="/test/f*o") as server:
_, output = do_request(server, "GET /test/f*o/livereload/0/0")
_, output = do_request(server, "GET /livereload/0/0")
self.assertTrue(output.isdigit())

@tempdir()
Expand Down

0 comments on commit b727215

Please sign in to comment.