Skip to content

Commit 6b61890

Browse files
authored
feat: introduce link tag filter hook (#71)
* fix: Do not generate link tags when both prefetch+preload are set to false Fixes: #70 * feat: introduce link filter hook Using this hook a hook can be set that allows for filtering the list of link tags that are generated by this plugin. The hook can both supress generating link tags or modify the contents of the links. Fixes: #7 Fixes: #10
1 parent 3f3d481 commit 6b61890

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/index.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ export default createUnplugin<Options | undefined>((userOptions) => {
8787
handler: (html, ctx) => {
8888
const tags = getHeadLinkTags(options)
8989
const files = Object.keys(ctx.bundle ?? {}).filter(key => fontFileRegex.test(key))
90+
const { prefetch: wantPrefetch, preload: wantPreload } = options?.custom || {}
9091
for (const file of files) {
92+
if (!(
93+
wantPrefetch === true || wantPreload === true ||
94+
(wantPrefetch === undefined && wantPreload === undefined)
95+
))
96+
continue
9197
const ext = extname(file)
9298
tags.push({
9399
tag: 'link',
@@ -101,7 +107,16 @@ export default createUnplugin<Options | undefined>((userOptions) => {
101107
},
102108
})
103109
}
104-
return tags
110+
let tagsReturned = tags
111+
if (options?.custom?.linkFilter) {
112+
const newTags: object[] | boolean = options?.custom?.linkFilter(tags)
113+
if (Array.isArray(newTags)) {
114+
tagsReturned = newTags
115+
} else {
116+
tagsReturned = newTags ? tags : []
117+
}
118+
}
119+
return tagsReturned
105120
},
106121
},
107122
},
@@ -121,7 +136,14 @@ function generateVitepressBundle(
121136

122137
const tags = getHeadLinkTags(options)
123138
const files = Object.keys(bundle ?? {}).filter(key => fontFileRegex.test(key))
139+
const { prefetch: wantPrefetch, preload: wantPreload } = options?.custom || {}
124140
for (const file of files) {
141+
if (!(
142+
wantPrefetch === true || wantPreload === true ||
143+
(wantPrefetch === undefined && wantPreload === undefined)
144+
))
145+
continue
146+
125147
const ext = extname(file)
126148
tags.push({
127149
tag: 'link',
@@ -136,7 +158,17 @@ function generateVitepressBundle(
136158
})
137159
}
138160

139-
for (const tag of tags) {
161+
let tagsReturned = tags
162+
if (options?.custom?.linkFilter) {
163+
const newTags: object[] | boolean = options?.custom?.linkFilter(tags)
164+
if (Array.isArray(newTags)) {
165+
tagsReturned = newTags
166+
} else {
167+
tagsReturned = newTags ? tags : []
168+
}
169+
}
170+
171+
for (const tag of tagsReturned) {
140172
vitepressConfig?.site?.head?.push([
141173
tag.tag,
142174
tag.attrs?.onload === 'this.rel=\'stylesheet\''

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ export interface CustomFonts {
9797
*/
9898
prefetch?: boolean
9999
prefetchPrefix?: string
100+
/**
101+
* Provides a hook for filtering which `<link>` tags should be actually
102+
* generated.
103+
* @default true
104+
*/
105+
linkFilter?: (tags: object[]) => object[] | boolean
106+
100107
/**
101108
* @default: 'head-prepend'
102109
*/

0 commit comments

Comments
 (0)