Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ calendar:
# For more information: https://www.w3.org/TR/resource-hints/#preconnect
preconnect: false

# Performance optimization
performance:
# Lazy-load non-critical CSS (FontAwesome, Fancybox, KaTeX)
# This improves Lighthouse scores by making these CSS files non-render-blocking
lazy_css: false

# Set the text alignment in posts / pages.
text_align:
# Available values: start | end | left | right | center | justify | justify-all | match-parent
Expand Down
4 changes: 2 additions & 2 deletions layout/_partials/head/head.njk
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@

{{ next_font() }}

{{ next_vendors('fontawesome') }}
{{ next_vendors('fontawesome', { lazy: true }) }}

{%- if theme.motion.enable %}
{{ next_vendors('animate_css') }}
{%- endif %}

{%- if theme.fancybox %}
{{ next_vendors('fancybox_css') }}
{{ next_vendors('fancybox_css', { lazy: true }) }}
{%- endif %}

{%- if theme.pace.enable %}
Expand Down
2 changes: 1 addition & 1 deletion layout/_third-party/math/katex.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ next_vendors('katex') }}
{{ next_vendors('katex', { lazy: true }) }}
{%- if theme.math.katex.copy_tex %}
{{ next_data('katex', {
copy_tex_js: theme.vendors.copy_tex_js
Expand Down
15 changes: 14 additions & 1 deletion scripts/helpers/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,26 @@ hexo.extend.helper.register('next_js', function(file, {
return `<script ${pjax ? 'data-pjax ' : ''}${module ? 'type="module" ' : ''}src="${src}" defer></script>`;
});

hexo.extend.helper.register('next_vendors', function(name) {
hexo.extend.helper.register('next_vendors', function(name, options = {}) {
const { url, integrity } = this.theme.vendors[name];
const type = url.endsWith('css') ? 'css' : 'js';
const { lazy = false } = options;

if (type === 'css') {
const integrityAttr = integrity ? ` integrity="${integrity}" crossorigin="anonymous"` : '';

// Lazy-load CSS using preload + onload technique
if (lazy && this.theme.performance?.lazy_css) {
return `<link rel="preload" href="${url}" as="style" onload="this.onload=null;this.rel='stylesheet'"${integrityAttr}>
<noscript><link rel="stylesheet" href="${url}"${integrityAttr}></noscript>`;
}

// Default: render-blocking CSS
if (integrity) return `<link rel="stylesheet" href="${url}" integrity="${integrity}" crossorigin="anonymous">`;
return `<link rel="stylesheet" href="${url}">`;
}

// JS handling unchanged (already uses defer)
if (integrity) return `<script src="${url}" integrity="${integrity}" crossorigin="anonymous" defer></script>`;
return `<script src="${url}" defer></script>`;
});
Expand Down