Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defer stats in loader #134

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions app/modules/stats/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ttl: 1000 * 60 * 60, // 1 hour
allowStale: true,
noDeleteOnFetchRejection: true,
fetchMethod: async (key) => {

Check warning on line 28 in app/modules/stats/index.tsx

View workflow job for this annotation

GitHub Actions / ⬣ Lint

'key' is defined but never used
console.log("Fetching fresh stats");
let [npmDownloads, githubContributors, githubStars, githubDependents] =
await Promise.all([
Expand All @@ -43,34 +43,32 @@
},
});

export async function getStats(): Promise<Stats[] | undefined> {
export async function getStats(): Promise<Stats[]> {
let cacheKey = "ONE_STATS_KEY_TO_RULE_THEM_ALL";
let statCounts = await statCountsCache.fetch(cacheKey);

return statCounts
? [
{
count: statCounts.npmDownloads,
label: "Downloads on npm",
svgId: "stat-download",
},
{
count: statCounts.githubContributors,
label: "Contributors on GitHub",
svgId: "stat-users",
},
{
count: statCounts.githubStars,
label: "Stars on GitHub",
svgId: "stat-star",
},
{
count: statCounts.githubDependents,
label: "Dependents on GitHub",
svgId: "stat-box",
},
]
: undefined;
return [
{
count: statCounts.npmDownloads,
label: "Downloads on npm",
svgId: "stat-download",
},
{
count: statCounts.githubContributors,
label: "Contributors on GitHub",
svgId: "stat-users",
},
{
count: statCounts.githubStars,
label: "Stars on GitHub",
svgId: "stat-star",
},
{
count: statCounts.githubDependents,
label: "Dependents on GitHub",
svgId: "stat-box",
},
];
}

/**
Expand Down
7 changes: 1 addition & 6 deletions app/pages/splash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { getStats } from "~/modules/stats";
import { getRootMatchData } from "~/ui/meta";

export let loader = async () => {
const stats = await getStats();
// replace with something better, just fixing types
if (!stats) {
throw new Error("Failed to load stats");
}
const stats = getStats();
return { stats };
};

Expand Down Expand Up @@ -153,7 +149,6 @@ export default function Home() {
<Await resolve={stats} errorElement={null}>
{(stats) => (
<ul className="mt-8 grid grid-cols-1 gap-8 md:grid md:grid-cols-2">
{/* @ts-expect-error -- these types didn't make it into RR7, this needs to be fixed */}
{stats.map(({ svgId, count, label }) => (
<li key={svgId} className="flex gap-4">
<svg
Expand Down