From dcb0add47bf1adf92215514f1ccfa4661d5215be Mon Sep 17 00:00:00 2001
From: Cotes Chung <11371340+cotes2020@users.noreply.github.com>
Date: Thu, 2 May 2024 05:11:45 +0800
Subject: [PATCH] refactor!: optimize the resource hints (#1717)
Improved the data structure for defining resource hints to the browser.
---
_data/origin/cors.yml | 24 +++++++++++++-----------
_includes/head.html | 36 +++++++++++++++---------------------
assets/js/data/swconf.js | 14 --------------
assets/js/pwa/sw.js | 18 ------------------
4 files changed, 28 insertions(+), 64 deletions(-)
diff --git a/_data/origin/cors.yml b/_data/origin/cors.yml
index a10d31c..ff249e6 100644
--- a/_data/origin/cors.yml
+++ b/_data/origin/cors.yml
@@ -1,18 +1,20 @@
-# CDNs
-
-cdns:
- # Google Fonts
+# Resource Hints
+resource_hints:
- url: https://fonts.googleapis.com
+ links:
+ - rel: preconnect
+ - rel: dns-prefetch
- url: https://fonts.gstatic.com
- args: crossorigin
- - url: https://fonts.googleapis.com
- # jsDelivr CDN
+ links:
+ - rel: preconnect
+ opts: [crossorigin]
+ - rel: dns-prefetch
- url: https://cdn.jsdelivr.net
- # polyfill.io for math (cdnjs.cloudflare.com/polyfill)
- - url: https://cdnjs.cloudflare.com
-
-# fonts
+ links:
+ - rel: preconnect
+ - rel: dns-prefetch
+# Web Fonts
webfonts: https://fonts.googleapis.com/css2?family=Lato:wght@300;400&family=Source+Sans+Pro:wght@400;600;700;900&display=swap
# Libraries
diff --git a/_includes/head.html b/_includes/head.html
index f3434cf..0e4725c 100644
--- a/_includes/head.html
+++ b/_includes/head.html
@@ -59,34 +59,28 @@
{% include_cached favicons.html %}
- {% if site.resources.ignore_env != jekyll.environment and site.resources.self_hosted %}
-
-
- {% else %}
- {% for cdn in site.data.origin[type].cdns %}
-
-
+
+ {% unless site.assets.self_host.enabled %}
+ {% for hint in site.data.origin.cors.resource_hints %}
+ {% for link in hint.links %}
+
+ {% endfor %}
{% endfor %}
-
-
- {% endif %}
-
-
- {% if jekyll.environment == 'production' and site.analytics.google.id != empty and site.analytics.google.id %}
-
-
-
-
-
- {% endif %}
+ {% endunless %}
-
+
+
+
+
+
+
+
-
+
{% if site.toc and page.toc %}
diff --git a/assets/js/data/swconf.js b/assets/js/data/swconf.js
index c02638f..5c1ed29 100644
--- a/assets/js/data/swconf.js
+++ b/assets/js/data/swconf.js
@@ -22,20 +22,6 @@ const swconf = {
{% endfor %}
],
- {%- comment -%} The request url with below domain will be cached. {%- endcomment -%}
- allowHosts: [
- {% if site.cdn and site.cdn contains '//' %}
- '{{ site.cdn | split: '//' | last | split: '/' | first }}',
- {% endif %}
-
- {%- unless site.assets.self_host.enabled -%}
- {% for cdn in site.data.origin["cors"].cdns %}
- '{{ cdn.url | split: "//" | last }}'
- {%- unless forloop.last -%},{%- endunless -%}
- {% endfor %}
- {% endunless %}
- ],
-
{%- comment -%} The request url with below path will not be cached. {%- endcomment -%}
denyPaths: [
{% for path in site.pwa.cache.deny_paths %}
diff --git a/assets/js/pwa/sw.js b/assets/js/pwa/sw.js
index dbf30f4..e9609b1 100644
--- a/assets/js/pwa/sw.js
+++ b/assets/js/pwa/sw.js
@@ -9,21 +9,7 @@ const swconfUrl = '{{ '/assets/js/data/swconf.js' | relative_url }}';
importScripts(swconfUrl);
const purge = swconf.purge;
-function verifyHost(url) {
- for (const host of swconf.allowHosts) {
- const regex = RegExp(`^http(s)?://${host}/`);
- if (regex.test(url)) {
- return true;
- }
- }
- return false;
-}
-
function verifyUrl(url) {
- if (!verifyHost(url)) {
- return false;
- }
-
const requestPath = new URL(url).pathname;
for (const path of swconf.denyPaths) {
@@ -34,10 +20,6 @@ function verifyUrl(url) {
return true;
}
-if (!purge) {
- swconf.allowHosts.push(location.host);
-}
-
self.addEventListener('install', (event) => {
if (purge) {
return;