Don't inject javascript poll script if livereload is disabled (#2742)

This commit is contained in:
Oleh Prypin
2022-01-06 17:31:18 +02:00
committed by GitHub
parent fafdcc240e
commit d4565cbed3
2 changed files with 11 additions and 9 deletions

View File

@@ -244,7 +244,7 @@ class LiveReloadServer(socketserver.ThreadingMixIn, wsgiref.simple_server.WSGISe
return []
return None # Not found
if file_path.endswith(".html"):
if self._watched_paths and file_path.endswith(".html"):
with file:
content = file.read()
content = self._inject_js_into_html(content, epoch)

View File

@@ -277,6 +277,8 @@ class BuildTests(unittest.TestCase):
)
def test_serves_modified_html(self, site_dir):
with testing_server(site_dir) as server:
server.watch(site_dir)
headers, output = do_request(server, "GET /normal.html")
self.assertRegex(output, fr"^<html><body>hello{SCRIPT_REGEX}</body></html>$")
self.assertEqual(headers.get("content-type"), "text/html")
@@ -296,14 +298,14 @@ class BuildTests(unittest.TestCase):
def test_serves_directory_index(self, site_dir):
with testing_server(site_dir) as server:
headers, output = do_request(server, "GET /")
self.assertRegex(output, fr"^<body>aaa{SCRIPT_REGEX}</body>$")
self.assertRegex(output, r"^<body>aaa</body>$")
self.assertEqual(headers["_status"], "200 OK")
self.assertEqual(headers.get("content-type"), "text/html")
self.assertEqual(headers.get("content-length"), str(len(output)))
for path in "/foo/", "/foo/index.html":
_, output = do_request(server, "GET /foo/")
self.assertRegex(output, fr"^<body>bbb{SCRIPT_REGEX}</body>$")
self.assertRegex(output, r"^<body>bbb</body>$")
with self.assertLogs("mkdocs.livereload"):
headers, _ = do_request(server, "GET /foo/index.html/")
@@ -321,18 +323,18 @@ class BuildTests(unittest.TestCase):
def test_serves_with_unicode_characters(self, site_dir):
with testing_server(site_dir) as server:
_, output = do_request(server, "GET /я.html")
self.assertRegex(output, fr"^<body>aaa{SCRIPT_REGEX}</body>$")
self.assertRegex(output, r"^<body>aaa</body>$")
_, output = do_request(server, "GET /%D1%8F.html")
self.assertRegex(output, fr"^<body>aaa{SCRIPT_REGEX}</body>$")
self.assertRegex(output, r"^<body>aaa</body>$")
with self.assertLogs("mkdocs.livereload"):
headers, _ = do_request(server, "GET /%D1.html")
self.assertEqual(headers["_status"], "404 Not Found")
_, output = do_request(server, "GET /测试2/")
self.assertRegex(output, fr"^<body>bbb{SCRIPT_REGEX}</body>$")
self.assertRegex(output, r"^<body>bbb</body>$")
_, output = do_request(server, "GET /%E6%B5%8B%E8%AF%952/index.html")
self.assertRegex(output, fr"^<body>bbb{SCRIPT_REGEX}</body>$")
self.assertRegex(output, r"^<body>bbb</body>$")
@tempdir()
def test_serves_polling_instantly(self, site_dir):
@@ -431,11 +433,11 @@ class BuildTests(unittest.TestCase):
def test_serves_from_mount_path(self, site_dir):
with testing_server(site_dir, mount_path="/sub") as server:
headers, output = do_request(server, "GET /sub/")
self.assertRegex(output, fr"^<body>aaa{SCRIPT_REGEX}</body>$")
self.assertRegex(output, r"^<body>aaa</body>$")
self.assertEqual(headers.get("content-type"), "text/html")
_, output = do_request(server, "GET /sub/sub/sub.html")
self.assertRegex(output, fr"^<body>bbb{SCRIPT_REGEX}</body>$")
self.assertRegex(output, r"^<body>bbb</body>$")
with self.assertLogs("mkdocs.livereload"):
headers, _ = do_request(server, "GET /sub/sub.html")