10000 refactor!: optimize the resource hints (#1717) · Goder-0/goder-0.github.io@dcb0add · GitHub
[go: up one dir, main page]

Skip to content

Commit dcb0add

Browse files
authored
refactor!: optimize the resource hints (cotes2020#1717)
Improved the data structure for defining resource hints to the browser.
1 parent f1c6d2a commit dcb0add

File tree

4 files changed

+28
-64
lines changed

4 files changed

+28
-64
lines changed

_data/origin/cors.yml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
# CDNs
2-
3-
cdns:
4-
# Google Fonts
1+
# Resource Hints
2+
resource_hints:
53
- url: https://fonts.googleapis.com
4+
links:
5+
- rel: preconnect
6+
- rel: dns-prefetch
67
- url: https://fonts.gstatic.com
7-
args: crossorigin
8-
- url: https://fonts.googleapis.com
9-
# jsDelivr CDN
8+
links:
9+
- rel: preconnect
10+
opts: [crossorigin]
11+
- rel: dns-prefetch
1012
- url: https://cdn.jsdelivr.net
11-
# polyfill.io for math (cdnjs.cloudflare.com/polyfill)
12-
- url: https://cdnjs.cloudflare.com
13-
14-
# fonts
13+
links:
14+
- rel: preconnect
15+
- rel: dns-prefetch
1516

17+
# Web Fonts
1618
webfonts: https://fonts.googleapis.com/css2?family=Lato:wght@300;400&family=Source+Sans+Pro:wght@400;600;700;900&display=swap
1719

1820
# Libraries

_includes/head.html

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,28 @@
5959

6060
{% include_cached favicons.html %}
6161

62-
{% if site.resources.ignore_env != jekyll.environment and site.resources.self_hosted %}
63-
<link href="{{ site.data.origin[type].webfonts | relative_url }}" rel="stylesheet">
64-
65-
{% else %}
66-
{% for cdn in site.data.origin[type].cdns %}
67-
<link rel="preconnect" href="{{ cdn.url }}" {{ cdn.args }}>
68-
<link rel="dns-prefetch" href="{{ cdn.url }}" {{ cdn.args }}>
62+
<!-- Resource Hints -->
63+
{% unless site.assets.self_host.enabled %}
64+
{% for hint in site.data.origin.cors.resource_hints %}
65+
{% for link in hint.links %}
66+
<link rel="{{ link.rel }}" href="{{ hint.url }}" {{ link.opts | join: ' ' }}>
67+
{% endfor %}
6968
{% endfor %}
70-
71-
<link rel="stylesheet" href="{{ site.data.origin[type].webfonts | relative_url }}">
72-
{% endif %}
73-
74-
<!-- GA -->
75-
{% if jekyll.environment == 'production' and site.analytics.google.id != empty and site.analytics.google.id %}
76-
<link rel="preconnect" href="https://www.google-analytics.com" crossorigin="use-credentials">
77-
<link rel="dns-prefetch" href="https://www.google-analytics.com">
78-
79-
<link rel="preconnect" href="https://www.googletagmanager.com" crossorigin="anonymous">
80-
<link rel="dns-prefetch" href="https://www.googletagmanager.com">
81-
{% endif %}
69+
{% endunless %}
8270

8371
<!-- Bootstrap -->
8472
<link rel="stylesheet" href="{{ site.data.origin[type].bootstrap.css | relative_url }}">
8573

86-
<!-- Font Awesome -->
74+
<!-- Theme style -->
75+
<link rel="stylesheet" href="{{ '/assets/css/:THEME.css' | replace: ':THEME', site.theme | relative_url }}">
76+
77+
<!-- Web Font -->
78+
<link rel="stylesheet" href="{{ site.data.origin[type].webfonts | relative_url }}">
79+
80+
<!-- Font Awesome Icons -->
8781
<link rel="stylesheet" href="{{ site.data.origin[type].fontawesome.css | relative_url }}">
8882

89-
<link rel="stylesheet" href="{{ '/assets/css/:THEME.css' | replace: ':THEME', site.theme | relative_url }}">
83+
<!-- 3rd-party Dependencies -->
9084

9185
{% if site.toc and page.toc %}
9286
<link rel="stylesheet" href="{{ site.data.origin[type].toc.css | relative_url }}">

assets/js/data/swconf.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,6 @@ const swconf = {
2222
{% endfor %}
2323
],
2424

25-
{%- comment -%} The request url with below domain will be cached. {%- endcomment -%}
26-
allowHosts: [
27-
{% if site.cdn and site.cdn contains '//' %}
28-
'{{ site.cdn | split: '//' | last | split: '/' | first }}',
29-
{% endif %}
30-
31-
{%- unless site.assets.self_host.enabled -%}
32-
{% for cdn in site.data.origin["cors"].cdns %}
33-
'{{ cdn.url | split: "//" | last }}'
34-
{%- unless forloop.last -%},{%- endunless -%}
35-
{% endfor %}
36-
{% endunless %}
37-
],
38-
3925
{%- comment -%} The request url with below path will not be cached. {%- endcomment -%}
4026
denyPaths: [
4127
{% for path in site.pwa.cache.deny_paths %}

assets/js/pwa/sw.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,7 @@ const swconfUrl = '{{ '/assets/js/data/swconf.js' | relative_url }}';
99
importScripts(swconfUrl);
1010
const purge = swconf.purge;
1111

12-
function verifyHost(url) {
13-
for (const host of swconf.allowHosts) {
14-
const regex = RegExp(`^http(s)?://${host}/`);
15-
if (regex.test(url)) {
16-
return true;
17-
}
18-
}
19-
return false;
20-
}
21-
2212
function verifyUrl(url) {
23-
if (!verifyHost(url)) {
24-
return false;
25-
}
26-
2713
const requestPath = new URL(url).pathname;
2814

2915
for (const path of swconf.denyPaths) {
@@ -34,10 +20,6 @@ function verifyUrl(url) {
3420
return true;
3521
}
3622

37-
if (!purge) {
38-
swconf.allowHosts.push(location.host);
39-
}
40-
4123
self.addEventListener('install', (event) => {
4224
if (purge) {
4325
return;

0 commit comments

Comments
 (0)
0