Load scripts using defer in <head> (#882)

This commit is contained in:
Kem Chen 2025-03-16 19:39:03 +08:00 committed by GitHub
parent fd3852602f
commit 69e0dbdeea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 11 deletions

View File

@ -5,6 +5,11 @@
{%- include '_partials/head/head-unique.njk' -%} {%- include '_partials/head/head-unique.njk' -%}
<title>{% block title %}{% endblock %}</title> <title>{% block title %}{% endblock %}</title>
{{ partial('_third-party/analytics/index.njk', {}, {cache: theme.cache.enable}) }} {{ partial('_third-party/analytics/index.njk', {}, {cache: theme.cache.enable}) }}
{{ partial('_scripts/index.njk', {}, {cache: theme.cache.enable}) }}
{{ partial('_third-party/index.njk', {}, {cache: theme.cache.enable}) }}
{{ partial('_third-party/statistics/index.njk', {}, {cache: theme.cache.enable}) }}
{%- include '_third-party/math/index.njk' -%}
{%- include '_third-party/quicklink.njk' -%}
{{- next_inject('head') }} {{- next_inject('head') }}
<noscript> <noscript>
<link rel="stylesheet" href="{{ url_for(theme.css) }}/noscript.css"> <link rel="stylesheet" href="{{ url_for(theme.css) }}/noscript.css">
@ -40,13 +45,6 @@
{{ partial('_partials/widgets.njk', {}, {cache: theme.cache.enable}) }} {{ partial('_partials/widgets.njk', {}, {cache: theme.cache.enable}) }}
{{ partial('_scripts/index.njk', {}, {cache: theme.cache.enable}) }}
{{ partial('_third-party/index.njk', {}, {cache: theme.cache.enable}) }}
{{ partial('_third-party/statistics/index.njk', {}, {cache: theme.cache.enable}) }}
{%- include '_third-party/math/index.njk' -%}
{%- include '_third-party/quicklink.njk' -%}
{{- next_inject('bodyEnd') }} {{- next_inject('bodyEnd') }}
</body> </body>
</html> </html>

View File

@ -1,5 +1,5 @@
{%- if theme.canvas_ribbon.enable %} {%- if theme.canvas_ribbon.enable %}
<script size="{{ theme.canvas_ribbon.size }}" alpha="{{ theme.canvas_ribbon.alpha }}" zIndex="{{ theme.canvas_ribbon.zIndex }}" src="{{ theme.vendors.canvas_ribbon.url }}"></script> <script size="{{ theme.canvas_ribbon.size }}" alpha="{{ theme.canvas_ribbon.alpha }}" zIndex="{{ theme.canvas_ribbon.zIndex }}" src="{{ theme.vendors.canvas_ribbon.url }}" defer></script>
{%- endif %} {%- endif %}
{%- for name in js_vendors() %} {%- for name in js_vendors() %}

View File

@ -32,7 +32,7 @@ hexo.extend.helper.register('next_js', function(file, {
custom : custom_cdn_url custom : custom_cdn_url
}); });
const src = links[internal] || links.local; const src = links[internal] || links.local;
return `<script ${pjax ? 'data-pjax ' : ''}${module ? 'type="module" ' : ''}src="${src}"></script>`; 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) {
@ -42,8 +42,8 @@ hexo.extend.helper.register('next_vendors', function(name) {
if (integrity) return `<link rel="stylesheet" href="${url}" integrity="${integrity}" crossorigin="anonymous">`; if (integrity) return `<link rel="stylesheet" href="${url}" integrity="${integrity}" crossorigin="anonymous">`;
return `<link rel="stylesheet" href="${url}">`; return `<link rel="stylesheet" href="${url}">`;
} }
if (integrity) return `<script src="${url}" integrity="${integrity}" crossorigin="anonymous"></script>`; if (integrity) return `<script src="${url}" integrity="${integrity}" crossorigin="anonymous" defer></script>`;
return `<script src="${url}"></script>`; return `<script src="${url}" defer></script>`;
}); });
hexo.extend.helper.register('next_data', function(name, ...data) { hexo.extend.helper.register('next_data', function(name, ...data) {