Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rishi-raj-jain/astro-font

`astro-font` will automatically optimize your Custom Fonts, Local Fonts, Fonts over any CDN and Google fonts for performance.
https://github.com/rishi-raj-jain/astro-font

astro css font fontkit google-fonts tsx

Last synced: 13 days ago
JSON representation

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

Awesome Lists containing this project

README

        

![imgonline-com-ua-compressed-gtwH4Klu9j](https://github.com/rishi-raj-jain/astro-font/assets/46300090/6a9588d0-50b9-4ade-91ee-76c7a201e1a6)

# Astro Font Optimization

`astro-font` will 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](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts).

## Installation

```bash
npm install astro-font
## or yarn
yarn add astro-font
## or pnpm
pnpm add astro-font
```

## 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

```astro
---
import { AstroFont } from "astro-font";
---

```

### Pick fonts from Google Fonts URL

```astro
---
import { AstroFont } from "astro-font";
---

```

## Local Fonts

```astro
---
import { join } from "node:path";
import { AstroFont } from "astro-font";
---

```

## 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:

```astro
---
import { join } from "node:path";
import { AstroFont } from "astro-font";
---

span",
fallback: "serif",
},
]}
/>

```

## Configuring CSS Classes

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

```astro
---
import { join } from "node:path"
---

span",
},
]}
/>
```

## Configuring CSS Variables

The `cssVariable` attribute per config object can be used to configure which CSS variable will reflect in your document's `:root` CSS selector (automatically including the references to fallback fonts CSS).

```astro
---
import { join } from "node:path"
---

```

## Configuring Fallback Font Name

The `fallbackName` attribute per config object can be used to configure the fallback font's family name (in CSS).

```astro
---
import { join } from "node:path"
---

```

### With Cloudflare Workers

`astro-font` uses the following node imports:

- `node:path`
- `node:buffer`

#### Step 1. Enable nodejs_compat

To make sure that it works in Cloudflare Workers, please enable the `node_compatibiliy` flag per the guide https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-with-workers.

If the above guide fails to work, go to your **Cloudflare project > Settings > Functions > Compatibility flags** and add the flag (as follows).

Screenshot 2024-03-21 at 7 39 51 AM

#### Step 2. Opt out of bundling Node.js built-ins

Per [Astro + Cloudflare docs](https://docs.astro.build/en/guides/integrations-guide/cloudflare/#nodejs-compatibility), you'd need to modify the vite configuration to allow for the node:* import syntax:

```diff
// File: astro.config.mjs

import { defineConfig } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';

// https://astro.build/config
export default defineConfig({
output: 'server',
adapter: cloudflare(),
+ vite: {
+ ssr: {
+ external: ["buffer", "path", "fs", "os", "crypto", "async_hooks"].map((i) => `node:${i}`),
+ },
+ // make sure to use node:fs, node:path, or node:os if you are using it in your project instead of fs, path or os
+ },
});
```

### Step 3. Use local fonts over CDN in case of Astro Server-Side Rendering with Cloudflare

As there's no access to the files in the `public` directory in the Cloudflare Pages (server-side render) context, you'd want to use fonts over CDN. To make the developer experience painless, use the following code snippet while setting up local fonts with Cloudflare in Astro:

```astro
---
import { join } from "node:path";
import { AstroFont } from "astro-font";

const fontPrefix = import.meta.env.PROD
? Astro.site.toString()
: join(process.cwd(), "public");
---

```

## Contributing

We love our contributors! Here's how you can contribute:

- [Open an issue](https://github.com/rishi-raj-jain/astro-font/issues) if you believe you've encountered a bug.
- Make a [pull request](https://github.com/rishi-raj-jain/astro-font/pull) to add new features/make quality-of-life improvements/fix bugs.



## Repo Activity

![`astro-font` repo activity – generated by Axiom](https://repobeats.axiom.co/api/embed/4730ca667c1a22348ac5d723d01f27ef3c7dce9a.svg "Repobeats analytics image")