diff --git a/mkdocs/livereload/__init__.py b/mkdocs/livereload/__init__.py index 55554a4025..9feb27f715 100644 --- a/mkdocs/livereload/__init__.py +++ b/mkdocs/livereload/__init__.py @@ -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'); @@ -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")]) @@ -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. @@ -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%b" % ( content[:body_end], script.encode(), diff --git a/mkdocs/tests/livereload_tests.py b/mkdocs/tests/livereload_tests.py index 62483b5bfd..ecbbaecca0 100644 --- a/mkdocs/tests/livereload_tests.py +++ b/mkdocs/tests/livereload_tests.py @@ -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()