All Projects → feat-agency → vite-plugin-webfont-dl

feat-agency / vite-plugin-webfont-dl

Licence: MIT license
⚡ Webfont Download Vite Plugin - Make your Vite site load faster

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to vite-plugin-webfont-dl

vite-plugin-environment
Easily expose environment variables in Vite.js
Stars: ✭ 57 (-17.39%)
Mutual labels:  vite, vitejs, vite-plugin
vite-plugin-ssr
Like Next.js / Nuxt but as do-one-thing-do-it-well Vite plugin.
Stars: ✭ 1,703 (+2368.12%)
Mutual labels:  vite, vitejs, vite-plugin
templates
tsParticles website templates collection
Stars: ✭ 42 (-39.13%)
Mutual labels:  svelte, sveltejs, vuejs3
admin-one-vue-tailwind
Free Vue.js 3.x Tailwind 3.x admin dashboard template with dark mode. Vite builds. Pinia state. Laravel integration available
Stars: ✭ 742 (+975.36%)
Mutual labels:  vite, vuejs3, vitejs
electron-vue-template
A very simplistic Electron + Vue 3 template including ViteJS and Electron Builder
Stars: ✭ 60 (-13.04%)
Mutual labels:  vite, vuejs3, vitejs
Urbanist
Urbanist is a low-contrast, geometric sans-serif inspired by Modernist design and typography.
Stars: ✭ 374 (+442.03%)
Mutual labels:  webfonts, google-fonts, webfont
vite-plugins
🌱 为社区尽一份绵薄之力
Stars: ✭ 63 (-8.7%)
Mutual labels:  vite, vitejs, vite-plugin
svelte-typescript-setups
Examples and tests of different bundler setups for Svelte with Typescript and PostCSS (Tailwind)
Stars: ✭ 50 (-27.54%)
Mutual labels:  svelte, vite
vite-plugin-banner
A banner plugin for Vite. It can adds a banner to the top of each generated chunk.
Stars: ✭ 39 (-43.48%)
Mutual labels:  vite, vite-plugin
fa-svelte
Font Awesome 5 for svelte.js
Stars: ✭ 72 (+4.35%)
Mutual labels:  svelte, sveltejs
svelte-datagrid
Svelte data grid spreadsheet best best features and performance from excel
Stars: ✭ 48 (-30.43%)
Mutual labels:  svelte, sveltejs
SvelteTetris
Basic Tetris game created with Svelte
Stars: ✭ 21 (-69.57%)
Mutual labels:  svelte, sveltejs
d3-fdg-svelte
d3 Force Directed Graph example (d3-force) implemented in sveltejs. REPL:
Stars: ✭ 31 (-55.07%)
Mutual labels:  svelte, sveltejs
image-optimizer
A free and open source tool for optimizing images and vector graphics.
Stars: ✭ 740 (+972.46%)
Mutual labels:  vite, vitejs
troisjs.github.io
📖 Examples and documentation for TroisJS ✨
Stars: ✭ 119 (+72.46%)
Mutual labels:  vuejs3, vitejs
kahi-ui
Straight-forward Svelte UI for the Web
Stars: ✭ 169 (+144.93%)
Mutual labels:  svelte, sveltejs
django-vite
Integration of ViteJS in a Django project.
Stars: ✭ 201 (+191.3%)
Mutual labels:  vite, vitejs
fastify-vite
This plugin lets you load a Vite client application and set it up for Server-Side Rendering (SSR) with Fastify.
Stars: ✭ 497 (+620.29%)
Mutual labels:  svelte, sveltejs
svelte-color-picker
A color picker component for svelte
Stars: ✭ 96 (+39.13%)
Mutual labels:  svelte, sveltejs
sveltekit-blog
Sveltekit blog starter project created with sveltekit, typescript, tailwindcss, postcss, husky, and storybook. The project has the structure set up for the scaleable web application.
Stars: ✭ 100 (+44.93%)
Mutual labels:  svelte, sveltejs

🔌 Webfont DL - Vite plugin

NPM NPM downloads

⚠️ Using the standard method to add webfonts (like Google Fonts) to a webpage can slow down page load significantly. Lighthouse and PageSpeed Insights audit calls this a "render-blocking resource", which means that the page can't load until the font has been fetched from the Google Fonts server.

📈 By avoiding render-blocking resources caused by Google Fonts loading, you can boost page performance which leads to better user-experience and improves SEO results.

⚙️ The plugin downloads the given fonts from Google Fonts and dynamically injects them (internal or external CSS) into your Vite project.

💡 Webfont-DL plugin lets you leverage the flexibility of Google Fonts without trade-offs when it comes to page perfomance.


📦 Install

