🔒 🔧 Fixes #142 Optimize CDN service code

This commit is contained in:
elkan1788 2024-07-20 21:36:08 +08:00
parent 4e05bfb399
commit c5c1f03557
9 changed files with 102 additions and 43 deletions

View File

@ -47,6 +47,14 @@
img {
display: inline-block;
max-width: 50px;
@include mobile() {
max-width: 40px;
}
&:last-child {
max-height: 18px;
}
}
$footer_vendors_imgs_len: length($footer_vendors_imgs_width);

View File

@ -240,25 +240,24 @@ NexT.utils = {
getCDNResource: function (res) {
let { plugins, router } = NexT.CONFIG.vendor;
let { name, version, file, alias, alias_name } = res;
let npm_name = name;
if (alias_name) npm_name = alias_name;
let res_src = '';
switch (plugins) {
case 'cdnjs':
case 'bootcdn':
case 'qiniu':
switch (router.type) {
case 'modern':
let cdnjs_name = alias || name;
let cdnjs_file = file.replace(/^(dist|lib|source|\/js|)\/(browser\/|)/, '');
if (cdnjs_file.indexOf('min') == -1) {
cdnjs_file = cdnjs_file.replace(/\.js$/, '.min.js');
}
res_src = `${router}/${cdnjs_name}/${version}/${cdnjs_file}`
res_src = `${router.url}/${cdnjs_name}/${version}/${cdnjs_file}`
break;
default:
res_src = `${router}/${npm_name}@${version}/${file}`
res_src = `${router.url}/${npm_name}@${version}/${file}`
}
return res_src;
},

View File

@ -1,33 +1,76 @@
# CDN 公共资源商列表
# Public CDN vendor list
vendors:
cdnjs: "https://cdnjs.cloudflare.com/ajax/libs"
unpkg: "https://unpkg.com"
bootcdn: "https://cdn.bootcdn.net/ajax/libs"
qiniu: "https://cdn.staticfile.net"
- name: cdnjs
home: "https://cdnjs.com"
logo: "https://cdnjs.com/_/f7a2ebfb819c118086546e481876aef6.svg"
type: "modern"
url: "https://cdnjs.cloudflare.com/ajax/libs"
- name: unpkg
home: "https://unpkg.com"
logo: "https://unpkg.fly.dev/_client/b870d5fb04d2854d.png"
type: "nodejs"
url: "https://unpkg.com"
- name: bootcdn
home: "https://www.bootcdn.cn"
logo: "https://www.bootcdn.cn/assets/img/logo.png"
type: "modern"
url: "https://cdn.bootcdn.net/ajax/libs"
- name: staticfile
home: "https://staticfile.net/"
logo: "https://www.staticfile.net/images/logo.png"
type: "modern"
url: "https://cdn.staticfile.net"
- name: 7ed
home: "https://www.7ed.net"
logo: "https://github.com/7ednet/homepage/raw/main/_images/logo.svg"
type: "modern"
url: "https://use.sevencdn.com/ajax/libs"
- name: zstatic
home: "https://www.zstatic.net"
logo: "https://www.zstatic.net/favicon/favicon-16x16.png"
type: "modern"
url: "https://s4.zstatic.net/ajax/libs"
- name: zeyao
home: "https://cdn.jsdmirror.com/"
logo: "https://img.cuteapi.com/2023/08/29/Background.png"
type: "nodejs"
url: "https://cdn.jsdmirror.com/npm"
- name: jsdelivr
home: "https://www.jsdelivr.com/"
logo: "https://www.jsdelivr.com/assets/0c183396fdb0079ea31849b02bc5e3429f1740de/img/jsdelivr-horizontal-regular.svg"
type: "nodejs"
url: "https://cdn.jsdelivr.net/npm"
# JavaScript 资源
# JavaScript Resources
js:
- name: animejs
version: 3.2.1
version: 3.2.2
file: lib/anime.min.js
- name: viewerjs
version: 1.11.0
version: 1.11.6
file: dist/viewer.min.js
# CSS 资源
# CSS Resources
css:
- name: '@fortawesome/fontawesome-free'
version: 6.5.2
version: 6.6.0
file: css/all.min.css
alias: font-awesome
- name: animate.css
version: 3.1.1
version: 4.1.1
file: animate.min.css
- name: viewerjs
version: 1.11.0
version: 1.11.6
file: dist/viewer.min.css
# 第三方服务组件资源
@ -84,11 +127,11 @@ livere:
# Full text search
algolia:
name: algoliasearch
version: 4.14.2
file: dist/algoliasearch-lite.umd.js
version: 4.24.0
file: dist/algoliasearch.umd.min.js
instant:
name: instantsearch.js
version: 4.49.0
version: 4.73.2
file: dist/instantsearch.production.min.js
plugins:
@ -96,26 +139,26 @@ plugins:
mathjax:
js:
name: mathjax
version: 3.2.0
file: es5/tex-mml-chtml.js
version: 3.2.2
file: es5/tex-mml-chtml.min.js
katex:
js:
- name: katex
version: 0.16.0
version: 0.16.11
file: dist/katex.min.js
- name: auto-render
alias_name: katex
version: 0.16.0
version: 0.16.11
file: dist/contrib/auto-render.min.js
css:
name: katex
version: 0.16.0
version: 0.16.11
file: dist/katex.min.css
# 画图渲染
mermaid:
js:
name: mermaid
version: 9.1.7
version: 10.9.1
file: dist/mermaid.min.js
# 文章访问/评论统计
waline:

View File

@ -1113,9 +1113,8 @@ params:
# Warning: If you are using the latest master branch of NexT, please set `internal: local`
internal: local
# The default CDN provider of third-party plugins.
# Available values: local | unpkg | cdnjs| qiniu | bootcdn | custom
# Dependencies for `plugins: local`: https://github.com/next-theme/plugins
plugins: qiniu
# Available values: local | | cdnjs | unpkg | bootcdn | staticfile | 7ed | zstatic | zeyao | jsdelivr | custom
plugins: jsdelivr
# TODO
# Custom CDN URL
# For example:

View File

@ -38,7 +38,7 @@
</main>
<footer class="footer">
<div class="footer-inner">
{{ partialCached "footer.html" . }}
{{ partial "footer.html" . }}
</div>
</footer>

View File

@ -8,7 +8,7 @@
{{ end }}
{{ $file := .res.file }}
{{ if ne .vendor "unpkg" }}
{{ if ne .router.type "nodejs" }}
{{ with .res.alias }}
{{ $npm = . }}
{{ end }}
@ -16,6 +16,6 @@
{{ $fmt = "%s/%s/%s/%s" }}
{{ end }}
{{ $pluginRes := printf $fmt .router $npm .res.version $file }}
{{ $pluginRes := printf $fmt .router.url $npm .res.version $file }}
{{ return $pluginRes }}

View File

@ -43,15 +43,19 @@
{{ if .enable }}
<div class="vendors-list">
{{ range .list }}
<a target="_blank" href="{{ .link }}" title="{{ .name }}">
{{ if .image }}
<img src="/imgs/img-lazy-loading.gif" data-src="{{ .image.url }}" alt="{{ .name }}" />
{{ else }}
{{ .name }}
{{ end }}
</a>
<a target="_blank" href="{{ .link }}" title="{{ .name }}">
{{ if .image }}
<img src="/imgs/img-lazy-loading.gif" data-src="{{ .image.url }}" alt="{{ .name }}" />
{{ else }}
{{ .name }}
{{ end }}
</a>
{{ end }}
<span>{{ .title }}</span>
{{ $cv := $.Scratch.Get "router" }}
<a target="_blank" href="{{ $cv.home }}" title="{{ $cv.name }}">
<img src="/imgs/img-lazy-loading.gif" data-src="{{ $cv.logo }}" alt="{{ $cv.name }}" />
</a>
<span>{{ .title }}</span>
</div>
{{ end }}
{{ end }}

View File

@ -21,7 +21,13 @@
{{ $globalVars.Set "tagsCount" (len .Site.Taxonomies.tags) }}
{{ $vendor := .Site.Params.vendors.plugins }}
{{ $router := index .Site.Data.resources.vendors $vendor }}
{{ $router := dict }}
{{ range .Site.Data.resources.vendors }}
{{ if eq .name $vendor }}
{{ $router = . }}
{{ break }}
{{ end }}
{{ end }}
{{ $globalVars.Set "vendor" $vendor }}
{{ $globalVars.Set "router" $router }}

View File

@ -3,8 +3,8 @@
{{ $vendor := .Site.Params.vendors.plugins }}
{{ $router := .Scratch.Get "router" }}
{{ range $jsRes }}
{{ $pluginJS := partial "_funs/get_res.html" (dict "res" . "vendor" $vendor "router" $router) }}
<script type="text/javascript" src="{{ $pluginJS }}" defer></script>
{{ $pluginJS := partial "_funs/get_res.html" (dict "res" . "vendor" $vendor "router" $router) }}
<script type="text/javascript" src="{{ $pluginJS }}" crossorigin="anonymous" defer></script>
{{ end }}
<script class="next-config" data-name="main" type="application/json">{{ .Scratch.Get "config" }}</script>