Merge pull request #30 from rishi-raj-jain/main

feat: astro-font - optimize fonts & prevent CLS
This commit is contained in:
Somrat
2024-01-27 10:00:30 +06:00
committed by GitHub
3 changed files with 40 additions and 19 deletions
+2 -1
View File
@@ -21,6 +21,7 @@
"@astrojs/tailwind": "^5.1.0",
"astro": "^4.0.9",
"astro-auto-import": "^0.4.2",
"astro-font": "^0.0.77",
"date-fns": "^3.0.6",
"disqus-react": "^1.1.5",
"github-slugger": "^2.0.0",
@@ -52,8 +53,8 @@
"prettier-plugin-astro": "^0.12.3",
"prettier-plugin-tailwindcss": "^0.5.10",
"sass": "^1.69.7",
"tailwindcss": "^3.4.0",
"tailwind-bootstrap-grid": "^5.1.0",
"tailwindcss": "^3.4.0",
"typescript": "5.3.3"
}
}
+34 -9
View File
@@ -1,4 +1,5 @@
---
import { AstroFont } from "astro-font";
import TwSizeIndicator from "@/components/TwSizeIndicator.astro";
import config from "@/config/config.json";
import theme from "@/config/theme.json";
@@ -13,6 +14,18 @@ import SearchModal from "./helpers/SearchModal";
const pf = theme.fonts.font_family.primary;
const sf = theme.fonts.font_family.secondary;
let fontPrimary, fontSecondary;
if (theme.fonts.font_family.primary) {
fontPrimary = theme.fonts.font_family.primary
.replace(/\+/g, " ")
.replace(/:[ital,]*[ital@]*[wght@]*[0-9,;]+/gi, "");
}
if (theme.fonts.font_family.secondary) {
fontSecondary = theme.fonts.font_family.secondary
.replace(/\+/g, " ")
.replace(/:[ital,]*[ital@]*[wght@]*[0-9,;]+/gi, "");
}
// types for frontmatters
export interface Props {
title?: string;
@@ -50,15 +63,27 @@ const { title, meta_title, description, image, noindex, canonical } =
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- google font css -->
<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=${pf}${
sf ? "&family=" + sf : ""
}&display=swap`}
rel="preload"
as="style"
onload="this.rel='stylesheet'"
<AstroFont
config={[
{
src: [],
preload: false,
display: "swap",
name: fontPrimary,
fallback: "sans-serif",
cssVariable: "font-primary",
googleFontsURL: `https://fonts.googleapis.com/css2?family=${pf}&display=swap`,
},
{
src: [],
preload: false,
display: "swap",
name: fontSecondary,
fallback: "sans-serif",
cssVariable: "font-secondary",
googleFontsURL: `https://fonts.googleapis.com/css2?family=${sf}&display=swap`,
},
]}
/>
<!-- responsive meta -->
+4 -9
View File
@@ -8,17 +8,12 @@ let h4 = h5 * font_scale;
let h3 = h4 * font_scale;
let h2 = h3 * font_scale;
let h1 = h2 * font_scale;
let fontPrimary, fontPrimaryType, fontSecondary, fontSecondaryType;
let fontPrimaryType, fontSecondaryType;
if (theme.fonts.font_family.primary) {
fontPrimary = theme.fonts.font_family.primary
.replace(/\+/g, " ")
.replace(/:[ital,]*[ital@]*[wght@]*[0-9,;]+/gi, "");
fontPrimaryType = theme.fonts.font_family.primary_type;
}
if (theme.fonts.font_family.secondary) {
fontSecondary = theme.fonts.font_family.secondary
.replace(/\+/g, " ")
.replace(/:[ital,]*[ital@]*[wght@]*[0-9,;]+/gi, "");
fontSecondaryType = theme.fonts.font_family.secondary_type;
}
@@ -75,8 +70,8 @@ module.exports = {
h6: h6 + "rem",
},
fontFamily: {
primary: [fontPrimary, fontPrimaryType],
secondary: [fontSecondary, fontSecondaryType],
primary: ["var(--font-primary)", fontPrimaryType],
secondary: ["var(--font-secondary)", fontSecondaryType],
},
},
},