Skip to content

Commit bb5e299

Browse files
committed
Merge branch 'feature/lazy-css-loading'
2 parents 0602137 + dccc6c3 commit bb5e299

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

_config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ calendar:
347347
# For more information: https://www.w3.org/TR/resource-hints/#preconnect
348348
preconnect: false
349349

350+
# Performance optimization
351+
performance:
352+
# Lazy-load non-critical CSS (FontAwesome, Fancybox, KaTeX)
353+
# This improves Lighthouse scores by making these CSS files non-render-blocking
354+
lazy_css: false
355+
350356
# Set the text alignment in posts / pages.
351357
text_align:
352358
# Available values: start | end | left | right | center | justify | justify-all | match-parent

layout/_partials/head/head.njk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@
4848

4949
{{ next_font() }}
5050

51-
{{ next_vendors('fontawesome') }}
51+
{{ next_vendors('fontawesome', { lazy: true }) }}
5252

5353
{%- if theme.motion.enable %}
5454
{{ next_vendors('animate_css') }}
5555
{%- endif %}
5656

5757
{%- if theme.fancybox %}
58-
{{ next_vendors('fancybox_css') }}
58+
{{ next_vendors('fancybox_css', { lazy: true }) }}
5959
{%- endif %}
6060

6161
{%- if theme.pace.enable %}

layout/_third-party/math/katex.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ next_vendors('katex') }}
1+
{{ next_vendors('katex', { lazy: true }) }}
22
{%- if theme.math.katex.copy_tex %}
33
{{ next_data('katex', {
44
copy_tex_js: theme.vendors.copy_tex_js

scripts/helpers/engine.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,26 @@ hexo.extend.helper.register('next_js', function(file, {
3535
return `<script ${pjax ? 'data-pjax ' : ''}${module ? 'type="module" ' : ''}src="${src}" defer></script>`;
3636
});
3737

38-
hexo.extend.helper.register('next_vendors', function(name) {
38+
hexo.extend.helper.register('next_vendors', function(name, options = {}) {
3939
const { url, integrity } = this.theme.vendors[name];
4040
const type = url.endsWith('css') ? 'css' : 'js';
41+
const { lazy = false } = options;
42+
4143
if (type === 'css') {
44+
const integrityAttr = integrity ? ` integrity="${integrity}" crossorigin="anonymous"` : '';
45+
46+
// Lazy-load CSS using preload + onload technique
47+
if (lazy && this.theme.performance?.lazy_css) {
48+
return `<link rel="preload" href="${url}" as="style" onload="this.onload=null;this.rel='stylesheet'"${integrityAttr}>
49+
<noscript><link rel="stylesheet" href="${url}"${integrityAttr}></noscript>`;
50+
}
51+
52+
// Default: render-blocking CSS
4253
if (integrity) return `<link rel="stylesheet" href="${url}" integrity="${integrity}" crossorigin="anonymous">`;
4354
return `<link rel="stylesheet" href="${url}">`;
4455
}
56+
57+
// JS handling unchanged (already uses defer)
4558
if (integrity) return `<script src="${url}" integrity="${integrity}" crossorigin="anonymous" defer></script>`;
4659
return `<script src="${url}" defer></script>`;
4760
});

0 commit comments

Comments
 (0)