Git Product home page Git Product logo

astro-fontaine's Introduction

astro-fontaine

This is an Astro plugin that will automatically generate fallback fonts for the web fonts in your site to prevent CLS using the wonderful fontaine library.

It does the following:

  1. Downloads provided font stylesheets.
  2. Parses and extracts all @font-face definitions.
  3. Downloads the fonts to your site's public directory and rewrites the stylesheet to use the fonts hosted on your site's domain.
  4. Generates fallback fonts.
  5. Inlines the rewritten font stylesheets and fallback @font-face declarations into all pages in your site's build.

Usage

Add this code to your astro.config.js:

import { defineConfig } from 'astro/config'
import fontaine from 'astro-fontaine'

export default defineConfig({
  integrations: [
    fontaine({
      // If you are using Google Fonts, Typekit or some other font hosting service, you can provide the URL to the
      // stylesheet here and the plugin will download and inline it into your webpages automatically.
      remoteFontFaceStylesheetURLs: [
        'https://fonts.googleapis.com/css2?family=Arvo:ital,wght@0,400;0,700;1,400;1,700&family=Fira+Code&family=Lato:ital,wght@0,400;0,700;0,900;1,400;1,700;1,900&display=fallback',
      ],
      // Array of font families that will be used to generate the fallback fonts
      fonts: [
        {
          family: 'Arvo',
          // You can provide fallbacks per fronts such that the fallback font is of the same style when it flashes.
          fallbacks: ['Georgia', 'Cambria'],
        },
        // If you omit fallbacks, the defaultFallbacks property will be used.
        { family: 'Lato' },
        {
          family: 'Fira Code',
          fallbacks: [
            'SFMono-Regular',
            'Menlo',
            'Monaco',
            'Consolas',
            'Liberation Mono',
            'Courier New',
            'monospace',
          ],
        },
      ],
      defaultFallbacks: [
        'ui-sans-serif',
        'Helvetica Neue',
        'Arial',
        'sans-serif',
      ],
    }),
  ]
})

Inspiration

This library borrows heavily from the following libraries and would not exist without them:

astro-fontaine's People

Contributors

eligundry avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.