npm i vite-plugin-webfont-dl -D

😎 Usage [method 🅰️]: Zero config

Extracts, downloads and injects fonts from the original Google Fonts code snippet.

  1. Select your font families at Google Fonts and copy the code into <head> from the "Use on the web" block:
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@300;400&family=Roboto:wght@100&display=swap" rel="stylesheet">
  2. Add webfontDownload to your Vite plugins without any configuration and the plugin automagically will take care of everything:
    // vite.config.js
    
    import webfontDownload from 'vite-plugin-webfont-dl';
    
    export default {
      plugins: [
        webfontDownload(),
      ],
    };

🦄 Usage [method 🅱️]: Simple config

Extracts, downloads and injects fonts from the configured webfont CSS URL(s).

  1. Select your font families at Google Fonts and copy the CSS URL(s) from the "Use on the web" code block:
    <link href="[CSS URL]" rel="stylesheet">
  2. Add webfontDownload to your Vite plugins with the selected Google Fonts CSS URL(s):
    // vite.config.js
    
    import webfontDownload from 'vite-plugin-webfont-dl';
    
    export default {
      plugins: [
        webfontDownload([
          'https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap',
          'https://fonts.googleapis.com/css2?family=Fira+Code&display=swap'
        ]),
      ],
    };

🚀 That's all!

From here the fonts are injected and available globally. Plugin works seamlessly even when working on local development server.

h1 {
  font-family: 'Press Start 2P', cursive;
}

h2 {
  font-family: 'Fira Code', monospace;
}

🔮 How it works

Google Fonts 📉

Google Fonts generates the following code which you have to inject into your website's <head>, example:

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fira+Code&display=swap" rel="stylesheet">

📱 What happens on client-side with Google Fonts:

  1. First line gives a hint to the browser to begin the connection handshake (DNS, TCP, TLS) with fonts.googleapis.com. This happens in the background to improve performance. [preconnect]
  2. Second line is another preconnect hint to fonts.gstatic.com. [preconnect]
  3. Third line instructs the browser to load and use a CSS stylesheet file from fonts.googleapis.com (with font-display:swap). [stylesheet]
  4. The browser downloads the CSS file and starts to parse it. The parsed CSS is a set of @font-face definitions containing font URLs from fonts.gstatic.com server.
  5. The browser starts to download the all relevant fonts from fonts.gstatic.com.
  6. After the successful fonts download the browser swaps the fallback fonts to the downloaded ones.

🆚

Webfont-DL Vite plugin 📈

On the contrary, Webfont-DL plugin does most of the job at build time, leaves the minimum to the browser.

Webfont-DL plugin downloads the Google Fonts CSS file(s), extracts the font URLs, downloads the fonts, generates an embedded CSS (<style> tag) or a webfont / external CSS file, add them to the bundle and injects the following code into your website's <head> using a non-render blocking method, example:

<style>
  @font-face {
    font-family: 'Fira Code';
    font-style: normal;
    font-weight: 300;
    font-display: swap;
    src: url(/assets/uU9eCBsR6Z2vfE9aq3bL0fxyUs4tcw4W_GNsJV37Nv7g.9c348768.woff2) format('woff2');
    unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
  }
  ...
</style>

or (using dev server or injectAsStyleTag: false option)

<link rel="preload" as="style" href="/assets/webfonts.b904bd45.css">
<link rel="stylesheet" media="print" onload="this.onload=null;this.removeAttribute('media');" href="/assets/webfonts.b904bd45.css">

📱 What happens on client-side with Webfont-DL plugin:

  1. Load fonts from the embedded CSS (<style> tag).

or

  1. First line instructs the browser to prefetch a CSS file for later use as stylesheet. [preload]
  2. Second line instructs the browser to load and use that CSS file as a "print" stylesheet (non-render blocking). After loading it promote to "all" media type stylesheet (by removing the "media" attribute). [stylesheet]

🛠️ Options

injectAsStyleTag: inject webfonts as <style> tag (embedded CSS) or as an external .css file

async: prevent the usage of inline event handlers that can cause Content Security Policy issues (has no effect when injectAsStyleTag is true)

usage:

ViteWebfontDownload(
  [],
  {
    injectAsStyleTag: true,
    async: true
  }
)

or:

ViteWebfontDownload(
  [
    'https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap'
  ],
  {
    injectAsStyleTag: true,
    async: true
  }
)

📈 Benchmark

Starter Vite project with

▶️ Standard Google Fonts 🆚 ▶️ Webfont DL Vite plugin
🔗 webfont.feat.agency 🔗 webfont-dl.feat.agency

Compare


📚 Resources


📄 License

MIT License © 2022 feat.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].