Skip to content

Commit 9577762

Browse files
committed
feat: add option to preload fonts
1 parent 87e89c9 commit 9577762

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

assets/apps/customizer-controls/src/typography-extra/LocalGoogleFonts.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const initLocalGoogleFonts = () => {
2020
return;
2121
}
2222

23-
const toggleControl =
23+
const localFontsToggle =
2424
new wp.customize.controlConstructor.neve_toggle_control(
2525
NeveReactCustomize.localGoogleFonts.key,
2626
{
@@ -32,7 +32,24 @@ const initLocalGoogleFonts = () => {
3232
}
3333
);
3434

35-
render(<ToggleComponent control={toggleControl} />, section.container[0]);
35+
const preloadFontsToggle =
36+
new wp.customize.controlConstructor.neve_toggle_control(
37+
NeveReactCustomize.preloadFonts.key,
38+
{
39+
section: section.id,
40+
label: __('Preload fonts', 'neve'),
41+
setting: NeveReactCustomize.preloadFonts.key,
42+
priority: 6,
43+
}
44+
);
45+
46+
render(
47+
<>
48+
<ToggleComponent control={preloadFontsToggle} />
49+
<ToggleComponent control={localFontsToggle} />
50+
</>,
51+
section.container[0]
52+
);
3653
};
3754

3855
export { initLocalGoogleFonts };

inc/core/settings/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class Config {
9494
const MODS_POST_TYPE_VSPACING = 'content_vspacing';
9595

9696
const OPTION_LOCAL_GOOGLE_FONTS_HOSTING = 'nv_pro_enable_local_fonts';
97+
const MODS_PRELOAD_FONTS = 'neve_preload_fonts';
9798
const OPTION_POSTS_PER_PAGE = 'posts_per_page';
9899

99100
const MODS_TPOGRAPHY_FONT_PAIRS = 'neve_font_pairs';

inc/customizer/loader.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ public function enqueue_customizer_controls() {
144144
'learnMore' => apply_filters( 'neve_external_link', 'https://docs.themeisle.com/article/1349-how-to-load-neve-fonts-locally', esc_html__( 'Learn more', 'neve' ) ),
145145
'key' => Config::OPTION_LOCAL_GOOGLE_FONTS_HOSTING,
146146
),
147+
'preloadFonts' => array(
148+
'key' => Config::MODS_PRELOAD_FONTS,
149+
),
147150
'fontPairs' => get_theme_mod( Config::MODS_TPOGRAPHY_FONT_PAIRS, Config::$typography_default_pairs ),
148151
'allowedGlobalCustomColor' => Colors_Background::CUSTOM_COLOR_LIMIT,
149152
'constants' => [
@@ -291,6 +294,14 @@ public function register_setting_local_gf( $wp_customize ) {
291294
'default' => false,
292295
]
293296
);
297+
298+
$wp_customize->add_setting(
299+
Config::MODS_PRELOAD_FONTS,
300+
[
301+
'sanitize_callback' => 'neve_sanitize_checkbox',
302+
'default' => false,
303+
]
304+
);
294305
}
295306

296307
/**

inc/views/font_manager.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function init() {
9393
add_action( 'enqueue_block_editor_assets', array( $this, 'register_google_fonts' ), 200 );
9494
add_action( 'enqueue_block_editor_assets', array( $this, 'do_editor_styles_google_fonts' ), 210 );
9595
add_action( 'wp_print_styles', array( $this, 'load_external_fonts_locally' ), PHP_INT_MAX );
96+
add_filter( 'style_loader_tag', array( $this, 'add_rel_preload' ), 10, 2 );
9697
}
9798

9899
/**
@@ -251,6 +252,46 @@ private function enqueue_google_font( $font, $weights = [], $skip_enqueue = fals
251252
wp_enqueue_style( 'neve-google-font-' . str_replace( ' ', '-', strtolower( $font ) ), $url, array(), NEVE_VERSION );
252253
}
253254

255+
/**
256+
* Add onload, rel and as attributes for Google Font stylesheets.
257+
* Implements lazy loading with preload for better performance.
258+
*
259+
* @param string $html Current html code.
260+
* @param string $handle Current script handle.
261+
*
262+
* @return string
263+
*/
264+
public function add_rel_preload( $html, $handle ) {
265+
if ( is_admin() ) {
266+
return $html;
267+
}
268+
269+
$preload_enabled = get_theme_mod( Config::MODS_PRELOAD_FONTS, false );
270+
271+
/**
272+
* Filters whether fonts should be preloaded.
273+
*
274+
* @param bool $preload_enabled Whether fonts should be preloaded. Default value is false.
275+
*
276+
* @since 3.9.0
277+
*/
278+
$should_preload = apply_filters( 'neve_preload_fonts', $preload_enabled );
279+
280+
if ( ! (bool) $should_preload ) {
281+
return $html;
282+
}
283+
284+
// Only preload Google Font stylesheets
285+
if ( strpos( $handle, 'neve-google-font-' ) === 0 ) {
286+
// Lazy load with JS, but also add noscript in case no JS
287+
$no_script = '<noscript>' . $html . '</noscript>';
288+
// Add onload, rel="preload", as="style", and put together with noscript
289+
$html = str_replace( 'rel=\'stylesheet\'', 'rel="preload" as="style" onload="this.rel=\'stylesheet\';"', $html ) . $no_script;
290+
}
291+
292+
return $html;
293+
}
294+
254295
/**
255296
* Load Google Fonts locally.
256297
*

0 commit comments

Comments
 (0)