erin-fitzpatric / next-aom-gg

https://aom.gg
16 stars 13 forks source link

Reafactor fetchRedditPosts to be an API Route aka Serverless Function w/Cache #96

Closed erin-fitzpatric closed 1 week ago

erin-fitzpatric commented 3 weeks ago

Reddit posts are currently fetched directly from the server, rather than via a request to the API layer. https://github.com/erin-fitzpatric/next-aom-gg/blob/main/src/server/fetchRedditPosts.ts

In next.js, all routes created in the /api become a severless function when deployed to vercel https://vercel.com/docs/functions

  1. Move this functionality to a new endpoint in api/reddit/route.ts
  2. Refactor reddit-feed.tsx to make a get request to this new endpoint
  3. Cache the response by adding headers like:
    return new Response(JSON.stringify(civStatsAggregate), {
      headers: {
        "Content-Type": "application/json",
        "Cache-Control": "public, max-age=43200, stale-while-revalidate=43200",
      },
    });

    I think the cache could be set to 1 hour, but this is open for discussion. Seems like a reasonable refresh rate and will help cut down on requests every someone hits the home page.

JeanPec commented 2 weeks ago

Hey I can work on this one

JeanPec commented 2 weeks ago

I added a last commit, I wanted to impelment a retry logic in the Get querry but I found it easier to just retry when fetching the call every 500ms

judgemavi commented 1 week ago

I have a PR up that changes reddit post to SSR instead of client browser via useEffect. there should be no need for cache as page's default caching strategy will apply