Skip to content

Commit ba2152c

Browse files
committed
A better way to inject livereload js.
close #182
1 parent 390187c commit ba2152c

2 files changed

Lines changed: 27 additions & 20 deletions

File tree

livereload/server.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ def start_response(status, response_headers, exc_info=None):
132132

133133
if status_code != 304:
134134
if "content-type" not in header_set:
135-
headers.append(("Content-Type", "application/octet-stream; charset=UTF-8"))
135+
headers.append((
136+
"Content-Type",
137+
"application/octet-stream; charset=UTF-8"
138+
))
136139
if "content-length" not in header_set:
137140
headers.append(("Content-Length", str(len(body))))
138141

@@ -207,11 +210,10 @@ def alert():
207210

208211
self.watcher.watch(filepath, func, delay, ignore=ignore)
209212

210-
def application(self, port, host, liveport=None, debug=None, live_css=True):
213+
def application(self, port, host, liveport=None, debug=None,
214+
live_css=True):
211215
LiveReloadHandler.watcher = self.watcher
212216
LiveReloadHandler.live_css = live_css
213-
if liveport is None:
214-
liveport = port
215217
if debug is None and self.app:
216218
debug = True
217219

@@ -224,24 +226,26 @@ def application(self, port, host, liveport=None, debug=None, live_css=True):
224226
# The livereload.js snippet.
225227
# Uses JavaScript to dynamically inject the client's hostname.
226228
# This allows for serving on 0.0.0.0.
227-
live_reload_path = ":{port}/livereload.js?port={port}".format(port=liveport)
228-
if liveport == 80 or liveport == 443:
229-
live_reload_path = "/livereload.js?port={port}".format(port=liveport)
230-
231-
live_script = escape.utf8((
232-
'<script type="text/javascript">'
233-
'document.write("<script src=''//"'
234-
' + window.location.hostname + "{path}''>'
235-
' </"+"script>");'
236-
'</script>'
237-
).format(path=live_reload_path))
229+
live_script = (
230+
'<script type="text/javascript">(function(){'
231+
'var s=document.createElement("script");'
232+
'var port=%s;'
233+
's.src="//"+window.location.hostname+":"+port'
234+
'+ "/livereload.js?port=" + port;'
235+
'document.head.appendChild(s);'
236+
'})();</script>'
237+
)
238+
if liveport:
239+
live_script = escape.utf8(live_script % liveport)
240+
else:
241+
live_script = escape.utf8(live_script % 'window.location.port')
238242

239243
web_handlers = self.get_web_handlers(live_script)
240244

241245
class ConfiguredTransform(LiveScriptInjector):
242246
script = live_script
243247

244-
if liveport == port:
248+
if not liveport:
245249
handlers = live_handlers + web_handlers
246250
app = web.Application(
247251
handlers=handlers,
@@ -271,7 +275,8 @@ def get_web_handlers(self, script):
271275
]
272276

273277
def serve(self, port=5500, liveport=None, host=None, root=None, debug=None,
274-
open_url=False, restart_delay=2, open_url_delay=None, live_css=True):
278+
open_url=False, restart_delay=2, open_url_delay=None,
279+
live_css=True):
275280
"""Start serve the server with the given port.
276281
277282
:param port: serve on this port, default is 5500
@@ -282,7 +287,8 @@ def serve(self, port=5500, liveport=None, host=None, root=None, debug=None,
282287
via Tornado (and causes polling). Defaults to True when
283288
``self.app`` is set, otherwise False.
284289
:param open_url_delay: open webbrowser after the delay seconds
285-
:param live_css: whether to use live css or force reload on css. Defaults to True
290+
:param live_css: whether to use live css or force reload on css.
291+
Defaults to True
286292
"""
287293
host = host or '127.0.0.1'
288294
if root is not None:
@@ -291,7 +297,8 @@ def serve(self, port=5500, liveport=None, host=None, root=None, debug=None,
291297
self._setup_logging()
292298
logger.info('Serving on http://%s:%s' % (host, port))
293299

294-
self.application(port, host, liveport=liveport, debug=debug, live_css=live_css)
300+
self.application(
301+
port, host, liveport=liveport, debug=debug, live_css=live_css)
295302

296303
# Async open web browser after 5 sec timeout
297304
if open_url or open_url_delay:

server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
server = Server()
66
server.watch('docs/*.rst', shell('make html'))
7-
server.serve(root='docs/_build/html', open_url_delay=5)
7+
server.serve(root='docs/_build/html')

0 commit comments

Comments
 (0)