Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force compilation flag to serve #128

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ function ws_tracker(ws::HTTP.WebSockets.WebSocket, target::AbstractString)
return nothing
end

"""
trigger_compilation(host, port)

Trigger compilation on the the server machinery. In many cases, this improves the user
experience because the compilation starts immediately once the server is running. Without
this, the compilation would not start until the user navigates to the server via the
browser.
"""
function trigger_compilation(host, port)
sleep(0.3)
url = "http://$host:$port"
HTTP.get(url; status_exception=false)
end

"""
serve(filewatcher; host="127.0.0.1", port=8000, dir="", verbose=false, coreloopfun=(c,fw)->nothing, inject_browser_reload_script::Bool = true, launch_browser::Bool = false, allow_cors::Bool = false)
Expand All @@ -258,6 +271,7 @@ directory. (See also [`example`](@ref) for an example folder).
- `verbose` is a boolean switch to make the server print information about file changes and connections.
- `coreloopfun` specifies a function which can be run every 0.1 second while the liveserver is going; it takes two arguments: the cycle counter and the filewatcher. By default the coreloop does nothing.
- `launch_browser=false` specifies whether to launch the ambient browser at the localhost URL or not.
- `force_compilation=true` specifies whether to trigger compilation as soon as the server is running. For users who don't open the browser immediately, this can speed up the time between calling `serve` and seeing the webpage.
- `allow_cors::Bool=false` will allow cross origin (CORS) requests to access the server via the "Access-Control-Allow-Origin" header.
- `preprocess_request=identity`: specifies a function which can transform a request before a response is returned; its only argument is the current request.

Expand All @@ -277,6 +291,7 @@ function serve(fw::FileWatcher=SimpleWatcher(file_changed_callback);
preprocess_request=identity,
inject_browser_reload_script::Bool = true,
launch_browser::Bool = false,
force_compilation::Bool = true,
allow_cors::Bool = false)

8000 ≤ port ≤ 9000 || throw(ArgumentError("The port must be between 8000 and 9000."))
Expand Down Expand Up @@ -312,6 +327,10 @@ function serve(fw::FileWatcher=SimpleWatcher(file_changed_callback);
end
end

if force_compilation
@async trigger_compilation(host, port)
end

launch_browser && open_in_default_browser(url)
# wait until user interrupts the LiveServer (using CTRL+C).
try
Expand Down
7 changes: 7 additions & 0 deletions test/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ tasks that you will try to start.
#
# STEP 2: triggering a request
#
url = "http://localhost:$port/"

# Using allocations to test `trigger_compilation` because it is more robust than runtime.
sleep(8)
allocations = @allocated HTTP.get(url)
@test allocations < 100_000_000

response = HTTP.get("http://localhost:$port/")
@test response.status == 200
# the browser script should be appended
Expand Down