Next.js Discord

Discord Forum

Load data from db when user opens app based on subdomain

Unanswered
Hunting wasp posted this in #help-forum
Open in Discord
Hunting waspOP
Seems like a super simple problem that would already have a solution but I could not find anything relevant through searches.

I would like to load some data from a db when a user first loads the app based on what subdomain they are hitting, at the app level, regardless of which page they are going to preferably server side. How would I go about doing this?

3 Replies

@Robinhood Import next/headers and use headers().get('host'); Here are some documentation on next/headers: https://nextjs.org/docs/app/api-reference/functions/headers
Hunting waspOP
How would I utilize this at the app level though? I don't want to have to call the same function on every page.
@Hunting wasp How would I utilize this at the app level though? I don't want to have to call the same function on every page.
Next will cache your db fetch so calling it on every page shouldn't be a problem. But if you really do not want to fetch it on every page, you might have to store the data in cache. Otherwise, I would be interested to see what others might have to offer.