Astro Font

Automatically optimize your Custom Fonts, Local Fonts, Fonts over any CDN a...

README

Astro Font Optimization


astro-fontwill automatically optimize your Custom Fonts, Local Fonts, Fonts over any CDN and Google fonts for performance.

The project is inspired by the Next.js Font Optimization .

Installation


  1. ``` shell
  2. npm install astro-font
  3. ## or yarn
  4. yarn add astro-font
  5. ## or pnpm
  6. pnpm add astro-font
  7. ```

Google Fonts


Automatically optimize any Google Font. To use the font in all your pages, add it to ``file in an Astro layout:

Use Google Fonts URL directly


  1. ``` astro
  2. ---
  3. import { AstroFont } from "astro-font";
  4. ---

  5. <head>
  6.     <AstroFont
  7.       config={[
  8.         {
  9.           src: [],
  10.           name: "Poppins",
  11.           // Google Fonts URL
  12.           googleFontsURL: 'https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,600;1,400;1,700&display=swap',
  13.           preload: true,
  14.           display: "swap",
  15.           selector: "body",
  16.           fallback: "sans-serif",
  17.         },
  18.       ]}
  19.     />
  20. </head>
  21. ```

Pick fonts from Google Fonts URL


  1. ``` astro
  2. ---
  3. import { AstroFont } from "astro-font";
  4. ---

  5. <head>
  6.     <AstroFont
  7.       config={[
  8.         {
  9.           name: "Afacad",
  10.           src: [
  11.             {
  12.               style: 'bold',
  13.               weight: '700',
  14.               // Picked up font URL by manually visiting Google Fonts URL
  15.               path: 'https://fonts.gstatic.com/s/afacad/v1/6NUK8FKMIQOGaw6wjYT7ZHG_zsBBfvLqagk-80KjZfJ_uw.woff2'
  16.             },
  17.           ],
  18.           preload: true,
  19.           display: "swap",
  20.           selector: "body",
  21.           fallback: "sans-serif",
  22.         },
  23.       ]}
  24.     />
  25. </head>
  26. ```

Local Fonts


  1. ``` astro
  2. ---
  3. import { join } from "node:path";
  4. import { AstroFont } from "astro-font";
  5. ---

  6. <head>
  7.     <AstroFont
  8.       config={[
  9.         {
  10.           name: "Afacad",
  11.           src: [
  12.             {
  13.               style: 'normal',
  14.               weight: '400',
  15.               path: join(process.cwd(), 'public', 'fonts', 'Afacad-Regular.ttf')
  16.             },
  17.             {
  18.               style: 'medium',
  19.               weight: '500',
  20.               path: join(process.cwd(), 'public', 'fonts', 'Afacad-Medium.ttf')
  21.             },
  22.           ],
  23.           preload: false,
  24.           display: "swap",
  25.           selector: "body",
  26.           fallback: "sans-serif",
  27.         },
  28.       ]}
  29.     />
  30. </head>
  31. ```

Using Multiple Fonts


You can import and use multiple fonts in your application. There are two approaches you can take.

Just extend the array of the config to contain the new collection of the fonts:

  1. ``` astro
  2. ---
  3. import { join } from "node:path";
  4. import { AstroFont } from "astro-font";
  5. ---

  6. <head>
  7.     <AstroFont
  8.       config={[
  9.         {
  10.           name: "Afacad",
  11.           src: [
  12.             {
  13.               style: 'bold',
  14.               weight: '700',
  15.               path: 'https://fonts.gstatic.com/s/afacad/v1/6NUK8FKMIQOGaw6wjYT7ZHG_zsBBfvLqagk-80KjZfJ_uw.woff2'
  16.             },
  17.           ],
  18.           preload: true,
  19.           display: "swap",
  20.           selector: "body",
  21.           fallback: "sans-serif",
  22.         },
  23.         {
  24.           name: "Inter",
  25.           src: [
  26.             {
  27.               weight: '400',
  28.               style: 'normal',
  29.               path: join(process.cwd(), 'public', 'fonts', 'Inter-Regular.ttf')
  30.             }
  31.           ],
  32.           preload: true,
  33.           display: "swap",
  34.           selector: "body > span",
  35.           fallback: "serif",
  36.         },
  37.       ]}
  38.     />
  39. </head>
  40. ```

Configuring CSS Classes


The selectorattribute per config object can be used to configure which CSS class will reflect the whole CSS (automatically including the references to fallback fonts CSS).

  1. ``` astro
  2. ---
  3. import { join } from "node:path"
  4. ---

  5. <AstroFont
  6.   config={[
  7.     {
  8.       name: "Afacad",
  9.       src: [
  10.         {
  11.           style: 'bold',
  12.           weight: '700',
  13.           path: 'https://fonts.gstatic.com/s/afacad/v1/6NUK8FKMIQOGaw6wjYT7ZHG_zsBBfvLqagk-80KjZfJ_uw.woff2'
  14.         },
  15.       ],
  16.       preload: true,
  17.       display: "swap",
  18.       fallback: "sans-serif",

  19.       // My Custom CSS Selector
  20.       // Type: ClassName
  21.       selector: ".custom_class",

  22.     },
  23.     {
  24.       name: "Inter",
  25.       src: [
  26.         {
  27.           weight: '400',
  28.           style: 'normal',
  29.           path: join(process.cwd(), 'public', 'fonts', 'Inter-Regular.ttf')
  30.         }
  31.       ],
  32.       preload: true,
  33.       display: "swap",
  34.       fallback: "serif",

  35.       // My Custom CSS Selector
  36.       // Type: CSS Selector
  37.       selector: "body > span",
  38.     },
  39.   ]}
  40. />
  41. ```