Table of Contents
436 words
3 minutes
Tutorial: Deploying Chinese Fonts and Compressing with Font-Spider
gemini-aiAI Summary
Gemini 2.5 Pro

I guarantee this is the most useful tutorial I’ve made—it’s dummy-proof. If you don’t understand, check GitHub.#

Chinese fonts are usually massive (several MBs), and using them directly on a webpage leads to slow loading and a poor user experience. This article introduces how to use Font-Spider to subset and compress Chinese fonts, keeping only the characters actually used on your site, reducing the size to just a few dozen KB.

Actually, I have another goal: to fix the mess of Font-Spider tutorials online. Most don’t teach the --map parameter, use manual HTML, or only work for static sites with no generators. I have to ask: can those actually be used in a real project?#

Note: This tutorial is based on Fuwari. If you don’t use Fuwari, you can still use it as a reference. The scripts are written in Node.js. Replace MiSans-Regular with your own font!

Preparation: Understanding Font-Spider#

Font-Spider hasn’t been updated in a while, so ignoring pnpm warnings about outdated dependencies is fine.

This tool scans specified files/folders (usually HTML), extracts @font-face tags from CSS, identifies the font, and based on the usage scope, extracts all characters and repacks the font.

The main issue is: what about web paths?

For example, I need to specify the font path as /MiSans-Regular.ttf so all pages can find it, but Font-Spider doesn’t recognize this and tries to find the font at the system root.

In this case, we need the --map parameter. The format is --map "original_path,replacement_path", such as --map "/foobar.ttf,dist/foobar.ttf". This solves the problem.

Step 1: Prepare Font Files#

First, prepare your font file (.ttf format). To make it accessible after Astro builds the site, put it in the public directory.

/public
├── MiSans-Regular.ttf
└── ...

Then, create src/configs/font.ts to manage font configuration:

export const fontConfig = {
enable: true,
fonts: [
{
name: "MiSans-Regular",
src: "/MiSans-Regular.ttf",
type: "truetype",
weight: "normal",
style: "normal",
display: "swap",
},
],
family: "'MiSans-Regular', sans-serif",
};

Step 2: Add Font-Spider Dependency#

Install font-spider as a dev dependency:

Terminal window
pnpm add -D font-spider

Step 3: Configure Global Styles (Critical)#

For Font-Spider to recognize font usage, you must explicitly declare @font-face and apply font-family.

In Astro, use <style is:global> in your layout file (e.g., src/layouts/Layout.astro). The is:global attribute ensures the style applies to the body and can be crawled by Font-Spider.

<style is:global set:html={`
${fontConfig.enable ? fontConfig.fonts.map((font) => `
@font-face {
font-family: '${font.name}';
src: url('${font.src}') format('${font.type}');
font-weight: ${font.weight};
font-style: ${font.style};
font-display: ${font.display};
}
`).join('') : ''}
body {
font-family: ${fontConfig.family};
}
`}></style>

Step 4: Write Automation Script#

Create scripts/compress-font.js to automatically run Font-Spider after building:

(Refer to the original post for the full script content).

Finally, update package.json:

"scripts": {
"build": "astro build && node scripts/compress-font.js"
}

Now, every time you run pnpm build, your fonts will be automatically optimized!

Tutorial: Deploying Chinese Fonts and Compressing with Font-Spider
https://wtada233.top/en/posts/font-deployment-tutorial/
Author
Wtada233
Published at
2025-12-23
License
CC BY-NC-SA 4.0

Last modified on 2025-12-23

Webmentions

0 Comments

Cover
Cover
0:00 / 0:00