@@ -64,6 +64,11 @@ export interface CustomFonts {
64
64
* @default true
65
65
*/
66
66
preload ?: boolean
67
+ /**
68
+ * Using `<link rel="prefetch">`
69
+ * @default true
70
+ */
71
+ prefetch ?: boolean
67
72
}
68
73
69
74
const resolveWeight = ( weightOrSrc ?: string | number ) => {
@@ -122,7 +127,7 @@ const createFontFaceCSS = ({ name, src, local, weight, style, display }: CustomF
122
127
}`
123
128
}
124
129
125
- const createFontFaceLink = ( href : string ) => {
130
+ const createPreloadFontFaceLink = ( href : string ) => {
126
131
return {
127
132
tag : 'link' ,
128
133
attrs : {
@@ -135,6 +140,19 @@ const createFontFaceLink = (href: string) => {
135
140
}
136
141
}
137
142
143
+ const createPrefetchFontFaceLink = ( href : string ) => {
144
+ return {
145
+ tag : 'link' ,
146
+ attrs : {
147
+ rel : 'prefetch' ,
148
+ as : 'font' ,
149
+ type : `font/${ href . split ( '.' ) . pop ( ) } ` ,
150
+ href,
151
+ crossorigin : true ,
152
+ } ,
153
+ }
154
+ }
155
+
138
156
export default ( options : CustomFonts , config : ResolvedConfig ) => {
139
157
const tags : HtmlTagDescriptor [ ] = [ ]
140
158
const css : string [ ] = [ ]
@@ -146,6 +164,7 @@ export default (options: CustomFonts, config: ResolvedConfig) => {
146
164
display = 'auto' ,
147
165
// eslint-disable-next-line prefer-const
148
166
preload = true ,
167
+ prefetch = true ,
149
168
} = options
150
169
151
170
// --- Cast as array of `CustomFontFamily`.
@@ -185,7 +204,8 @@ export default (options: CustomFonts, config: ResolvedConfig) => {
185
204
. map ( src => src . replace ( config . root , '.' ) )
186
205
187
206
// --- Generate `<link>` tags.
188
- if ( preload ) tags . push ( ...hrefs . map ( createFontFaceLink ) )
207
+ if ( preload ) tags . push ( ...hrefs . map ( createPreloadFontFaceLink ) )
208
+ if ( prefetch ) tags . push ( ...hrefs . map ( createPrefetchFontFaceLink ) )
189
209
190
210
// --- Generate CSS `@font-face` rules.
191
211
for ( const face of faces ) css . push ( createFontFaceCSS ( face ) )
0 commit comments