-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(v-font): new mechanics to write fonts in header
- Loading branch information
1 parent
0ac46c8
commit 8937852
Showing
15 changed files
with
194 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
title: 鈿狅笍 useBoosterHead | ||
--- | ||
|
||
# {{$frontmatter.title}} | ||
|
||
::: warning | ||
|
||
`useBoosterhead` is an internally used composable that is called once in the plugin. | ||
|
||
Is required to create and manage a head entry. | ||
|
||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { logDebug } from '../utils/log'; | ||
|
||
export default function () { | ||
const head = injectHead(); | ||
const nuxtApp = useNuxtApp(); | ||
|
||
const { debug } = useRuntimeConfig().public.booster; | ||
const collection = ref(new FontsCollection()); | ||
|
||
let headEntry; | ||
watch( | ||
() => collection.value, | ||
() => { | ||
if (headEntry) { | ||
headEntry.patch(createEntry(collection, debug)); | ||
} else { | ||
headEntry = head.push({}); | ||
} | ||
} | ||
); | ||
|
||
nuxtApp.$router.afterEach(() => { | ||
nextTick(() => { | ||
collection.value = new FontsCollection( | ||
collection.value.list.filter(item => { | ||
return !dispatchCollections.includes(item); | ||
}) | ||
); | ||
dispatchCollections = []; | ||
}); | ||
}); | ||
|
||
let dispatchCollections = []; | ||
const push = (fontCollection, isCritical, options) => { | ||
if (!collection) { | ||
throw new Error('pushFontCollection must be called before setupHead'); | ||
} | ||
let value = { fontCollection, isCritical, options }; | ||
collection.value = new FontsCollection([...collection.value.list, value]); | ||
value = collection.value.list[collection.value.list.length - 1]; | ||
return { | ||
dispose: () => dispatchCollections.push(value) | ||
}; | ||
}; | ||
return { push, collection }; | ||
} | ||
|
||
const createEntry = (collection, debug) => { | ||
return () => { | ||
if (debug) { | ||
logDebug('Head Font Collections:', collection.value.toJSON()); | ||
} | ||
|
||
const items = collection.value.list.filter( | ||
({ fontCollection }) => fontCollection.size | ||
); | ||
|
||
const uniqList = items => | ||
Array.from(new Map(items.map(item => [item.key, item])).values()); | ||
|
||
return { | ||
link: uniqList( | ||
items | ||
.filter(({ fontCollection }) => fontCollection.size) | ||
.map(({ fontCollection, isCritical }) => | ||
fontCollection.getPreloadDescriptions(isCritical) | ||
) | ||
.flat() | ||
), | ||
style: uniqList( | ||
items | ||
.map(({ fontCollection, options }) => | ||
fontCollection.getStyleDescriptions(options) | ||
) | ||
.flat() | ||
), | ||
noscript: uniqList( | ||
items | ||
.map(({ fontCollection }) => | ||
fontCollection.getNoScriptStyleDescriptions() | ||
) | ||
.flat() | ||
) | ||
}; | ||
}; | ||
}; | ||
|
||
class FontsCollection { | ||
constructor(list = []) { | ||
this.list = list; | ||
} | ||
|
||
get size() { | ||
return this.list.length; | ||
} | ||
|
||
toJSON() { | ||
return { | ||
list: this.list.map(item => ({ | ||
...item, | ||
fontCollection: item.fontCollection.toJSON() | ||
})) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const logDebug = (...args) => { | ||
console.log('[DEBUG][BOOSTER]:', ...args); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters