Support preconnect hint for CDN (#122)

This commit is contained in:
1v9 2020-10-18 11:42:43 +08:00 committed by GitHub
parent 088c030073
commit f360c28597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -337,6 +337,10 @@ calendar:
# See: https://theme-next.js.org/docs/theme-settings/miscellaneous # See: https://theme-next.js.org/docs/theme-settings/miscellaneous
# --------------------------------------------------------------- # ---------------------------------------------------------------
# Preconnect CDN for fonts and plugins.
# For more information: https://www.w3.org/TR/resource-hints/#preconnect
preconnect: false
# Set the text alignment in posts / pages. # Set the text alignment in posts / pages.
text_align: text_align:
# Available values: start | end | left | right | center | justify | justify-all | match-parent # Available values: start | end | left | right | center | justify | justify-all | match-parent

View File

@ -3,6 +3,8 @@
<meta name="theme-color" content="{{ theme.android_chrome_color }}"> <meta name="theme-color" content="{{ theme.android_chrome_color }}">
<meta name="generator" content="Hexo {{ hexo_version }}"> <meta name="generator" content="Hexo {{ hexo_version }}">
{{ next_pre() }}
{%- if theme.favicon.apple_touch_icon %} {%- if theme.favicon.apple_touch_icon %}
<link rel="apple-touch-icon" sizes="180x180" href="{{ url_for(theme.favicon.apple_touch_icon) }}"> <link rel="apple-touch-icon" sizes="180x180" href="{{ url_for(theme.favicon.apple_touch_icon) }}">
{%- endif %} {%- endif %}

View File

@ -28,6 +28,26 @@ hexo.extend.helper.register('next_js', function(file, pjax = false) {
return `<script ${pjax ? 'data-pjax ' : ''}src="${src}"></script>`; return `<script ${pjax ? 'data-pjax ' : ''}src="${src}"></script>`;
}); });
hexo.extend.helper.register('next_pre', function() {
const { preconnect } = this.theme;
if (!preconnect) return '';
const { enable, host } = this.theme.font;
const { internal, plugins } = this.theme.vendors;
const links = {
local : '',
jsdelivr: 'https://cdn.jsdelivr.net',
unpkg : 'https://unpkg.com',
cdnjs : 'https://cdnjs.cloudflare.com'
};
const h = enable ? host || 'https://fonts.googleapis.com' : '';
const i = links[internal];
const p = links[plugins];
const results = [...new Set([h, i, p].filter(origin => origin))].map(
origin => `<link rel="preconnect" href="${origin}" crossorigin>`
);
return results.join('\n');
});
hexo.extend.helper.register('post_gallery', function(photos) { hexo.extend.helper.register('post_gallery', function(photos) {
if (!photos || !photos.length) return ''; if (!photos || !photos.length) return '';
const content = photos.map(photo => ` const content = photos.map(photo => `