Skip to content

Commit

Permalink
perf(assets): reduce HTTP requests to CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed May 12, 2023
1 parent 016a9ba commit 9d97120
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 135 deletions.
70 changes: 39 additions & 31 deletions _includes/comments/giscus.html
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
<!-- https://giscus.app/ -->
<script type="text/javascript">
$(function () {
const origin = "https://giscus.app";
const iframe = "iframe.giscus-frame";
const lightTheme = "light";
const darkTheme = "dark_dimmed";
(function () {
const origin = 'https://giscus.app';
const iframe = 'iframe.giscus-frame';
const lightTheme = 'light';
const darkTheme = 'dark_dimmed';

let initTheme = lightTheme;
const html = document.documentElement;

if ($("html[data-mode=dark]").length > 0
|| ($("html[data-mode]").length == 0
&& window.matchMedia("(prefers-color-scheme: dark)").matches)) {
if (
(html.hasAttribute('data-mode') &&
html.getAttribute('data-mode') === 'dark') ||
(!html.hasAttribute('data-mode') &&
window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
initTheme = darkTheme;
}

let giscusAttributes = {
"src": "https://giscus.app/client.js",
"data-repo": "{{ site.comments.giscus.repo}}",
"data-repo-id": "{{ site.comments.giscus.repo_id }}",
"data-category": "{{ site.comments.giscus.category }}",
"data-category-id": "{{ site.comments.giscus.category_id }}",
"data-mapping": "{{ site.comments.giscus.mapping | default: 'pathname' }}",
"data-reactions-enabled": "{{ site.comments.giscus.reactions_enabled | default: '1' }}",
"data-emit-metadata": "0",
"data-theme": initTheme,
"data-input-position": "{{ site.comments.giscus.input_position | default: 'bottom' }}",
"data-lang": "{{ site.comments.giscus.lang | default: lang }}",
"crossorigin": "anonymous",
"async": ""
src: 'https://giscus.app/client.js',
'data-repo': '{{ site.comments.giscus.repo}}',
'data-repo-id': '{{ site.comments.giscus.repo_id }}',
'data-category': '{{ site.comments.giscus.category }}',
'data-category-id': '{{ site.comments.giscus.category_id }}',
'data-mapping': '{{ site.comments.giscus.mapping | default: 'pathname' }}',
'data-reactions-enabled': '{{ site.comments.giscus.reactions_enabled | default: '1' }}',
'data-emit-metadata': '0',
'data-theme': initTheme,
'data-input-position': '{{ site.comments.giscus.input_position | default: 'bottom' }}',
'data-lang': '{{ site.comments.giscus.lang | default: lang }}',
crossorigin: 'anonymous',
async: ''
};

let giscusScript = document.createElement("script");
Object.entries(giscusAttributes).forEach(([key, value]) => giscusScript.setAttribute(key, value));
document.getElementById("tail-wrapper").appendChild(giscusScript);
let giscusScript = document.createElement('script');
Object.entries(giscusAttributes).forEach(([key, value]) =>
giscusScript.setAttribute(key, value)
);
document.getElementById('tail-wrapper').appendChild(giscusScript);

addEventListener("message", (event) => {
if (event.source === window && event.data &&
event.data.direction === ModeToggle.ID) {
addEventListener('message', (event) => {
if (
event.source === window &&
event.data &&
event.data.direction === ModeToggle.ID
) {
/* global theme mode changed */
const mode = event.data.message;
const theme = (mode === ModeToggle.DARK_MODE ? darkTheme : lightTheme);
const theme = mode === ModeToggle.DARK_MODE ? darkTheme : lightTheme;

const message = {
setConfig: {
Expand All @@ -49,8 +59,6 @@
const giscus = document.querySelector(iframe).contentWindow;
giscus.postMessage({ giscus: message }, origin);
}

});

});
})();
</script>
2 changes: 0 additions & 2 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@

<!-- JavaScript -->

<script src="{{ site.data.assets[origin].jquery.js | relative_url }}"></script>

{% unless site.theme_mode %}
{% include mode-toggle.html %}
{% endunless %}
Expand Down
36 changes: 25 additions & 11 deletions _includes/js-selector.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
<!-- JS selector for site. -->

<!-- commons -->

{% assign urls = site.data.assets[origin].jquery.js
| append: ','
| append: site.data.assets[origin].bootstrap.js
| append: ','
| append: site.data.assets[origin].search.js
%}

<!-- layout specified -->

{% if page.layout == 'post' %}
{% if site.google_analytics.pv.proxy_endpoint or site.google_analytics.pv.cache_path %}
<!-- pv-report needs countup.js -->
<script async src="{{ site.data.assets[origin].countup.js | relative_url }}"></script>
{% assign urls = urls | append: ',' | append: site.data.assets[origin].countup.js %}
{% endif %}
{% endif %}

{% if page.layout == 'post' or page.layout == 'page' or page.layout == 'home' %}
{% assign _urls = site.data.assets[origin].lazysizes.js %}
{% assign urls = urls | append: ',' | append: site.data.assets[origin].lazysizes.js %}

{% unless page.layout == 'home' %}
{% assign _urls = _urls
<!-- image lazy-loading & popup & clipboard -->
{% assign urls = urls
| append: ','
| append: site.data.assets[origin]['magnific-popup'].js
| append: ','
| append: site.data.assets[origin].clipboard.js
%}
{% endunless %}

{% include jsdelivr-combine.html urls=_urls %}
{% endif %}

{% if page.layout == 'home'
Expand All @@ -32,7 +40,9 @@
%}
{% assign locale = site.lang | split: '-' | first %}

{% assign _urls = site.data.assets[origin].dayjs.js.common
{% assign urls = urls
| append: ','
| append: site.data.assets[origin].dayjs.js.common
| append: ','
| append: site.data.assets[origin].dayjs.js.locale
| replace: ':LOCALE', locale
Expand All @@ -41,10 +51,18 @@
| append: ','
| append: site.data.assets[origin].dayjs.js.localizedFormat
%}
{% endif %}

{% include jsdelivr-combine.html urls=_urls %}
{% if page.content contains '<h2' or page.content contains '<h3' and site.toc and page.toc %}
{% assign urls = urls | append: ',' | append: site.data.assets[origin].toc.js %}
{% endif %}

{% if page.mermaid %}
{% assign urls = urls | append: ',' | append: site.data.assets[origin].mermaid.js %}
{% endif %}

{% include jsdelivr-combine.html urls=urls %}

{% case page.layout %}
{% when 'home', 'categories', 'post', 'page' %}
{% assign type = page.layout %}
Expand Down Expand Up @@ -80,10 +98,6 @@
<script id="MathJax-script" async src="{{ site.data.assets[origin].mathjax.js | relative_url }}"></script>
{% endif %}

<!-- commons -->

<script src="{{ site.data.assets[origin].bootstrap.js | relative_url }}"></script>

{% if jekyll.environment == 'production' %}
<!-- PWA -->
{% if site.pwa.enabled %}
Expand Down
6 changes: 0 additions & 6 deletions _includes/jsdelivr-combine.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{% assign domain = 'https://cdn.jsdelivr.net/' %}

{% for url in urls %}

{% if url contains domain %}
{% assign url_snippet = url | slice: domain.size, url.size %}

Expand All @@ -16,15 +15,10 @@
{% endif %}

{% elsif url contains '//' %}

<script src="{{ url }}"></script>

{% else %}

<script src="{{ url | relative_url }}"></script>

{% endif %}

{% endfor %}

{% if combined_urls %}
Expand Down
57 changes: 28 additions & 29 deletions _includes/mermaid.html
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
<!--
mermaid-js loader
-->

<script src="{{ site.data.assets[origin].mermaid.js | relative_url }}"></script>

<script>
<!-- mermaid-js loader -->
<script type="text/javascript">
(function () {

function updateMermaid(event) {
if (event.source === window && event.data &&
event.data.direction === ModeToggle.ID) {

if (event.source === window && event.data && event.data.direction === ModeToggle.ID) {
const mode = event.data.message;

if (typeof mermaid === "undefined") {
if (typeof mermaid === 'undefined') {
return;
}

let expectedTheme = (mode === ModeToggle.DARK_MODE ? "dark" : "default");
let config = {theme: expectedTheme};
let expectedTheme = mode === ModeToggle.DARK_MODE ? 'dark' : 'default';
let config = { theme: expectedTheme };

/* Re-render the SVG › <https://github.com/mermaid-js/mermaid/issues/311#issuecomment-332557344> */
$(".mermaid").each(function () {
$('.mermaid').each(function () {
let svgCode = $(this).prev().children().html();
$(this).removeAttr("data-processed");
$(this).removeAttr('data-processed');
$(this).html(svgCode);
});

mermaid.initialize(config);
mermaid.init(undefined, ".mermaid");
mermaid.init(undefined, '.mermaid');
}
}

let initTheme = "default";
let initTheme = 'default';
const html = document.documentElement;

if ($("html[data-mode=dark]").length > 0
|| ($("html[data-mode]").length == 0
&& window.matchMedia("(prefers-color-scheme: dark)").matches)) {
initTheme = "dark";
if (
(html.hasAttribute('data-mode') && html.getAttribute('data-mode') === 'dark') ||
(!html.hasAttribute('data-mode') && window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
initTheme = 'dark';
}

let mermaidConf = {
theme: initTheme /* <default|dark|forest|neutral> */
theme: initTheme /* <default|dark|forest|neutral> */
};

/* Create mermaid tag */
$("pre").has("code.language-mermaid").each(function () {
let svgCode = $(this).children().html();
$(this).addClass("unloaded");
$(this).after(`<pre class=\"mermaid\">${svgCode}</pre>`);
document.querySelectorAll('pre>code.language-mermaid').forEach((elem) => {
const svgCode = elem.textContent;
const backup = elem.parentElement;
backup.classList.add('unloaded');
/* create mermaid node */
let mermaid = document.createElement('pre');
mermaid.classList.add('mermaid');
const text = document.createTextNode(svgCode);
mermaid.appendChild(text);
backup.after(mermaid);
});

mermaid.initialize(mermaidConf);

window.addEventListener("message", updateMermaid);
window.addEventListener('message', updateMermaid);
})();

</script>
Loading

0 comments on commit 9d97120

Please sign in to comment.