From a6ae7158312f8d33c358435473a622110430d56a Mon Sep 17 00:00:00 2001 From: rikhuijzer Date: Fri, 1 Oct 2021 14:50:03 +0200 Subject: [PATCH 1/2] Add trigger compilation flag to serve --- src/server.jl | 19 +++++++++++++++++++ test/server.jl | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/src/server.jl b/src/server.jl index ac16342..edffb22 100644 --- a/src/server.jl +++ b/src/server.jl @@ -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 reduces 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) @@ -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. @@ -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.")) @@ -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 diff --git a/test/server.jl b/test/server.jl index 921dc16..1a8c27c 100644 --- a/test/server.jl +++ b/test/server.jl @@ -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 From 2cef68ac06e772882e90e433b023bda018880aa5 Mon Sep 17 00:00:00 2001 From: rikhuijzer Date: Fri, 1 Oct 2021 14:57:56 +0200 Subject: [PATCH 2/2] Fix typo --- src/server.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.jl b/src/server.jl index edffb22..8b51cdb 100644 --- a/src/server.jl +++ b/src/server.jl @@ -246,7 +246,7 @@ end """ trigger_compilation(host, port) -Trigger compilation on the the server machinery. In many cases, this reduces the user +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.