Skip to content

Commit e45ca5f

Browse files
committed
initial impl
1 parent b99cc00 commit e45ca5f

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

web/src/app/api/agents/route.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { unstable_cache } from 'next/cache'
66

77
import { logger } from '@/util/logger'
88

9-
// Cache for 60 seconds with stale-while-revalidate
10-
export const revalidate = 60
9+
// Enable static generation for API route
10+
export const revalidate = 600 // Cache for 10 minutes
1111

1212
// Cached function for expensive agent aggregations
1313
const getCachedAgents = unstable_cache(
@@ -251,7 +251,7 @@ const getCachedAgents = unstable_cache(
251251
},
252252
['agents-data'],
253253
{
254-
revalidate: 60,
254+
revalidate: 60 * 10, // Cache for 10 minutes
255255
tags: ['agents'],
256256
}
257257
)
@@ -265,9 +265,13 @@ export async function GET() {
265265
// Add cache headers for CDN and browser caching
266266
response.headers.set(
267267
'Cache-Control',
268-
'public, max-age=60, s-maxage=60, stale-while-revalidate=300'
268+
'public, max-age=600, s-maxage=600, stale-while-revalidate=600'
269269
)
270270

271+
// Add additional headers for better CDN caching
272+
response.headers.set('Vary', 'Accept-Encoding')
273+
response.headers.set('X-Content-Type-Options', 'nosniff')
274+
271275
return response
272276
} catch (error) {
273277
logger.error({ error }, 'Error fetching agents')

web/src/app/store/page.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Suspense } from 'react'
22
import { Metadata } from 'next'
3-
import { getServerSession } from 'next-auth/next'
4-
import { authOptions } from '@/app/api/auth/[...nextauth]/auth-options'
53
import { unstable_cache } from 'next/cache'
64
import AgentStoreClient from './store-client'
75

@@ -84,29 +82,27 @@ export const metadata: Metadata = {
8482
},
8583
}
8684

87-
// Enable static generation with revalidation
88-
export const revalidate = 60
85+
// Enable static site generation with ISR
86+
export const revalidate = 60 * 10 // Revalidate every hour
8987

9088
interface StorePageProps {
9189
searchParams: { [key: string]: string | string[] | undefined }
9290
}
9391

9492
export default async function StorePage({ searchParams }: StorePageProps) {
95-
// Get session for conditional rendering
96-
const session = await getServerSession(authOptions)
97-
98-
// Fetch agents data at build time / on revalidation
93+
// Fetch agents data at build time
9994
const agentsData = await getCachedAgentsData()
10095

101-
// For now, pass empty array for publishers - client will handle this
96+
// For static generation, we don't pass session data
97+
// The client will handle authentication state
10298
const userPublishers: PublisherProfileResponse[] = []
10399

104100
return (
105101
<Suspense fallback={<StorePageSkeleton />}>
106102
<AgentStoreClient
107103
initialAgents={agentsData}
108104
initialPublishers={userPublishers}
109-
session={session}
105+
session={null} // Client will handle session
110106
searchParams={searchParams}
111107
/>
112108
</Suspense>

web/src/app/store/store-client.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useState, useMemo, useCallback, memo, useEffect, useRef } from 'react'
44
import { useQuery } from '@tanstack/react-query'
55
import { useWindowVirtualizer } from '@tanstack/react-virtual'
6+
import { useSession } from 'next-auth/react'
67
import {
78
Search,
89
TrendingUp,
@@ -29,7 +30,7 @@ import {
2930
SelectValue,
3031
} from '@/components/ui/select'
3132
import { toast } from '@/components/ui/use-toast'
32-
import { formatRelativeTime } from '@/lib/date-utils'
33+
import { RelativeTime } from '@/components/ui/relative-time'
3334
import { cn } from '@/lib/utils'
3435
import { useResponsiveColumns } from '@/hooks/use-responsive-columns'
3536
import type { Session } from 'next-auth'
@@ -93,9 +94,12 @@ const EDITORS_CHOICE_AGENTS = [
9394
export default function AgentStoreClient({
9495
initialAgents,
9596
initialPublishers,
96-
session,
97+
session: initialSession,
9798
searchParams,
9899
}: AgentStoreClientProps) {
100+
// Use client-side session for authentication state
101+
const { data: clientSession } = useSession()
102+
const session = clientSession || initialSession
99103
const [searchQuery, setSearchQuery] = useState(
100104
(searchParams.search as string) || ''
101105
)
@@ -403,7 +407,7 @@ export default function AgentStoreClient({
403407
className="text-xs text-muted-foreground/60"
404408
title={new Date(agent.last_used).toLocaleString()}
405409
>
406-
Used {formatRelativeTime(agent.last_used)}
410+
Used <RelativeTime date={agent.last_used} />
407411
</span>
408412
)}
409413
</div>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use client'
2+
3+
import { useState, useEffect } from 'react'
4+
import { formatRelativeTime } from '@/lib/date-utils'
5+
6+
interface RelativeTimeProps {
7+
date: string
8+
}
9+
10+
export function RelativeTime({ date }: RelativeTimeProps) {
11+
const [isClient, setIsClient] = useState(false)
12+
const [relativeTime, setRelativeTime] = useState('')
13+
14+
useEffect(() => {
15+
setIsClient(true)
16+
setRelativeTime(formatRelativeTime(date))
17+
18+
// Update every minute to keep relative time fresh
19+
const interval = setInterval(() => {
20+
setRelativeTime(formatRelativeTime(date))
21+
}, 60000)
22+
23+
return () => clearInterval(interval)
24+
}, [date])
25+
26+
// Show absolute date on server, relative time on client
27+
if (!isClient) {
28+
return <>{new Date(date).toLocaleDateString()}</>
29+
}
30+
31+
return <>{relativeTime}</>
32+
}

0 commit comments

Comments
 (0)