Diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Load the scripts with defer
A plain script in the head held up parsing until it had downloaded and run, though cgit.js only acts once the document is ready. Deferred scripts still run in the order given and before DOMContentLoaded, so a site script after cgit.js can still call window.cgit.
-rw-r--r--MANUAL.txt2
-rw-r--r--custom/cgitrc5
-rw-r--r--source/ui-shared.c2
-rwxr-xr-xtests/t0005-markup.sh5
4 files changed, 11 insertions, 3 deletions
diff --git a/MANUAL.txt b/MANUAL.txt
index 59e4b04..48d5561 100644
--- a/MANUAL.txt
+++ b/MANUAL.txt
@@ -315,6 +315,8 @@ js::
Url which specifies the javascript script document to include in all
cgit pages. Default value: "/cgit.js". May be given multiple times, each
script URL path is added in the head section of the document in turn.
+ Each is loaded with defer, so it runs once the page has been parsed,
+ still in the order given.
Setting this to an empty string will disable generation of the link to
this file in the head section.
diff --git a/custom/cgitrc b/custom/cgitrc
index f816f56..569feb4 100644
--- a/custom/cgitrc
+++ b/custom/cgitrc
@@ -85,8 +85,9 @@ root-desc=a fast webinterface for the git dscm
# Value is one or more urls. Default is /cgit.css.
css=/cgit.css
-# JavaScript document urls included on every page. An empty value disables it.
-# Value is one or more urls. Default is /cgit.js.
+# JavaScript document urls included on every page, loaded with defer in the
+# order given. An empty value disables it. Value is one or more urls. Default
+# is /cgit.js.
js=/cgit.js
# Shortcut icon url for all pages. Value is a url. Default is /favicon.ico.
diff --git a/source/ui-shared.c b/source/ui-shared.c
index ff691f8..e7b7d88 100644
--- a/source/ui-shared.c
+++ b/source/ui-shared.c
@@ -379,7 +379,7 @@ static void emit_js_link(const char *src)
html("<script src='");
html_attr(src);
- html("'></script>\n");
+ html("' defer></script>\n");
}
static void add_clone_urls(void (*fn)(const char *), char *urls, char *suffix)
diff --git a/tests/t0005-markup.sh b/tests/t0005-markup.sh
index e2bf8f3..22ac81c 100755
--- a/tests/t0005-markup.sh
+++ b/tests/t0005-markup.sh
@@ -120,6 +120,11 @@ test_expect_success 'the page background is the same everywhere it is set' '
test_cmp expect actual
'
+test_expect_success 'the script waits for the page instead of holding it up' '
+ hooks "hooks/" >tmp &&
+ grep "<script src=./cgit.js. defer></script>" tmp
+'
+
# The rest of the hooks need a tag, a symlink, a submodule, a trailer, a lone
# carriage return, clone urls, an empty repository and a second page of
# repositories, so they get a commit and a config of their own after the checks