Skip to content

Commit

Permalink
feat: add fontBaseUrl and preconnectUrl on google font provider
Browse files Browse the repository at this point in the history
close #78
  • Loading branch information
stafyniaksacha committed Dec 1, 2024
1 parent 4bbe9c1 commit 12684bc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ Unfonts({
*/
preconnect: false,

/**
* allow preconnect to be customized
* default: 'https://fonts.gstatic.com'
*/
preconnectUrl: 'https://fonts.gstatic.com',

/**
* customizes the base URL for the font request
* default: 'https://fonts.googleapis.com/css2'
*/
fontBaseUrl: 'https://fonts.googleapis.com/css2',

/**
* values: auto, block, swap(default), fallback, optional
* default: 'swap'
Expand Down
42 changes: 23 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/loaders/google-fonts.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { HtmlTagDescriptor } from 'vite'
import type { GoogleFonts } from '../types'

const GoogleFontsBase = 'https://fonts.googleapis.com/css2'
const GStaticBase = 'https://fonts.gstatic.com/'

export function googleLoader({
families,
text,
preconnect = true,
display = 'swap',
injectTo = 'head-prepend',
fontBaseUrl = 'https://fonts.googleapis.com/css2',
preconnectUrl = 'https://fonts.gstatic.com/',
}: GoogleFonts): HtmlTagDescriptor[] {
const specs: string[] = []
const deferedSpecs: string[] = []
Expand Down Expand Up @@ -61,7 +60,7 @@ export function googleLoader({
injectTo,
attrs: {
rel: 'preconnect',
href: GStaticBase,
href: preconnectUrl,
crossorigin: 'anonymous',
},
})
Expand All @@ -70,7 +69,7 @@ export function googleLoader({
// defer loading font-faces definitions
// @see https://web.dev/optimize-lcp/#defer-non-critical-css
if (deferedSpecs.length > 0) {
let href = `${GoogleFontsBase}?family=${deferedSpecs.join('&family=')}`
let href = `${fontBaseUrl}?family=${deferedSpecs.join('&family=')}`

if (typeof display === 'string' && display !== 'auto')
href += `&display=${display}`
Expand All @@ -91,7 +90,7 @@ export function googleLoader({

// load critical fonts
if (specs.length > 0) {
let href = `${GoogleFontsBase}?family=${specs.join('&family=')}`
let href = `${fontBaseUrl}?family=${specs.join('&family=')}`

if (typeof display === 'string' && display !== 'auto')
href += `&display=${display}`
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ export interface GoogleFonts {
* @default: 'head-prepend'
*/
injectTo?: 'head' | 'body' | 'head-prepend' | 'body-prepend'
/**
* @default: 'https://fonts.googleapis.com/css2'
*/
fontBaseUrl?: string
/**
* @default: 'https://fonts.gstatic.com/'
*/
preconnectUrl?: string
}

export interface TypeKitFonts {
Expand Down

0 comments on commit 12684bc

Please sign in to comment.