Next.js Discord

Discord Forum

Singleton API initialization not working correctly.

Unanswered
Spanish Water Dog posted this in #help-forum
Open in Discord
Spanish Water DogOP
I have expensive API that I want to only initialize once as it makes some web calls that need to be handled. I feel like it should work, but maybe im doing it wrong?

I attached how the api gets initialized (based on prisma's way of doing it) but it will will reinit on different routes.

such as home, /search, /search/player/[...].

The main issue is that every new player page (the catchall /search/player/[...]) reinits the API EVERY single time which I can have happening. Any thoughts?

This is how I use it given the ./api.ts file attached to the post.
import { api } from "./api"

console.log(api.getHeroes())
console.log(api.searchAccount("test123"))


in a test file like this, it works wonders but breaks with the reinitializing between pages. any way to get around this issue?

0 Replies