Skip to content

Commit

Permalink
fix(font): fix missing fontaine fallback family
Browse files Browse the repository at this point in the history
  • Loading branch information
ThornWalli committed Sep 13, 2023
1 parent e2b49ab commit c793fd5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
15 changes: 11 additions & 4 deletions src/runtime/classes/Font.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export default class Font {
return btoa(JSON.stringify(data));
}

getCSSText() {
getCSSText({ usedFontaine = false } = {}) {
const selector = createSelector(this.rootSelector, this.selector);
const family = `"${this.family}"`;
const family = prepareFamily(this.family, { usedFontaine });
return wrapByMedia(
`${selector} {
font-family: ${this.fallbackFamily.join(', ')};
Expand All @@ -64,9 +64,9 @@ export default class Font {
);
}

getNoScriptCSSText() {
getNoScriptCSSText({ usedFontaine = false } = {}) {
const selector = createSelector(this.rootSelector, this.selector);
const family = `"${this.family}"`;
const family = prepareFamily(this.family, { usedFontaine });
return wrapByMedia(
`${selector} {
font-family: ${[family].concat(this.fallbackFamily).join(', ')};
Expand Down Expand Up @@ -122,3 +122,10 @@ function weightNormalize(weight) {
return weight;
}
}

function prepareFamily(family, { usedFontaine = false } = {}) {
if (!usedFontaine) {
return `"${family}"`;
}
return `"${family}", "${family} fallback"`;
}
10 changes: 6 additions & 4 deletions src/runtime/classes/FontCollection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ export default class FontCollection {
);
}

getStyleDescriptions() {
getStyleDescriptions(options) {
return getRelevantDescriptions([
getStyleDescription(this.list.map(font => font.getCSSText()).join(' '))
getStyleDescription(
this.list.map(font => font.getCSSText(options)).join(' ')
)
]);
}

getNoScriptStyleDescriptions() {
getNoScriptStyleDescriptions(options) {
return getRelevantDescriptions([
getStyleDescription(
this.list.map(font => font.getNoScriptCSSText()).join(' '),
this.list.map(font => font.getNoScriptCSSText(options)).join(' '),
true
)
]);
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/composables/fonts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function useFonts(context) {

const fontCollection = reactive(new FontCollection());

writeHead(isCritical, fontCollection);
writeHead(isCritical, fontCollection, runtimeConfig);

return {
isCritical,
Expand All @@ -30,16 +30,17 @@ export default function useFonts(context) {
};
}

function writeHead(isCritical, fontCollection) {
function writeHead(isCritical, fontCollection, runtimeConfig) {
const options = { usedFontaine: runtimeConfig.usedFontaine };
useHead({
link: computed(() => {
return fontCollection.getPreloadDescriptions(isCritical.value);
}),
style: computed(() => {
return fontCollection.getStyleDescriptions();
return fontCollection.getStyleDescriptions(options);
}),
noscript: computed(() => {
return fontCollection.getNoScriptStyleDescriptions();
return fontCollection.getNoScriptStyleDescriptions(options);
})
});
}
3 changes: 2 additions & 1 deletion src/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export function isViteBuild(nuxt) {
export const setPublicRuntimeConfig = (nuxt, options) => {
nuxt.options.runtimeConfig.public.speedkit = {
lazyOffsetComponent: options.lazyOffset.component,
lazyOffsetAsset: options.lazyOffset.asset
lazyOffsetAsset: options.lazyOffset.asset,
usedFontaine: !options.disableNuxtFontaine
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/utils/options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function getDefaultOptions() {
debug: false,

disableNuxtCritters: true, // If set, `@nuxtjs/critters` will not be integrated.
disableNuxtFontaine: true, // If set, `@nuxtjs/fontaine` will not be integrated.
disableNuxtFontaine: false, // If set, `@nuxtjs/fontaine` will not be integrated.
disableNuxtImage: false, // If set, `@nuxt/image` will not be integrated.

optimizePreloads: true,
Expand Down

0 comments on commit c793fd5

Please sign in to comment.