-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFavicon.astro
More file actions
336 lines (248 loc) · 6.01 KB
/
Favicon.astro
File metadata and controls
336 lines (248 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
---
/**
* @module Favicon
*
* Favicon component for injecting complete favicon HTML into Astro pages
* Includes all meta tags, manifest links, and optional cache busting
* @see {@link https://realfavicongenerator.net/ Real Favicon Generator}
*/
import { BustUrl } from "../Function/BustUrl.js";
import type {
Settings,
TouchSettings,
WebAppManifestSettings,
} from "../Interface/Option.js";
/**
* Component props for Favicon
*/
interface Props {
/**
* The base path where favicon files are located.
* Default: "/"
*/
Path?: string;
/**
* Favicon generation settings (optional, defaults to sensible defaults).
*/
Settings?: Settings | boolean;
/**
* Whether to include all meta tags (default: true)
*/
Meta?: boolean;
/**
* Application title for mobile web app
*/
AppTitle?: string;
/**
* Whether to bust cache on all favicon URLs (default: true)
*/
BustURL?: boolean;
}
const {
Path = "/",
Settings,
Meta = true,
AppTitle,
BustURL = true,
} = Astro.props;
/**
* Get settings object if valid
*/
const GetSettingsObject = (): Settings | null =>
Settings && typeof Settings === "object" ? Settings : null;
/**
* Get touch settings from Settings
*/
const GetTouchSettings = (): TouchSettings | null => {
const SettingsObject = GetSettingsObject();
if (!SettingsObject) return null;
const Touch = SettingsObject.Touch;
if (!Touch) return null;
return typeof Touch === "boolean" ? null : Touch;
};
/**
* Get WebAppManifest settings from Settings
*/
const GetWebAppManifestSettings = (): WebAppManifestSettings | null => {
const SettingsObject = GetSettingsObject();
if (!SettingsObject) return null;
const WebAppManifest = SettingsObject.WebAppManifest;
if (!WebAppManifest) return null;
return typeof WebAppManifest === "boolean" ? null : WebAppManifest;
};
/**
* Get theme color from settings or default
*/
const GetThemeColor = (): string => {
const WebAppManifestSettings = GetWebAppManifestSettings();
return WebAppManifestSettings?.ThemeColor ?? "#eaeaea";
};
/**
* Get favicon name for mobile apps
*/
const GetFaviconName = (): string => {
const WebAppManifestSettings = GetWebAppManifestSettings();
return AppTitle ?? WebAppManifestSettings?.Name ?? "";
};
/**
* Get touch app title
*/
const GetTouchAppTitle = (): string => {
const TouchSettings = GetTouchSettings();
const FaviconName = GetFaviconName();
return TouchSettings?.AppTitle ?? FaviconName;
};
/**
* Bust URL if enabled
*/
const BustedUrl = (Url: string): string => (BustURL ? BustUrl(Url) : Url);
/**
* Create link HTML element
*/
const CreateLink = (
Rel: string,
Href: string,
Type?: string,
Sizes?: string,
CrossOrigin?: string,
): string => {
const Attributes: string[] = [`rel="${Rel}"`, `href="${BustedUrl(Href)}"`];
if (Type) {
Attributes.push(`type="${Type}"`);
}
if (Sizes) {
Attributes.push(`sizes="${Sizes}"`);
}
if (CrossOrigin) {
Attributes.push(`crossorigin="${CrossOrigin}"`);
}
return `<link ${Attributes.join(" ")} />`;
};
/**
* Create meta HTML element
*/
const CreateMeta = (Name: string, Content: string): string =>
`<meta name="${Name}" content="${Content}" />`;
/**
* Get theme color meta tags
*/
const GetThemeColorMetaTags = (): string[] => {
const ThemeColor = GetThemeColor();
const Tags: string[] = [];
if (ThemeColor) {
Tags.push(CreateMeta("theme-color", ThemeColor));
}
return Tags;
};
/**
* Get mobile web app meta tags
*/
const GetMobileWebAppMetaTags = (): string[] => {
const Tags: string[] = [
CreateMeta("mobile-web-app-capable", "yes"),
CreateMeta("apple-mobile-web-app-capable", "yes"),
CreateMeta("apple-mobile-web-app-status-bar-style", "default"),
];
const TouchAppTitle = GetTouchAppTitle();
if (TouchAppTitle) {
Tags.push(CreateMeta("apple-mobile-web-app-title", TouchAppTitle));
}
return Tags;
};
/**
* Get Microsoft application meta tags
*/
const GetMicrosoftMetaTags = (): string[] => {
const ThemeColor = GetThemeColor();
const Tags: string[] = [];
if (ThemeColor) {
Tags.push(CreateMeta("msapplication-TileColor", ThemeColor));
Tags.push(
CreateMeta(
"msapplication-TileImage",
BustedUrl(`${Path}mstile-144x144.png`),
),
);
Tags.push(
CreateMeta(
"msapplication-config",
BustedUrl(`${Path}browserconfig.xml`),
),
);
}
return Tags;
};
/**
* Get all favicon link elements
*/
const GetFaviconLinks = (): string[] => {
const Links: string[] = [];
// Standard favicon links
Links.push(
CreateLink("icon", `${Path}favicon-96x96.png`, "image/png", "96x96"),
);
Links.push(CreateLink("icon", `${Path}favicon.svg`, "image/svg+xml"));
Links.push(CreateLink("shortcut icon", `${Path}favicon.ico`));
// Apple touch icon
Links.push(
CreateLink(
"apple-touch-icon",
`${Path}apple-touch-icon.png`,
undefined,
"180x180",
),
);
// Apple touch icon precomposed (for iOS < 6)
Links.push(
CreateLink(
"apple-touch-icon-precomposed",
`${Path}apple-touch-icon.png`,
undefined,
"180x180",
),
);
return Links;
};
/**
* Get all meta tags
*/
const GetAllMetaTags = (): string[] => {
if (!Meta) return [];
return [
...GetThemeColorMetaTags(),
...GetMobileWebAppMetaTags(),
...GetMicrosoftMetaTags(),
];
};
/**
* Get manifest link
*/
const GetManifestLink = (): string => {
const WebAppManifestSettings = GetWebAppManifestSettings();
// Use WebAppManifest settings path or default to Manifest.json
const ManifestPath =
WebAppManifestSettings &&
typeof WebAppManifestSettings === "object" &&
"Name" in WebAppManifestSettings
? `${Path}manifest.json`
: "/Manifest.json";
return CreateLink(
"manifest",
ManifestPath,
undefined,
undefined,
"use-credentials",
);
};
const FaviconLinks = GetFaviconLinks();
const MetaTags = GetAllMetaTags();
const ManifestLink = GetManifestLink();
---
<>
<!-- Favicon Links -->
{FaviconLinks.map((Link) => <Fragment set:html={Link} />)}
<!-- Web App Manifest -->
<Fragment set:html={ManifestLink} />
<!-- Favicon Meta Tags -->
{MetaTags.map((Tag) => <Fragment set:html={Tag} />)}
</>