Skip to content

Commit 2461d8d

Browse files
committed
chore: Offload dependents crawling to separate service
1 parent 11ebb01 commit 2461d8d

File tree

4 files changed

+35
-124
lines changed

4 files changed

+35
-124
lines changed

packages/docs/src/app/(pages)/_landing/dependents/dependents.tsx packages/docs/src/app/(pages)/_landing/dependents.tsx

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
import { cn } from '@/src/lib/utils'
22
import Image from 'next/image'
3-
import { Result, crawlDependents } from './crawler'
3+
import { z } from 'zod'
4+
5+
const dependentSchema = z.object({
6+
stars: z.number(),
7+
owner: z.string(),
8+
name: z.string(),
9+
pkg: z.string(),
10+
avatarURL: z.string(),
11+
createdAt: z.string().transform(date => new Date(date))
12+
})
13+
type Dependent = z.infer<typeof dependentSchema>
14+
15+
async function fetchDependents() {
16+
const data = await fetch('https://dependents.47ng.com', {
17+
next: {
18+
revalidate: 86_400,
19+
tags: ['dependents']
20+
}
21+
}).then(res => res.json())
22+
return z.array(dependentSchema).parse(data)
23+
}
424

525
export async function DependentsSection() {
6-
let dependents: Result[] = []
26+
let dependents: Dependent[] = []
727
try {
8-
dependents = await crawlDependents()
28+
dependents = await fetchDependents()
929
} catch (error) {
1030
console.error(error)
1131
return <section className="text-red-500">{String(error)}</section>
@@ -30,13 +50,13 @@ export async function DependentsSection() {
3050
<div className="flex flex-wrap justify-center gap-1.5">
3151
{dependents.map(dep => (
3252
<a
33-
key={dep.repo}
34-
href={`https://github.com/${dep.repo}`}
53+
key={dep.owner + dep.name}
54+
href={`https://github.com/${dep.owner}/${dep.name}`}
3555
className="relative h-8 w-8 rounded-full"
3656
>
3757
<Image
38-
src={`https://avatars.githubusercontent.com/u/${dep.avatarID}?s=64&v=4`}
39-
alt={dep.repo}
58+
src={upscaleGitHubAvatar(dep.avatarURL, 64)}
59+
alt={dep.owner + '/' + dep.name}
4060
className="rounded-full"
4161
width={64}
4262
height={64}
@@ -54,3 +74,9 @@ export async function DependentsSection() {
5474
</section>
5575
)
5676
}
77+
78+
function upscaleGitHubAvatar(originalURL: string, size: number) {
79+
const url = new URL(originalURL)
80+
url.searchParams.set('s', size.toFixed())
81+
return url.toString()
82+
}

packages/docs/src/app/(pages)/_landing/dependents/crawler.ts

-115
This file was deleted.

packages/docs/src/app/(pages)/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Metadata } from 'next'
2-
import { DependentsSection } from './_landing/dependents/dependents'
2+
import { DependentsSection } from './_landing/dependents'
33
import { FeaturesSection } from './_landing/features'
44
import Footer from './_landing/footer'
55
import { HeroSection } from './_landing/hero'

packages/docs/src/app/api/isr/route.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { revalidateTag } from 'next/cache'
22
import { NextRequest, NextResponse } from 'next/server'
33

4-
const ACCEPTED_TAGS = ['github', 'github-actions-status', 'npm']
4+
const ACCEPTED_TAGS = ['github', 'github-actions-status', 'npm', 'dependents']
55

66
export async function GET(req: NextRequest) {
77
const token = req.nextUrl.searchParams.get('token')

0 commit comments

Comments
 (0)