Skip to content

Commit

Permalink
refactor!: optimize the resource hints (#1717)
Browse files Browse the repository at this point in the history
Improved the data structure for defining resource hints to the browser.
  • Loading branch information
cotes2020 authored May 1, 2024
1 parent 49951e3 commit ac3eb35
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 64 deletions.
24 changes: 13 additions & 11 deletions _data/origin/cors.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 15 additions & 21 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,28 @@

{% include_cached favicons.html %}

{% if site.resources.ignore_env != jekyll.environment and site.resources.self_hosted %}
<link href="{{ site.data.origin[type].webfonts | relative_url }}" rel="stylesheet">

{% else %}
{% for cdn in site.data.origin[type].cdns %}
<link rel="preconnect" href="{{ cdn.url }}" {{ cdn.args }}>
<link rel="dns-prefetch" href="{{ cdn.url }}" {{ cdn.args }}>
<!-- Resource Hints -->
{% unless site.assets.self_host.enabled %}
{% for hint in site.data.origin.cors.resource_hints %}
{% for link in hint.links %}
<link rel="{{ link.rel }}" href="{{ hint.url }}" {{ link.opts | join: ' ' }}>
{% endfor %}
{% endfor %}

<link rel="stylesheet" href="{{ site.data.origin[type].webfonts | relative_url }}">
{% endif %}

<!-- GA -->
{% if jekyll.environment == 'production' and site.analytics.google.id != empty and site.analytics.google.id %}
<link rel="preconnect" href="https://www.google-analytics.com" crossorigin="use-credentials">
<link rel="dns-prefetch" href="https://www.google-analytics.com">

<link rel="preconnect" href="https://www.googletagmanager.com" crossorigin="anonymous">
<link rel="dns-prefetch" href="https://www.googletagmanager.com">
{% endif %}
{% endunless %}

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

<!-- Font Awesome -->
<!-- Theme style -->
<link rel="stylesheet" href="{{ '/assets/css/:THEME.css' | replace: ':THEME', site.theme | relative_url }}">

<!-- Web Font -->
<link rel="stylesheet" href="{{ site.data.origin[type].webfonts | relative_url }}">

<!-- Font Awesome Icons -->
<link rel="stylesheet" href="{{ site.data.origin[type].fontawesome.css | relative_url }}">

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

{% if site.toc and page.toc %}
<link rel="stylesheet" href="{{ site.data.origin[type].toc.css | relative_url }}">
Expand Down
14 changes: 0 additions & 14 deletions assets/js/data/swconf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
18 changes: 0 additions & 18 deletions assets/js/pwa/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -34,10 +20,6 @@ function verifyUrl(url) {
return true;
}

if (!purge) {
swconf.allowHosts.push(location.host);
}

self.addEventListener('install', (event) => {
if (purge) {
return;
Expand Down

0 comments on commit ac3eb35

Please sign in to comment.