Skip to content

Commit 02cf99f

Browse files
chore: update prefetch readme
1 parent 84c151c commit 02cf99f

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ export default {
140140
* CSSOM to be created.
141141
*/
142142
preload: true
143+
144+
/**
145+
* Using `<link rel="prefetch">` is intended for prefetching resources
146+
* that will be used in the next navigation/page load
147+
* (e.g. when you go to the next page)
148+
*
149+
* Note: this can not be used with `preload`
150+
*/
151+
prefetch: false
143152
}
144153
})
145154
],

src/custom.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ export interface CustomFonts {
6464
* @default true
6565
*/
6666
preload?: boolean
67-
/**
68-
* Using `<link rel="prefetch">`
69-
* @default true
67+
68+
/**
69+
* Using `<link rel="prefetch">` is intended for prefetching resources
70+
* that will be used in the next navigation/page load
71+
* (e.g. when you go to the next page)
72+
*
73+
* Note: this can not be used with `preload`
74+
* @default false
7075
*/
7176
prefetch?: boolean
7277
}
@@ -127,11 +132,11 @@ const createFontFaceCSS = ({ name, src, local, weight, style, display }: CustomF
127132
}`
128133
}
129134

130-
const createPreloadFontFaceLink = (href: string) => {
135+
const createFontFaceLink = (prefetch = false) => (href: string) => {
131136
return {
132137
tag: 'link',
133138
attrs: {
134-
rel: 'preload',
139+
rel: prefetch ? 'prefetch' : 'preload',
135140
as: 'font',
136141
type: `font/${href.split('.').pop()}`,
137142
href,
@@ -140,32 +145,19 @@ const createPreloadFontFaceLink = (href: string) => {
140145
}
141146
}
142147

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-
156148
export default (options: CustomFonts, config: ResolvedConfig) => {
157149
const tags: HtmlTagDescriptor[] = []
158150
const css: string[] = []
159151

160152
// --- Extract and defaults plugin options.
153+
/* eslint-disable prefer-const */
161154
let {
162155
families = [],
163-
// eslint-disable-next-line prefer-const
164156
display = 'auto',
165-
// eslint-disable-next-line prefer-const
166157
preload = true,
167158
prefetch = false,
168159
} = options
160+
/* eslint-enable prefer-const */
169161

170162
// --- Cast as array of `CustomFontFamily`.
171163
if (!Array.isArray(families)) {
@@ -205,12 +197,11 @@ export default (options: CustomFonts, config: ResolvedConfig) => {
205197

206198
// --- Generate `<link>` tags.
207199
// --- We can not do a prefetch and a preload for the same files.
208-
if(preload && prefetch){
209-
console.warn('vite-plugin-fonts','We can not do a prefetch and a preload for the same files.');
210-
console.warn('vite-plugin-fonts','The prefetch stand for a lower priority for the resource (maybe we will need it in a future page) whereas preload is for the current page, so we can not have both.')
211-
};
212-
if (preload) tags.push(...hrefs.map(createPreloadFontFaceLink))
213-
if (prefetch && !preload) tags.push(...hrefs.map(createPrefetchFontFaceLink))
200+
if (preload && prefetch) {
201+
console.warn('vite-plugin-fonts: Prefetch and a Preload options can not be used together.')
202+
console.warn('vite-plugin-fonts: The prefetch stand for a lower priority for the resource (maybe we will need it in a future page) whereas preload is for the current page, so we can not have both.')
203+
}
204+
if (preload || prefetch) tags.push(...hrefs.map(createFontFaceLink(prefetch)))
214205

215206
// --- Generate CSS `@font-face` rules.
216207
for (const face of faces) css.push(createFontFaceCSS(face))

0 commit comments

Comments
 (0)