Skip to content

Commit df0adcc

Browse files
fixed: fonts are now lazy loaded and fixed delayed loading
1 parent 6ebc460 commit df0adcc

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

.config/webpack.config.dev.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = [
3434

3535
output: {
3636
filename: '[name].js',
37+
chunkFilename: '[name].[contenthash].json', // Output filename for dynamically imported chunks
3738
},
3839

3940
// Externals are only WordPress loaded libraries.

.config/webpack.config.prod.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = [
3434

3535
output: {
3636
filename: '[name].js',
37+
chunkFilename: '[name].[contenthash].json', // Output filename for dynamically imported chunks
3738
},
3839

3940
// Externals are only WordPress loaded libraries.

src/components/font-family-control/index.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { i18n } from 'stackable'
66
/**
77
* Internal dependencies
88
*/
9-
import fonts from './google-fonts.json'
109
import {
1110
loadGoogleFont,
1211
MODERN_FONT_STACKS,
@@ -18,20 +17,32 @@ import {
1817
*/
1918
import { __ } from '@wordpress/i18n'
2019
import { applyFilters } from '@wordpress/hooks'
21-
import { useMemo } from '@wordpress/element'
20+
import {
21+
useMemo, useState, useEffect,
22+
} from '@wordpress/element'
2223
import AdvancedAutosuggestControl from '../advanced-autosuggest-control'
2324

2425
import { select } from '@wordpress/data'
2526

26-
const fontOptions = fonts.map( font => {
27-
return { label: font.family, value: font.family }
28-
} )
27+
const loadGoogleFonts = async () => {
28+
const { default: fonts } =
29+
await import( /* webpackChunkName: "data/google-fonts" */ './google-fonts.json' )
30+
return fonts.map( font => ( { label: font.family, value: font.family } ) )
31+
}
2932

3033
const FontFamilyControl = props => {
3134
const {
3235
loadingThemeFont, themeFonts, themeFontOptions,
3336
} = select( 'stackable/theme-fonts' ).getThemeFonts()
3437

38+
const [ googleFontOptions, setGoogleFontOptions ] = useState( [] )
39+
40+
useEffect( () => {
41+
loadGoogleFonts().then( fontOptions => {
42+
setGoogleFontOptions( fontOptions )
43+
} )
44+
}, [] )
45+
3546
const options = useMemo( () => {
3647
const allFontOptions = [
3748
{
@@ -53,7 +64,7 @@ const FontFamilyControl = props => {
5364
{
5465
id: 'google-fonts',
5566
title: __( 'Google Fonts', i18n ),
56-
options: fontOptions,
67+
options: googleFontOptions,
5768
},
5869
]
5970
if ( themeFonts.length ) {
@@ -64,7 +75,7 @@ const FontFamilyControl = props => {
6475
} )
6576
}
6677
return applyFilters( 'stackable.font-family-control.options', allFontOptions )
67-
}, [ loadingThemeFont ] )
78+
}, [ loadingThemeFont, googleFontOptions ] )
6879

6980
return (
7081
<AdvancedAutosuggestControl
@@ -74,7 +85,7 @@ const FontFamilyControl = props => {
7485
onChange={ fontFamily => {
7586
if ( ! themeFonts.includes( fontFamily ) ) {
7687
// Load font if it's a Google font.
77-
fontOptions.some( font => {
88+
googleFontOptions.some( font => {
7889
if ( font.value === fontFamily ) {
7990
loadGoogleFont( fontFamily )
8091
return true

src/hooks/use-font-loader.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import { select } from '@wordpress/data'
77
export const useFontLoader = fontFamilyValue => {
88
const { loadingThemeFont, themeFonts } = select( 'stackable/theme-fonts' ).getThemeFonts()
99
useEffect( () => {
10-
if ( ! loadingThemeFont ) {
11-
if ( ! themeFonts.includes( fontFamilyValue ) ) {
12-
loadGoogleFont( fontFamilyValue )
13-
doAction( 'stackable.font-loader.load', fontFamilyValue )
14-
}
10+
if ( ! themeFonts.includes( fontFamilyValue ) ) {
11+
loadGoogleFont( fontFamilyValue )
12+
doAction( 'stackable.font-loader.load', fontFamilyValue )
1513
}
1614
}, [ loadingThemeFont, fontFamilyValue ] )
1715
}

src/util/font.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,18 @@ export const getGoogleFontURL = fontName => {
106106
return `https://fonts.googleapis.com/css?family=${ family }:100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic${ subset }`
107107
}
108108

109-
export const isWebFont = fontName => fontName && ! fontName?.match( /^(sans[-+]serif|serif|monospace|serif-alt)$/i )
109+
export const isWebFont = fontName => {
110+
if ( fontName ) {
111+
if ( Object.keys( SYSTEM_FONT_STACKS ).includes( fontName ) ) {
112+
return false
113+
}
114+
if ( Object.keys( MODERN_FONT_STACKS ).includes( fontName ) ) {
115+
return false
116+
}
117+
return true
118+
}
119+
return false
120+
}
110121

111122
/**
112123
* Returns the current block editor head

0 commit comments

Comments
 (0)