-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
fonts.$id.install.tsx
81 lines (67 loc) · 2.33 KB
/
fonts.$id.install.tsx
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
import type { LoaderFunctionArgs, MetaFunction } from '@remix-run/node';
import { json } from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import invariant from 'tiny-invariant';
import { Install } from '@/components/preview/Install';
import { TabsWrapper } from '@/components/preview/Tabs';
import { ogMeta } from '@/utils/meta';
import { getMetadata, getStats, getVariable } from '@/utils/metadata.server';
import type { Metadata, VariableData } from '@/utils/types';
interface FontMetadata {
metadata: Metadata;
variable?: VariableData;
downloadCount: number;
}
export const loader = async ({ params }: LoaderFunctionArgs) => {
const { id } = params;
invariant(id, 'Missing font ID!');
const metadata = await getMetadata(id);
const [variable, stats] = await Promise.all([
metadata.variable ? getVariable(id) : undefined,
getStats(id),
]);
const res: FontMetadata = {
metadata,
variable,
downloadCount: stats.total.npmDownloadTotal,
};
return json(res, {
headers: {
'Cache-Control': 'public, max-age=300',
},
});
};
const generateDescription = (metadata: Metadata) => {
const { family, category, weights, styles, variable } = metadata;
const weightDesc =
weights.length > 1
? `weights ranging from ${weights[0]} to ${weights.at(-1)}`
: `a single weight of ${weights[0]}`;
const italicDesc = styles.includes('italic')
? ' including italic variants'
: '';
const variableDesc = variable ? 'variable ' : '';
return `The ${family} ${variableDesc}font family is a versatile ${category} web typeface offering ${weightDesc}${italicDesc} for free. Download and self-host via an NPM package for performance and privacy, enhancing your website's typography and user experience.`;
};
export const meta: MetaFunction<typeof loader> = ({ data }) => {
const title = data?.metadata.family
? `${data.metadata.family} | Install | Fontsource`
: undefined;
const description = data?.metadata
? generateDescription(data.metadata)
: undefined;
return ogMeta({ title, description });
};
export default function InstallPage() {
const data = useLoaderData<FontMetadata>();
const { metadata, variable, downloadCount } = data;
return (
<TabsWrapper metadata={metadata} tabsValue="install">
<Install
metadata={metadata}
variable={variable}
downloadCount={downloadCount}
/>
</TabsWrapper>
);
}