Commit 2210d60

HPCesia <me@hpcesia.com>
2025-02-09 15:39:08
feat: favicon
1 parent 0a04480
public/favicon/favicon-128x128.png
Binary file
public/favicon/favicon-180x180.png
Binary file
public/favicon/favicon-192x192.png
Binary file
public/favicon/favicon-32x32.png
Binary file
src/constants/icon.ts
@@ -0,0 +1,20 @@
+import type { Favicon } from '@/types/config';
+
+export const defaultFavicons: Favicon[] = [
+  {
+    src: '/favicon/favicon-32x32.png',
+    sizes: '32x32',
+  },
+  {
+    src: '/favicon/favicon-128x128.png',
+    sizes: '128x128',
+  },
+  {
+    src: '/favicon/favicon-180x180.png',
+    sizes: '180x180',
+  },
+  {
+    src: '/favicon/favicon-192x192.png',
+    sizes: '192x192',
+  },
+];
src/layouts/GlobalLayout.astro
@@ -1,6 +1,8 @@
 ---
 import { profileConfig, siteConfig } from '@/config';
 import '@/styles/global.css';
+import type { Favicon } from '@/types/config';
+import { defaultFavicons } from '@constants/icon';
 import { ClientRouter } from 'astro:transitions';
 
 interface Props {
@@ -18,6 +20,9 @@ else
 
 if (!lang) lang = `${siteConfig.lang}`;
 const siteLang = lang.replace('_', '-');
+
+const favicons: Favicon[] =
+  siteConfig.favicon.length > 0 ? siteConfig.favicon : defaultFavicons;
 ---
 
 <!doctype html>
@@ -45,6 +50,20 @@ const siteLang = lang.replace('_', '-');
 
     <meta name="generator" content={Astro.generator} />
 
+    {
+      favicons.map((favicon) => (
+        <link
+          rel="icon"
+          href={
+            favicon.src.startsWith('/')
+              ? ['', import.meta.env.BASE_URL, favicon.src].join('/').replace(/\/+/g, '/')
+              : favicon.src
+          }
+          sizes={favicon.sizes}
+        />
+      ))
+    }
+
     <slot name="head" />
   </head>
   <body class="bg-base-100 text-base-content flex min-h-screen flex-col">
src/types/config.ts
@@ -1,5 +1,20 @@
 import type I18nKey from '@i18n/I18nKey';
 
+export type Favicon = {
+  /**
+   * The URL of the favicon.
+   *
+   * favicon 的 URL。
+   */
+  src: string;
+  /**
+   * The sizes of the favicon.
+   *
+   * favicon 的尺寸。
+   */
+  sizes?: `${string}x${string}`;
+};
+
 export type ButtonSubConfig<T extends string> = T extends 'text'
   ? {
       /**
@@ -94,7 +109,7 @@ export type SiteConfig = {
    *
    * 站点的 favicon。
    */
-  favicon: string[];
+  favicon: Favicon[];
   /**
    * The number of posts displayed per page.
    *
src/config.ts
@@ -16,7 +16,10 @@ export const siteConfig: SiteConfig = {
   title: 'Astral Halo',
   subtitle: '',
   lang: 'en', // "en" | "zh_CN" | "zh_TW"
-  favicon: [''],
+  favicon: [
+    // Leave this array empty to use the default favicon.
+    // 留空数组以使用默认的 favicon。
+  ],
   postsPerPage: 10,
 };