Skip to content

Commit 977d71b

Browse files
committed
fix to tracking of custom css in servedocs
1 parent fbdd2b7 commit 977d71b

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

docs/src/assets/custom.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ nav.toc {
1616
nav.toc ul li ul li.current ul.internal li a.toctext:before {
1717
content:"• ";
1818
}
19+
20+
blockquote {
21+
background: #eee;
22+
border-left: 7px solid #a8a8a8;
23+
margin: 1.5em 10px;
24+
padding: 5px 10px;
25+
font-style: italic;
26+
border-radius: 10px;
27+
}

docs/src/index.md

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LiveServer is inspired from Python's [`http.server`](https://docs.python.org/3/l
77

88
## Installation
99

10-
In Julia ≥ 1.0, you can add it using the Package Manager writing
10+
In Julia ≥ 1.0, you can add it using the Package Manager with
1111

1212
```julia-repl
1313
pkg> add LiveServer
@@ -67,35 +67,31 @@ Let's assume you have a directory similar to that generated by [`LiveServer.exam
6767
└── blah.html
6868
```
6969
70-
Calling `serve()` from within this directory starts a file server. It serves
71-
the contents of the directory as a static site, with the folder structure
72-
defining the paths of the URLs. That is, the file `blah.html` can be viewed
73-
at `/pages/blah.html`. When a directory is specified instead of a file,
74-
the server checks whether there is a file `index.html` in this directory and
75-
serves it if available.
70+
Calling `serve()` from within this directory starts a file server.
71+
It serves the contents of the directory as a static site, with the folder structure defining the paths of the URLs.
72+
That is, the file `blah.html` can be viewed at `/pages/blah.html`.
73+
When a directory is specified instead of a file, the server checks whether there is a file `index.html` in this directory and serves it if available.
7674
7775
Visiting `http://localhost:8000/` makes your browser send a standard HTTP `GET`
78-
request to the server. The server, running a listener loop, receives this
79-
request, looks for `index.html` in the root
80-
folder, and serves it. After serving it, `LiveServer` adds this file to the
81-
list of watched files. That is, whenever this file changes, a callback is
82-
fired (see below). The HTML page may also contain references to style sheets or
83-
pictures, which are then requested by your browser as well. The server
84-
sends them to the browser, and also adds them to the list of watched
85-
files.
86-
87-
**But what about the live reloading?** Triggering a page refresh in the browser
88-
is achieved by a WebSocket connection. The WebSocket API, according to
89-
[MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API), is
76+
request to the server.
77+
The server, running a listener loop, receives this request, looks for `index.html` in the root folder, and serves it.
78+
After serving it, `LiveServer` adds this file to the list of watched files.
79+
That is, whenever this file changes, a callback is fired (see below).
80+
The HTML page may also contain references to style sheets or pictures, which are then requested by your browser as well.
81+
The server sends them to the browser, and also adds them to the list of watched files.
82+
83+
#### But what about the live reloading?
84+
85+
Triggering a page refresh in the browser is achieved by a WebSocket connection.
86+
The WebSocket API, according to [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API), is
87+
9088
> an advanced technology that makes it possible to open a two-way interactive
9189
> communication session between the user's browser and a server.
9290
`LiveServer` injects a small piece of JavaScript code into every HTML file
9391
before serving it. This snippet is executed by the browser and opens a WebSocket
9492
connection to the server, which in turn adds it to a list of viewers of this page.
9593
96-
The server can now send the message "update" to all viewers of a page
97-
whenever the page is changed. The code snippet reacts to this message by
98-
triggering a page reload (same as hitting `F5`). The update is triggered by the
99-
callback mentioned above. When the file is not an HTML file, the viewers of _any_
100-
HTML file are updated, since `LiveServer` currently does not keep track of which
101-
HTML files reference what other files.
94+
The server can now send the message "update" to all viewers of a page whenever the page is changed.
95+
The code snippet reacts to this message by triggering a page reload.
96+
The update is triggered by the callback mentioned above.
97+
When the file is not an HTML file, the viewers of _any_ HTML file are updated, since `LiveServer` currently does not keep track of which HTML files reference what other files.

src/server.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function serve_file(fw, req::HTTP.Request)
9999
# build the response with appropriate mime type (this is inspired from Mux
100100
# https://github.com/JuliaWeb/Mux.jl/blob/master/src/examples/files.jl)
101101
headers = ["Content-Type" => get(MIME_TYPES, ext, "application/octet-stream")]
102-
resp = Response(content)
102+
resp = HTTP.Response(content)
103103
isempty(headers) || (resp.headers = HTTP.mkheaders(headers))
104104

105105
# add the file to the file watcher
@@ -214,7 +214,8 @@ function serve(fw::FileWatcher=SimpleWatcher(file_changed_callback);
214214

215215
server = Sockets.listen(port)
216216
println("✓ LiveServer listening on http://localhost:$port/ ...\n (use CTRL+C to shut down)")
217-
@async HTTP.listen(Sockets.localhost, port, server=server, readtimeout=0) do http::HTTP.Stream
217+
@async HTTP.listen(Sockets.localhost, port;
218+
server=server, readtimeout=0, reuse_limit=0) do http::HTTP.Stream
218219
if HTTP.WebSockets.is_upgrade(http.message)
219220
# upgrade to websocket
220221
ws = ws_upgrade(http)

src/utils.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ function servedocs_callback!(dw::SimpleWatcher, fp::AbstractString, makejl::Abst
1717
empty!(dw.watchedfiles)
1818
scan_docs!(dw, literate)
1919
end
20-
if splitext(fp)[2] (".md", ".jl")
20+
fext = splitext(fp)[2]
21+
P1 = fext (".md", ".jl")
22+
# the second condition is for CSS files, we want to track it but not the output
23+
# if we track the output then there's an infinite loop being triggered (see docstring)
24+
if P1 || (fext == ".css" && !occursin(joinpath("docs", "build", "assets"), fp))
2125
Main.include(makejl)
2226
file_changed_callback(fp)
2327
end

0 commit comments

Comments
 (0)